Python 爬虫
Python 爬虫相关库
0x01.requests、re
主要是利用正则来进行匹配,得到想要的数据。前面已介绍,这里不做过多讲解,核心点是正则表达式。
0x02.BeautifulSoup
Beautiful Soup 是一个可以从 HTML 或 XML 中提取数据的 Python 库。它可以通过你喜欢的转换器快速帮你解析并查找整个HTML文档。官方文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
2.1 安装
主要安装 BeautifulSoup 和 lxml:pip install bs4、pip install lxml。
2.2 解析器
BeautifulSoup 在解析时依赖解析器,它除了支持 Python 标准库中的 HTML 解析器外,还支持一些第三方库(比如 lxml)。
| 解析器 | 使用方法 | 优势 | 劣势 |
|---|---|---|---|
| Python 标准库 | BeautifulSoup(markup,'html.parser') | python 内置的标准库,执行速度适中 | Python 3.2.2之前的版本容错能力差 |
| lxmlHTML 解析器 | BeautifulSoup(markup,'lxml') | 速度快、文档容错能力强 | 需要安装C语言库 |
| lxmlXML 解析器 | BeautifulSoup(markup,'xml') | 速度快,唯一支持XML的解析器 | 需要安装C语言库 |
| html5lib | BeautifulSoup(markup,'html5lib') | 最好的容错性、以浏览器的方式解析文档、生成HTML5格式的文档 | 速度慢、不依赖外部拓展 |
lxml 解析器可以解析 HTML 和 XML 文档,并且速度快,容错能力强。如果使用 lxml,那么在初始化的 BeautifulSoup时候,可以把第二个参数设为 lxml 即可。如:
from bs4 import BeautifulSoup
soup=BeauifulSoup('<h1>Hello Hacker</h1>','lxml')
print(soup.h1)
2.3 简单使用
#coding=utf-8
import requests
from bs4 import BeautifulSoup
# 准备网站
url = "http://www.baidu.com"
# 爬取内容
res = requests.get(url)
# 获取内容
html = res.text
# 初始化BeautifulSoup
soup = BeautifulSoup(res.content, 'html.parser')
# 获取标题标签
print(soup.title)
# 获取标题文本
print(soup.title.string)

2.4 解析数据的方法
2.4.1 soup.tagName
返回该标签第一次出现的内容
# coding=utf-8
import requests
from bs4 import BeautifulSoup
# 准备网站
url = "http://www.baidu.com"
# 爬取内容
res = requests.get(url)
# 设置编码为 UTF-8
res.encoding = 'utf-8'
# 获取内容
html = res.text
# 初始化BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
# 获取a标签第一次出现的内容
print(soup.a)

2.4.2 soup.find
find()主要是有两个方法:
返回某个标签第一次出现的内容,等同于上面的soup.tagName。
属性定位:用于查找某个有特定性质的标签。
(1)返回标签第一次出现的内容
# coding=utf-8
import requests
from bs4 import BeautifulSoup
# 准备网站
url = "http://www.baidu.com"
# 爬取内容
res = requests.get(url)
# 设置编码为 UTF-8
res.encoding = 'utf-8'
# 获取内容
html = res.text
# 初始化BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
# 获取a标签第一次出现的内容
print(soup.find('a'))
(2)属性定位
通过id
找id="main-menu"的ul
# coding=utf-8
import requests
from bs4 import BeautifulSoup
# 准备网站
url = "http://www.baidu.com"
# 爬取内容
res = requests.get(url)
# 设置编码为 UTF-8
res.encoding = 'utf-8'
# 获取内容
html = res.text
# 初始化BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
# 获取a标签第一次出现的内容
print(soup.find('ul',id='main-menu'))
通过class
在BS4中规定,如果遇到要查询class情况,需要使用class_来代替:
# coding=utf-8
import requests
from bs4 import BeautifulSoup
# 准备网站
url = "http://www.baidu.com"
# 爬取内容
res = requests.get(url)
# 设置编码为 UTF-8
res.encoding = 'utf-8'
# 获取内容
html = res.text
# 初始化BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
# 获取a标签第一次出现的内容
print(soup.find('a',class_='smooth'))
通过attrs
# coding=utf-8
import requests
from bs4 import BeautifulSoup
# 准备网站
url = "http://www.baidu.com"
# 爬取内容
res = requests.get(url)
# 设置编码为 UTF-8
res.encoding = 'utf-8'
# 获取内容
html = res.text
# 初始化BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
# 获取a标签第一次出现的内容
print(soup.find('span',attrs={'class':'title'}))
print(soup.find(attrs={'class':'title'}))
2.4.3 soup.find_all
该方法返回的是指定标签下面的所有内容,而且是列表的形式;传入的方式是多种多样的。
(1)传入单个标签
# coding=utf-8
import requests
from bs4 import BeautifulSoup
# 准备网站
url = "http://www.baidu.com"
# 爬取内容
res = requests.get(url)
# 设置编码为 UTF-8
res.encoding = 'utf-8'
# 获取内容
html = res.text
# 初始化BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
# 获取a标签第一次出现的内容
a_list=soup.find_all('a')
for a in a_list:
print(a)
(2)传入多个标签
# coding=utf-8
import requests
from bs4 import BeautifulSoup
# 准备网站
url = "http://www.baidu.com"
# 爬取内容
res = requests.get(url)
# 设置编码为 UTF-8
res.encoding = 'utf-8'
# 获取内容
html = res.text
# 初始化BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
con_list=soup.find_all(['a','span'])
for item in con_list:
print(item)
(3)传入正则表达式
# coding=utf-8
import requests,re
from bs4 import BeautifulSoup
# 准备网站
url = "http://www.baidu.com"
# 爬取内容
res = requests.get(url)
# 设置编码为 UTF-8
res.encoding = 'utf-8'
# 获取内容
html = res.text
# 初始化BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
con_list=soup.find_all(re.compile('^span'))
for item in con_list:
print(item)
2.4.4 soup.select
这个基本就是和 css 选择器一致了。
# 标签选择器
res = soup.select('a') #返回包含页面中所有 <a> 标签元素的列表
# 类选择器
res = soup.select('标签名.类名')
res = soup.select('a.smooth')
# id选择器
# <div id="指纹识别">asdf</div>
res = soup.select('#指纹识别')
# 层级选择
res = soup.select('.main-menu .title')
res = soup.select('.main-menu > li > a > .title')
2.4.5 获取文本内容
主要有三个方法:
.text
.string
.get_text()
# 获取a
res = soup.find('a')
# 得到里面的内容
con = res.text
print(con)
con = res.string
print(con)
con = res.get_text()
print(con)
区别:.text和.get_text()一致;.string只能获取直接的字符串内容,不能获取子级标签中的字符串。
2.4.6 获取属性值
在BS中,可以通过中括号,获取html标签的属性值。
# coding=utf-8
import requests,re
from bs4 import BeautifulSoup
# 准备网站
url = "http://www.baidu.com"
# 爬取内容
res = requests.get(url)
# 设置编码为 UTF-8
res.encoding = 'utf-8'
# 获取内容
html = res.text
# 初始化BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
#获取a
res=soup.find_all('a',attrs={'class':'smooth'})
#打印类型
print(type(res[0]))
#获取href属性
print(res[0]['href'])
#获取a下面span的类名
print(res[0].span['class'])

浙公网安备 33010602011771号