# coding = utf-8
# BeautifulSoup 主要功能是解析提取HTML数据
# re lxml bs4 # pip install Beautifulsoup4 # from bs4 import BeautifulSoup html = '''
<html><head><title>The Dormouse's story</title></head> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p> <p class="story">...</p> '''
############################################################################
# BeautifulSoup部分
############################################################################# # soup = BeautifulSoup(html, 'lxml') # 四大对象种类:Tag NavigableString Beautifulsoup Comment # print(soup.a) # 获取a标签
# print(soup.a.get('href')) # 取a标签的属性,获得超链接
# print(soup.a.text) # 获取a标签下的文本,若a下有子标签,可能获取不到
# print(soup.a.string) # 获取a标签(包含a下的子标签)下的文本 # 搜索文档:find find_all 按照一定的过滤条件进行匹配 # 字符串
# print(soup.find_all('a')) # 匹配整个文档中的a标签
# print(soup.find_all(attrs={'class': 'title'})) # 匹配class为title的标签 # #正则表达式
# import re
# print(soup.find_all(re.compile('^p'))) # 匹配以p开头的标签
# print(soup.find_all(re.compile('y$'))) # 匹配以y结尾的标签
# print(soup.find_all(re.compile('t'))) # 匹配包含t的标签 # 列表
# for tag in soup.find_all(['a', 'b']): # 匹配a标签,b标签
# print(tag) # for tag in soup.find_all('p', class_='story'): # 匹配class=story的p标签
# print(tag) # # 方法 给find_all传入一个方法作为过滤条件
# def has_class_but_no_id(tag):
# """
# 定义一个判断有class属性但是没有id属性的方法,作为过滤条件
# """
# return tag.has_attr('class') and not tag.has_attr('id')
#
# for tag in soup.find_all(has_class_but_no_id):
# print(tag) # css选择器
# print(soup.select('title')) # 通过标签名查找
# print(soup.select('.sister')) # 通过class名查找
# print(soup.select('#link1')) # 通过id名查找
# print(soup.select('p #link2')) # 组合查找,id为link2的p标签 # > 只能够一级一级向下查找
# print(soup.select('body > p .sister')) # 查找body下类名为sister的p # 百度搜索python,对返回页面进行属性查找
# import requests
# url = 'http://www.baidu.com/s?wd=python'
# response = requests.get(url) # 获取的数据是网页源代码,未经过js渲染
#
# soup = BeautifulSoup(response.text, 'lxml') # 查找返回页面搜索到的结果
# items = soup.find_all('div', class_='result c-container ') # 打印搜索结果
# for item in items:
# print(item.select('h3 > a')[0].get('href') # 取a标签
# print(item.select('h3 > a')[0].get_text()) #################################################################################
# xpath 部分
# 通配符 / // @ # . ..
# /表示从当前节点匹配 //整个文档匹配 @选取属性 *
########################################################################################
html = '''
<html><head><title>The Dormouse's story</title></head>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
'''
# from lxml import etree
# e = etree.HTML(html)
# for i in e.xpath('//p'): # 整个文档中搜索p标签
# # print(i.xpath('string(.)')) # 获取当前标签下所有文本(标签下套标签),包括下面子标签的文本
# print(i.text) # 匹配当前标签下的文本内容,不包含子标签 """
# for i in e.xpath('//p/@class'): # 选取p的class属性
# for i in e.xpath('//p[@class=title]'): # 搜索class=title的p标签
//title[@*] 匹配所有有属性的title标签
"""
# 百度搜索python,用xpath查找
import requests
from lxml import etree url = 'http://www.baidu.com/s?wd=python'
response = requests.get(url) # 获取的数据是网页源代码
tree = etree.HTML(response.text) # 查找返回页面搜索到的结果
items = tree.xpath('//div[@class="result c-container "]')
for item in items:
# print(item.xpath('h3/a/@href'))
print(item.xpath('h3/a')[0].xpath('string(.)'))

最新文章

  1. [Amazon] Amazon IAP for Unity
  2. PSP(11.24~11.30)
  3. 优秀工具推荐:两款很棒的 HTML5 游戏开发工具
  4. Sublime多行编辑快捷键
  5. MorkDown 常用语法总结
  6. 细化如何安装LNMP + Zabbix 监控安装文档以及故障排除
  7. 实现远程连接ACCESS数据库的方法
  8. javascript中静态方法、实例方法、内部方法和原型的一点见解
  9. 视频最后用使用了function(i,ot)一笔带过,但我看不懂i和ot这2个参数的具体值是怎么获取得到的,能不能说一下参数传递过程?
  10. Codevs 1198 国王游戏 2012年NOIP全国联赛提高组
  11. 基于Redis的CAS服务端集群
  12. Python 显示LinkedIn用户作业
  13. GJB150-2009军用装备实验室环境试验方法新版标准
  14. Node.js微服务实践(一)
  15. 【UOJ448】【集训队作业2018】人类的本质 min_25筛
  16. tp5 mkdir(): Permission denied 问题
  17. 定位bug的方法总结
  18. C++:运算符重载
  19. webstorm软件小技巧
  20. [BZOJ1185][HNOI2007]最小矩形覆盖-[凸包+旋转卡壳]

热门文章

  1. Python中的is和==的区别
  2. mac os 10.15 virtualBox6.0.12崩溃
  3. linux 编译引用动态库时,报GLIBC_2,14 not found的处理方法
  4. 证明:S = 1 + 1/2 + 1/4 + 1/8 + 1/16 + &#183;&#183;&#183;&#183;&#183;&#183;&#183;,求证 S = 2
  5. 关于路由器漏洞利用,qemu环境搭建,网络配置的总结
  6. 02-model设计
  7. jquery jssdk分享报错解决方法
  8. 从 DevOps 到 Serverless:通过“不用做”的方式解决“如何更高效做”的问题
  9. getClass()和instanceof以及类的equals方法
  10. centos6官网镜像dvd1和dvd2的解释