Infi-chu:

http://www.cnblogs.com/Infi-chu/

Beautiful Soup

借助网页的结构和属性等特性来解析网页,这样就可以省去复杂的正则表达式的编写。

Beautiful Soup是Python的一个HTML或XML的解析库。

1.解析器

解析器 使用方法 优势 劣势
Python标准库 BeautifulSoup(markup,"html.parser") 执行速度适中、文档容错能力强 2.7.3和3.2.2之前的版本容错能力差
lxml HTML解析器 BeautifulSoup(markup,"lxml") 速度快、文档容错能力强 需要安装C语言库
lxml XML解析器 BeautifulSoup(markup,"xml") 速度快,唯一支持XML的解析器 需要安装C语言库
html5lib BeautifulSoup(markup,"html5lib") 最好的容错性、以浏览器的方式解析文档、生成HTML5格式的文档 速度慢、不依赖外部扩展

综上所述,推荐lxml HTML解析器

from bs4 import BeautifulSoup
soup = BeautifulSoup('<p>Hello World</p>','lxml')
print(soup.p.string)

2.基本用法:

html = '''
<html>
<head><title>Infi-chu example</title></head>
<body>
<p class="title" name="dr"><b>title example</b></p>
<p class="story">link
<a href="http://example.com/elsie" class="sister" id="link1">elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">lacie</a>,
<a href="http://example.com/tillie" class="sister" id="link3">tillie</a>,
last sentence</p>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html,'lxml')
print(soup.prettify()) # 修复html
print(soup.title.string) # 输出title节点的字符串内容

3.节点选择器:

选择元素

使用soup.元素的方式获取

提取信息

(1)获取名称

使用soup.元素.name获取元素名称

(2)获取属性

使用soup.元素.attrs

使用soup.元素.attrs['name']

(3)元素内容

使用soup.元素.string获取内容

嵌套选择

使用soup.父元素.元素.string获取内容

关联选择

(1)子节点和子孙节点

html = '''
<html>
<head><title>Infi-chu example</title></head>
<body>
<p class="title" name="dr"><b>title example</b></p>
<p class="story">link
<a href="http://example.com/elsie" class="sister" id="link1"><span>elsie</span></a>,
<a href="http://example.com/lacie" class="sister" id="link2"><span>lacie</span></a>,
<a href="http://example.com/tillie" class="sister" id="link3"><span>tillie</span></a>,
last sentence</p>
'''
from bs4 import BeautifulSoup
# 得到直接子节点,children属性
soup = BeautifulSoup(html,'lxml')
print(soup.p.children)
for i ,child in enumerate(soup.p.children):
print(i,child) # 得到所有的子孙节点,descendants属性
soup = BeautifulSoup(html,'lxml')
print(soup.p.descendants)
for i,child in enmuerate(soup.p.descendants):
print(i,child)

(2)父节点和祖先节点

调用父节点,使用parent属性

获取所有祖先节点,使用parents属性

(3)兄弟节点

next_sibling  下一个兄弟元素

previous_sibling  上一个兄弟元素

next_siblings  所有前面兄弟节点

previous_siblings  所有后面兄弟节点

(4)提取信息

4.方法选择器:

find_all()

find_all(name,attrs,recursize,text,**kwargs)

(1)name

soup.find_all(name='ul')
for ul in soup.find_all(name='ul'):
print(ul.find_all(name='ul'))
for ul in soup.find_all(name='ul'):
print(ul.find_all(name='li'))
for li in ul.find_all(name='li'):
print(li.string)

(2)attes

# 根据节点名查询
print(soup.find_all(attrs={'id':'list1'}))
print(soup.find_all(attrs={'name':'elements'})) # 也可以写成
print(soup.find_all(id='list1'))
print(soup.find_all(class='elements'))

(3)text

text参数可以用来匹配节点的文本,传入的形式可以是字符串,可以是正则表达式对象

from bs4 import BeautifulSoup
soup = BeautifulSoup(html,'lxml')
print(soup.find_all(text=re.compile('link')))

find()

返回一个元素

【注】

find_parents()和find_parent()

find_next_siblings()和find_next_sibling()

find_previous_siblings()和find_previous_sibling()

find_all_next()和find_next()

find_all_previous()和find_previous()

5.CSS选择器:

嵌套选择

for ul in soup.select('ul'):
print(ul.select('li'))

获取属性

for ul in soup.select('ul'):
print(ul['id'])
# 等价于
print(ul.attrs['id'])

获取文本

获取文本除了string属性还有get_text()方法

for li in soup.select('li'):
# 效果一样
print(li.get_text())
print(li.string)

Python3爬虫(六) 解析库的使用之Beautiful Soup的更多相关文章

  1. 爬虫(五)—— 解析库(二)beautiful soup解析库

    目录 解析库--beautiful soup 一.BeautifulSoup简介 二.安装模块 三.Beautiful Soup的基本使用 四.Beautiful Soup查找元素 1.查找文本.属性 ...

  2. ubuntu下的python网页解析库的安装——lxml, Beautiful Soup, pyquery, tesserocr

    lxml 的安装(xpath) pip3 install lxml 可能会缺少以下依赖: sudo apt-get install -y python3-dev build-e ssential li ...

  3. Python爬虫【解析库之beautifulsoup】

    解析库的安装 pip3 install beautifulsoup4 初始化 BeautifulSoup(str,"解析库") from bs4 import BeautifulS ...

  4. python爬虫之解析库Beautiful Soup

    为何要用Beautiful Soup Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式, 是一个 ...

  5. 爬虫之解析库-----re、beautifulsoup、pyquery

    一.介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...

  6. Python爬虫【解析库之pyquery】

    该库跟jQuery的使用方法基本一样  http://pyquery.readthedocs.io/ 官方文档 解析库的安装 pip3 install pyquery 初始化 1.字符串初始化 htm ...

  7. python爬虫三大解析库之XPath解析库通俗易懂详讲

    目录 使用XPath解析库 @(这里写自定义目录标题) 使用XPath解析库 1.简介   XPath(全称XML Path Languang),即XML路径语言,是一种在XML文档中查找信息的语言. ...

  8. 爬虫之解析库BeautifulSoup

    介绍 Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等 ...

  9. 【Python爬虫学习笔记(3)】Beautiful Soup库相关知识点总结

    1. Beautiful Soup简介     Beautiful Soup是将数据从HTML和XML文件中解析出来的一个python库,它能够提供一种符合习惯的方法去遍历搜索和修改解析树,这将大大减 ...

随机推荐

  1. 模拟ArrayList

    package com.helloidea; import java.util.ArrayList; import java.util.Collection; import java.util.Lis ...

  2. DOS下启动MySQL时输入net start mysql 提示服务名无效的问题

    原因:mysql服务名错误. 正确做法:net start  +mysql服务名

  3. python入门24 json模块

    现在很多接口传参和响应都是json串. json.dumps()  python对象转化为json字符串 json.loads()  json字符串转化为python对象.[]转变成list,{}转变 ...

  4. 十天精通CSS3

    课程地址:http://www.imooc.com/learn/33 第1章 初识CSS3 CSS3课程列出第一站,先带领大家进入CSS3的世界,探索CSS3的魅力! 你做好准备了吗? 第2章 边框 ...

  5. BZOJ4520:[CQOI2016]K远点对(K-D Tree)

    Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 N 行,每行两个整数 X,Y,表示一个点 的坐标 ...

  6. cftool拟合&函数逼近

    cftool拟合&函数逼近 cftool 真是神奇,之前我们搞的一些线性拟合解方程,多项式拟合,函数拟合求参数啊,等等. 已经超级多了,为啥还得搞一个cftool拟合啊?而且毫无数学理论. 如 ...

  7. ACM-ICPC(10/23)

    贪心 区间相关问题 选择不相交区间: hdu 2037 给定一些区间,选择尽量多的区间,他们互相不交叉.(活动安排问题) 分析:贪心思路是解决活动安排问题的好方案. 按照区间右端点排序,从前往后遍历, ...

  8. 2018.11.26 struts2流程源码

    struts2的架构图 从最上面的类开始,也就是i 我们的核心过滤器strutsPrepareAndExecuteFilter 判断当前请求是否由struts2来处理,如果是就往else走,不由它来处 ...

  9. vue快速使用

    1.引用脚本 <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script ...

  10. 【luogu P1807 最长路_NOI导刊2010提高(07)】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1807 求最大路?就是把权值取相反数跑最短路. #include <cstdio> #includ ...