BeautifulSoup库
'''灵活又方便的网页解析库,处理高效,支持多种解析器。利用它不用编写正则表达式即可方便的实现网页信息的提取。'''
BeautifulSoup库包含的一些解析库:
解析库 使用方法 优势 劣势
python标准库 BeautifulSoup(markup,"html.parser") python内置标准库、执行速度适中、文档容错能力强 python 2.7.0 or 3.2.2前的版本中文容错能力差
lxml HTML解析库 BeautifulSoup(markup,"lxml") 速度快、文档容错能力强 需要安装C语言库
lxml XML解析库 BeautifulSuop(markup,"xml") 速度快、唯一支持XML的解析库 需要安装C语言库
html5lib BeautifulSoup(markup,"html5lib") 最好的容错性、以浏览器的方式解析文档、生成HTML5格式的文档 速度慢、不依赖外部扩展
'''基本使用'''
html = '''
<html lang="zh-cn">
<head>
<title>翱游 - 博客园</title>
</head>
<body>
<p class="tab" name="morning">
good night
<a name="top" href="http://www.123.com">
<span>wisir</span>
</a>
<a href="hello.cn">hh</a>
<a href="man.cn">man</a>
</p>
<p class="cp" name="dromouse">hello,world</p>
<p>...</p>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html,"lxml")
print(soup.prettify()) #自动格式化输出与补全代码 '''标签选择器'''
print("-标签选择器-"*20)
#选择元素:返回匹配的第一个元素
print(type(soup.title),soup.title)
print(soup.p)
#获取名称
print(soup.title.name)
#获取属性
print(soup.p.attrs['name'])
print(soup.p['name'])
#获取内容
print(soup.title.string)
#嵌套选择
print(soup.head.title.string)
#子节点和子孙节点
print(soup.p.contents) #返回以子节点为元素的列表
print(soup.p.children) #.children得到的是一个迭代器对象,.contents得到的是一个列表
for i,child in enumerate(soup.p.children):
print(i,child)
print(soup.p.descendants) #.descendants获取所有的子孙节点
for i,child in enumerate(soup.p.descendants):
print(i,child)
#父节点和祖先结点
print(soup.a.parent) #输出父节点
print(list(enumerate(soup.a.parents))) #输出祖先结点,层层向上
#兄弟节点
print(list(enumerate(soup.a.next_siblings))) #输出所有的后兄弟节点
print(list(enumerate(soup.a.previous_siblings))) #输出所有的前兄弟节点
'''find_all(name,attrs,recursive,text,**kwargs):可根据标签名、属性、内容查找文档'''
print("-find_all-"*20)
html = '''
<div class="panel">
<div class="panel-heading">
<h4>Hello</h4>
</div>
<div class="panel-body">
<ul class="list" id="list-1" name="element">
<li class="element">Foo</li>
<li class="element">Bar</li>
<li class="element">Jay</li>
</ul>
<ul class="list list-small" id="list-2">
<li class="element">Foo</li>
<li class="element">Bar</li>
</ul>
</div>
</div>
'''
soup = BeautifulSoup(html,"lxml")
#name
print(soup.find_all('ul'))
print(type(soup.find_all("ul")[0]))
#attrs
print(soup.find_all(attrs={"id":"list-1"}))
print(soup.find_all(attrs={"name":"element"}))
#text
print(soup.find_all(text="Foo")) '''find(name,attrs,recursive,text,**kwargs):返回find_all方法的第一个元素'''
print(soup.find("ul")) '''
find_parents()与find_parents
find_next_siblings()与find_next_sibling()
find_previous_siblings()与find_previous_sibling()
find_all_next()与find_next()
find_all_previous()和find_previous()
同find_all()与find()
''' '''CSS选择器'''
print("-CSS选择器-"*20)
#通过select直接传入CSS选择器即可完成选择,返回符合要求的所有结果为元素的列表
print(soup.select('.panel .panel-heading')) #类选择器前加.,层级选择器之间加空格
print(soup.select('ul li'))
print(soup.select('#list-2 .element'))
print(type(soup.select('ul')[0]))
#获取属性
for ul in soup.select('ul'):
print(ul['id'])
print(ul.attrs['id'])
#获取内容
for li in soup.select('li'):
print(li.get_text()) '''
总结:
推荐使用lxml解析库,必要时使用html.parser
标签选择筛选功能弱但是速度快
建议使用find()、find_all()查询匹配单个结果或多个结果
如果对CSS选择器熟悉建议使用select()
记住常用的获取属性和文本值的方法
'''
BeautifulSoup库的更多相关文章
- Python爬虫小白入门(三)BeautifulSoup库
# 一.前言 *** 上一篇演示了如何使用requests模块向网站发送http请求,获取到网页的HTML数据.这篇来演示如何使用BeautifulSoup模块来从HTML文本中提取我们想要的数据. ...
- BeautifulSoup库children(),descendants()方法的使用
BeautifulSoup库children(),descendants()方法的使用 示例网站:http://www.pythonscraping.com/pages/page3.html 网站内容 ...
- 网络爬虫BeautifulSoup库的使用
使用BeautifulSoup库提取HTML页面信息 #!/usr/bin/python3 import requests from bs4 import BeautifulSoup url='htt ...
- BeautifulSoup库的使用
1.简介 BeautifulSoup库也是一个HTML/XML的解析器,其使用起来很简单,但是其实解析网站用xpath和re已经足矣,这个库其实很少用到.因为其占用内存资源还是比xpath更高. '' ...
- python爬虫学习之使用BeautifulSoup库爬取开奖网站信息-模块化
实例需求:运用python语言爬取http://kaijiang.zhcw.com/zhcw/html/ssq/list_1.html这个开奖网站所有的信息,并且保存为txt文件和excel文件. 实 ...
- python下载安装BeautifulSoup库
python下载安装BeautifulSoup库 1.下载https://www.crummy.com/software/BeautifulSoup/bs4/download/4.5/ 2.解压到解压 ...
- 基于BeautifulSoup库的HTML内容的查找
一.BeautifulSoup库提供了一个检索的参数: <>.find_all(name,attrs,recursive,string,**kwargs),它返回一个列表类型,存储查找的结 ...
- python BeautifulSoup库的基本使用
Beautiful Soup 是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖析树(parse tree). 它提供简单又常用的导航(navigating),搜索以 ...
- python爬虫学习(一):BeautifulSoup库基础及一般元素提取方法
最近在看爬虫相关的东西,一方面是兴趣,另一方面也是借学习爬虫练习python的使用,推荐一个很好的入门教程:中国大学MOOC的<python网络爬虫与信息提取>,是由北京理工的副教授嵩天老 ...
随机推荐
- 解决 win10 由于磁盘缓慢造成机器迟钝
关闭 windows 的superfetch服务 建议禁止 superfetch服务: http://www.360quan.com/safe/6946.html 操作: http://jingyan ...
- 4.7Python数据处理篇之Matplotlib系列(七)---matplotlib原理分析
目录 目录 前言 (一)总框架分析 (二)函数式的绘图 1.说明: 2.函数绘图的缺优点 3.绘图类的函数 4.操作类的函数 5.例子: (三)面向对象式的绘图 1.基本概念 2.基本对象 3.面向对 ...
- 【转载】ubuntu下/usr/bin和/usr/local/bin的区别
这篇文章已经无法考证是谁原创的了 首先注意usr 指 Unix System Resource,而不是User 然后通常: /usr/bin下面的都是系统预装的可执行程序,会随着系统升级而改变. /u ...
- March 07th, 2018 Week 10th Wednesday
Better later than never. 亡羊补牢,时犹未晚. Time and again all of us are told to complete the tasks assigned ...
- 聚类——KFCM
聚类——认识KFCM算法 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 一.KFCM概述 KFCM:基于核的改进的模糊c均值聚类算法.它是通过核函数将 ...
- webpack常见的配置项
使用vue init webpack test(项目文件夹名)命令初始化一个vue项目,cd test,然后安装依赖npm install之后会生成一些默认的文件夹和文件,这些文件和文件夹中有些和配置 ...
- php面试题整理(五)
表单也得改
- 深入理解Ribbon之源码解析
什么是Ribbon Ribbon是Netflix公司开源的一个负载均衡的项目,它属于上述的第二种,是一个客户端负载均衡器,运行在客户端上.它是一个经过了云端测试的IPC库,可以很好地控制HTTP和TC ...
- Python中的__new__()方法与实例化
@Python中的__new__()方法与实例化 __new__()是在新式类中新出现的方法,它作用在构造方法建造实例之前,可以这么理解,在Python 中 存在于类里面的构造方法__init__ ...
- Integer判断大于 == 127时的坑
在一次判断返回Interger类型的code, 用==结果, 没进去 Integer的值在-128到127时,Integer对象是在IntegerCache.cache产生,会复用已有对象,也就是说 ...