Python爬虫学习笔记之爬虫基础库-python爬虫入门教程(非常详细)

384次阅读
没有评论

知识预览

  • ​​beautifulsoup的简单使用​​
  • ​​beautifulsoup的遍历文档树​​
  • ​​beautifulsoup的搜索文档树​​
  • ​​beautifulsoup的css选择器​​

​​回到顶部​​

beautifulsoup的简单使用

简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据。官方解释如下:

”’
Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。
它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序。
”’

安装

pip3 install beautifulsoup4

 

解析器

Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python 会使用 Python默认的解析器,lxml 解析器更加强大,速度更快,推荐安装。

pip3 install lxml

另一个可供选择的解析器是纯Python实现的 html5lib , html5lib的解析方式与浏览器相同,可以选择下列方法来安装html5lib:

pip install html5lib

 

解析器对比 

Python爬虫学习笔记之爬虫基础库-python爬虫入门教程(非常详细)

 

​​官网文档​​

快速开始

下面的一段HTML代码将作为例子被多次用到.这是 爱丽丝梦游仙境的 的一段内容(以后内容中简称为 爱丽丝 的文档):

Python爬虫学习笔记之爬虫基础库_字符串_02

Python爬虫学习笔记之爬虫基础库_爬虫库_03

html_doc = “””
<html><head><title>The Dormouse’s story</title></head>
<body>
<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>
“””

Python爬虫学习笔记之爬虫基础库_html_04

Python爬虫学习笔记之爬虫基础库_子节点_05

使用BeautifulSoup解析这段代码,能够得到一个 ​​BeautifulSoup​​ 的对象,并能按照标准的缩进格式的结构输出:

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, ‘html.parser’)

print(soup.prettify()

Python爬虫学习笔记之爬虫基础库_子节点_06

<html>
<head>
<title>
The Dormouse’s story
</title>
</head>
<body>
<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 class=”sister” href=”http://example.com/elsie” id=”link1″>
Elsie
</a>
,
<a class=”sister” href=”http://example.com/lacie” id=”link2″>
Lacie
</a>
and
<a class=”sister” href=”http://example.com/tillie” id=”link3″>
Tillie
</a>
;
and they lived at the bottom of a well.
</p>
<p class=”story”>

</p>
</body>
</html>

Python爬虫学习笔记之爬虫基础库_字符串_07

几个简单的浏览结构化数据的方法:

Python爬虫学习笔记之爬虫基础库_子节点_08

Python爬虫学习笔记之爬虫基础库_html_09

soup.title
# <title>The Dormouse’s story</title>

soup.title.name
# u’title’

soup.title.string
# u’The Dormouse’s story’

soup.title.parent.name
# u’head’

soup.p
# <p class=”title”><b>The Dormouse’s story</b></p>

soup.p[‘class’]
# u’title’

soup.a
# <a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>

soup.find_all(‘a’)
# [<a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>,
# <a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>,
# <a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>]

soup.find(id=”link3″)
# <a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>

Python爬虫学习笔记之爬虫基础库_子节点_10

Python爬虫学习笔记之爬虫基础库_html_11

从文档中找到所有<a>标签的链接:

for link in soup.find_all(‘a’):
print(link.get(‘href’))
# http://example.com/elsie
# http://example.com/lacie
# http://example.com/tillie

从文档中获取所有文字内容:

print(soup.get_text())

如何使用

将一段文档传入BeautifulSoup 的构造方法,就能得到一个文档的对象, 可以传入一段字符串或一个文件句柄.

from bs4 import BeautifulSoup

soup = BeautifulSoup(open(“index.html”))

soup = BeautifulSoup(“<html>data</html>”)

然后,Beautiful Soup选择最合适的解析器来解析这段文档,如果手动指定解析器那么Beautiful Soup会选择指定的解析器来解析文档。

对象的种类

Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为种

​​Tag​​ , ​​NavigableString​​ , ​​BeautifulSoup​​ , ​​Comment​​ .

Tag

​​通俗点讲就是 HTML 中的一个个标签,Tag​​ 对象与XML或HTML原生文档中的tag相同:

soup = BeautifulSoup(‘<b class=”boldest”>Extremely bold</b>’)
tag = soup.b
type(tag)
# <class ‘bs4.element.Tag’>

tag的名字

soup对象再以爱丽丝梦游仙境的html_doc为例,操作文档树最简单的方法就是告诉它你想获取的tag的name.如果想获取 <head> 标签,只要用 ​​soup.head​​ :

soup.head
# <head><title>The Dormouse’s story</title></head>

soup.title
# <title>The Dormouse’s story</title>

这是个获取tag的小窍门,可以在文档树的tag中多次调用这个方法.下面的代码可以获取<body>标签中的第一个<b>标签:

soup.body.b
# <b>The Dormouse’s story</b>

通过点取属性的方式只能获得当前名字的第一个tag:

soup.a
# <a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>

如果想要得到所有的<a>标签,或是通过名字得到比一个tag更多的内容的时候,就需要用到 Searching the tree 中描述的方法,比如: find_all()

soup.find_all(‘a’)
# [<a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>,
# <a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>,
# <a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>]

我们可以利用 soup加标签名轻松地获取这些标签的内容,注意,它查找的是在所有内容中的第一个符合要求的标签。

name和attributes属性

Tag有很多方法和属性,现在介绍一下tag中最重要的属性: name和attributes

每个tag都有自己的名字,通过 ​​.name​​ 来获取:

Python爬虫学习笔记之爬虫基础库_爬虫库_12

Python爬虫学习笔记之爬虫基础库_子节点_13

tag.name
# u’b’

tag[‘class’]
# u’boldest’

tag.attrs
# {u’class’: u’boldest’}

Python爬虫学习笔记之爬虫基础库_爬虫库_14

Python爬虫学习笔记之爬虫基础库_字符串_15

tag的属性可以被添加,删除或修改. 再说一次, tag的属性操作方法与字典一样

Python爬虫学习笔记之爬虫基础库_html_16

Python爬虫学习笔记之爬虫基础库_字符串_17

tag[‘class’] = ‘verybold’
tag[‘id’] = 1
tag
# <blockquote class=”verybold” id=”1″>Extremely bold</blockquote>

del tag[‘class’]
del tag[‘id’]
tag
# <blockquote>Extremely bold</blockquote>

tag[‘class’]
# KeyError: ‘class’
print(tag.get(‘class’))
# None

Python爬虫学习笔记之爬虫基础库_子节点_18

Python爬虫学习笔记之爬虫基础库_爬虫库_19

NavigableString(字符串)

既然我们已经得到了标签的内容,那么问题来了,我们要想获取标签内部的文字怎么办呢?很简单,用 .string 即可.

字符串常被包含在tag内.Beautiful Soup用 ​​NavigableString​​ 类来包装tag中的字符串,通过 ​​unicode()​​ 方法可以直接将 ​​NavigableString​​ 对象转换成Unicode字符串:

Python爬虫学习笔记之爬虫基础库_html_20

Python爬虫学习笔记之爬虫基础库_子节点_21

tag.string
# u’Extremely bold’
type(tag.string)
# <class ‘bs4.element.NavigableString’>

unicode_string = unicode(tag.string)
unicode_string
# u’Extremely bold’
type(unicode_string)
# <type ‘unicode’>

Python爬虫学习笔记之爬虫基础库_子节点_22

Python爬虫学习笔记之爬虫基础库_子节点_23

tag中包含的字符串不能编辑,但是可以被替换成其它的字符串,用 replace_with() 方法:

tag.string.replace_with(“No longer bold”)
tag
# <blockquote>No longer bold</blockquote>

BeautifulSoup

BeautifulSoup 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 Tag 对象,是一个特殊的 Tag,我们可以分别获取它的类型,名称,以及属性。

Python爬虫学习笔记之爬虫基础库_子节点_24

print type(soup.name)
#<type ‘unicode’>
print soup.name
# [document]
print soup.attrs
#{} 空字典

Python爬虫学习笔记之爬虫基础库_爬虫库_25

Comment

Python爬虫学习笔记之爬虫基础库_子节点_26

html_doc='<a href=”http://example.com/elsie” class=”sister” id=”link1″><!– Elsie –></a>’

soup = BeautifulSoup(html_doc, ‘html.parser’)

print(soup.a.string) # Elsie
print(type(soup.a.string)) # <class ‘bs4.element.Comment’>

Python爬虫学习笔记之爬虫基础库_爬虫库_27

a 标签里的内容实际上是注释,但是如果我们利用 .string 来输出它的内容,我们发现它已经把注释符号去掉了,所以这可能会给我们带来不必要的麻烦。

另外我们打印输出下它的类型,发现它是一个 Comment 类型,所以,我们在使用前最好做一下判断,判断代码如下:

if type(soup.a.string)==bs4.element.Comment:
print soup.a.string

上面的代码中,我们首先判断了它的类型,是否为 Comment 类型,然后再进行其他操作,如打印输出。

​​回到顶部​​

beautifulsoup的遍历文档树

还拿”爱丽丝梦游仙境”的文档来做例子:

Python爬虫学习笔记之爬虫基础库_字符串_28

Python爬虫学习笔记之爬虫基础库_子节点_29

html_doc = “””
<html><head><title>The Dormouse’s story</title></head>
<body>
<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 bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, ‘html.parser’)

Python爬虫学习笔记之爬虫基础库_html_30

Python爬虫学习笔记之爬虫基础库_html_31

通过这段例子来演示怎样从文档的一段内容找到另一段内容

(1) 子节点

一个Tag可能包含多个字符串或其它的Tag,这些都是这个Tag的子节点.Beautiful Soup提供了许多操作和遍历子节点的属性.

注意: Beautiful Soup中字符串节点不支持这些属性,因为字符串没有子节点。

.contents 和 .children

tag的 ​​.contents​​ 属性可以将tag的子节点以列表的方式输出:

Python爬虫学习笔记之爬虫基础库_字符串_32

Python爬虫学习笔记之爬虫基础库_html_33

head_tag = soup.head
head_tag
# <head><title>The Dormouse’s story</title></head>

head_tag.contents
[<title>The Dormouse’s story</title>]

title_tag = head_tag.contents[0]
title_tag
# <title>The Dormouse’s story</title>
title_tag.contents
# [u’The Dormouse’s story’]

Python爬虫学习笔记之爬虫基础库_子节点_34

Python爬虫学习笔记之爬虫基础库_子节点_35

字符串没有 ​​.contents​​ 属性,因为字符串没有子节点:

text = title_tag.contents[0]
text.contents
# AttributeError: ‘NavigableString’ object has no attribute ‘contents’

.children它返回的不是一个 list,不过我们可以通过遍历获取所有子节点。我们打印输出 .children 看一下,可以发现它是一个 list 生成器对象

通过tag的 ​​.children​​ 生成器,可以对tag的子节点进行循环:

Python爬虫学习笔记之爬虫基础库_字符串_36

Python爬虫学习笔记之爬虫基础库_子节点_37

print(title_tag.children) # <list_iterator object at 0x101b78860>
print(type(title_tag.children)) # <class ‘list_iterator’>


for child in title_tag.children:
print(child)
# The Dormouse’s story

Python爬虫学习笔记之爬虫基础库_爬虫库_38

Python爬虫学习笔记之爬虫基础库_爬虫库_39

.descendants

​​.contents​​ 和 ​​.children​​ 属性仅包含tag的直接子节点.例如,<head>标签只有一个直接子节点<title>

head_tag.contents
# [<title>The Dormouse’s story</title>]

但是<title>标签也包含一个子节点:字符串 “The Dormouse’s story”,这种情况下字符串 “The Dormouse’s story”也属于<head>标签的子孙节点. 

​​.descendants​​ 属性可以对所有tag的子孙节点进行递归循环 。

for child in head_tag.descendants:
print(child)
# <title>The Dormouse’s story</title>
# The Dormouse’s story

上面的例子中, <head>标签只有一个子节点,但是有2个子孙节点:<head>节点和<head>的子节点, ​​BeautifulSoup​​ 有一个直接子节点(<html>节点),却有很多子孙节点:

len(list(soup.children))
# 1
len(list(soup.descendants))
# 25

(2) 节点内容

如果tag只有一个 NavigableString 类型子节点,那么这个tag可以使用 .string 得到子节点。如果一个tag仅有一个子节点,那么这个tag也可以使用 .string 方法,输出结果与当前唯一子节点的 .string 结果相同。

通俗点说就是:如果一个标签里面没有标签了,那么 .string 就会返回标签里面的内容。如果标签里面只有唯一的一个标签了,那么 .string 也会返回最里面的内容。例如:

print (soup.head.string)
#The Dormouse’s story
print (soup.title.string)
#The Dormouse’s story

如果tag包含了多个子节点,tag就无法确定,string 方法应该调用哪个子节点的内容, .string 的输出结果是 None

print (soup.html.string)
#None

(3) 多个内容

​​?​​

1

​​.strings  .stripped_strings 属性​​

.strings

获取多个内容,不过需要遍历获取,比如下面的例子:

Python爬虫学习笔记之爬虫基础库_子节点_40Python爬虫学习笔记之爬虫基础库_字符串_41

for string in soup.strings:
print(repr(string))


”’


“The Dormouse’s story”




“The Dormouse’s story”


‘Once upon a time there were three little sisters; and their names were

‘Elsie’
‘,

‘Lacie’
‘ and

‘Tillie’
‘;
and they lived at the bottom of a well.’


‘…’

”’

View Code

.stripped_strings 

输出的字符串中可能包含了很多空格或空行,使用 .stripped_strings 可以去除多余空白内容

Python爬虫学习笔记之爬虫基础库_字符串_42Python爬虫学习笔记之爬虫基础库_html_43

for string in soup.stripped_strings:
print(repr(string))


”’

“The Dormouse’s story”
“The Dormouse’s story”
‘Once upon a time there were three little sisters; and their names were’
‘Elsie’
‘,’
‘Lacie’
‘and’
‘Tillie’
‘;
and they lived at the bottom of a well.’
‘…’

”’

View Code

(4) 父节点

继续分析文档树,每个tag或字符串都有父节点:被包含在某个tag中

.parent

通过 ​​.parent​​ 属性来获取某个元素的父节点.在例子“爱丽丝”的文档中,<head>标签是<title>标签的父节点:

title_tag = soup.title
title_tag
# <title>The Dormouse’s story</title>
title_tag.parent
# <head><title>The Dormouse’s story</title></head>

文档的顶层节点比如<html>的父节点是 ​​BeautifulSoup​​ 对象:

html_tag = soup.html
type(html_tag.parent)
# <class ‘bs4.BeautifulSoup’>

.parents

通过元素的 ​​.parents​​ 属性可以递归得到元素的所有父辈节点,下面的例子使用了 ​​.parents​​ 方法遍历了<a>标签到根节点的所有节点.

Python爬虫学习笔记之爬虫基础库_字符串_44

Python爬虫学习笔记之爬虫基础库_子节点_45

link = soup.a
link
# <a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>
for parent in link.parents:
if parent is None:
print(parent)
else:
print(parent.name)
# p
# body
# html
# [document]
# None

Python爬虫学习笔记之爬虫基础库_字符串_46

Python爬虫学习笔记之爬虫基础库_html_47

(5) 兄弟节点

sibling_soup = BeautifulSoup(“<a><b>text1</b><c>text2</c></b></a>”)

.next_sibling 和 .previous_sibling

兄弟节点可以理解为和本节点处在统一级的节点,.next_sibling 属性获取了该节点的下一个兄弟节点,.previous_sibling 则与之相反,如果节点不存在,则返回 None

在文档树中,使用 ​​.next_sibling​​ 和 ​​.previous_sibling​​ 属性来查询兄弟节点:

sibling_soup.b.next_sibling
# <c>text2</c>

sibling_soup.c.previous_sibling
# <b>text1</b>

注意:实际文档中的tag的 .next_sibling 和 .previous_sibling 属性通常是字符串或空白,因为空白或者换行也可以被视作一个节点,所以得到的结果可能是空白或者换行

实际文档中的tag的 ​​.next_sibling​​ 和 ​​.previous_sibling​​ 属性通常是字符串或空白. 看看“爱丽丝”文档:

<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>

如果以为第一个<a>标签的 ​​.next_sibling​​ 结果是第二个<a>标签,那就错了,真实结果是第一个<a>标签和第二个<a>标签之间的顿号和换行符:

Python爬虫学习笔记之爬虫基础库_子节点_48

link = soup.a
link
# <a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>

link.next_sibling
# u’,

Python爬虫学习笔记之爬虫基础库_字符串_49

第二个<a>标签是顿号的 ​​.next_sibling​​ 属性:

link.next_sibling.next_sibling
# <a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>

全部兄弟节点

.next_siblings  .previous_siblings 属性

通过 .next_siblings 和 .previous_siblings 属性可以对当前节点的兄弟节点迭代输出

Python爬虫学习笔记之爬虫基础库_字符串_50

Python爬虫学习笔记之爬虫基础库_子节点_51

for sibling in soup.a.next_siblings:
print(repr(sibling))

”’
‘,

<a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>
‘ and

<a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>
‘;
and they lived at the bottom of a well.’

”’

Python爬虫学习笔记之爬虫基础库_字符串_52

Python爬虫学习笔记之爬虫基础库_html_53

前后节点

.next_element  .previous_element 属性

与 .next_sibling  .previous_sibling 不同,它并不是针对于兄弟节点,而是在所有节点,不分层次

比如 head 节点为

​​?​​

1

​​<​​​​head​​​​><​​​​title​​​​>The Dormouse’s story</​​​​title​​​​></​​​​head​​​​>​​

那么它的下一个节点便是 title,它是不分层次关系的

print(soup.head.next_element)
#<title>The Dormouse’s story</title>

 

所有前后节点

.next_elements  .previous_elements 属性

通过 .next_elements 和 .previous_elements 的迭代器就可以向前或向后访问文档的解析内容,就好像文档正在被解析一样

Python爬虫学习笔记之爬虫基础库_字符串_54

Python爬虫学习笔记之爬虫基础库_字符串_55

for i in soup.a.next_elements:
print(repr(i))

”’
‘Elsie’
‘,

<a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>
‘Lacie’
‘ and

<a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>
‘Tillie’
‘;
and they lived at the bottom of a well.’


<p class=”story”>…</p>
‘…’


”’

Python爬虫学习笔记之爬虫基础库_爬虫库_56

Python爬虫学习笔记之爬虫基础库_子节点_57

以上是遍历文档树的基本用法。

​​回到顶部​​

beautifulsoup的搜索文档树

find_all

​​?​​

1

​​find_all( name , attrs , recursive , string , **kwargs )​​

​​find_all()​​ 方法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件:

Python爬虫学习笔记之爬虫基础库_html_58

Python爬虫学习笔记之爬虫基础库_爬虫库_59

soup.find_all(“title”)
# [<title>The Dormouse’s story</title>]

soup.find_all(“p”, “title”)
# [<p class=”title”><b>The Dormouse’s story</b></p>]

soup.find_all(“a”)
# [<a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>,
# <a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>,
# <a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>]

soup.find_all(id=”link2″)
# [<a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>]

import re
soup.find(string=re.compile(“sisters”))
# u’Once upon a time there were three little sisters; and their names were

Python爬虫学习笔记之爬虫基础库_字符串_60

Python爬虫学习笔记之爬虫基础库_子节点_61

有几个方法很相似,还有几个方法是新的,参数中的 ​​string​​ 和 ​​id​​ 是什么含义? 为什么 ​​find_all(“p”, “title”)​​ 返回的是CSS Class为”title”的<p>标签? 我们来仔细看一下 ​​find_all()​​ 的参数.

name 参数

​​name​​ 参数可以查找所有名字为 ​​name​​ 的tag,字符串对象会被自动忽略掉.

简单的用法如下:

soup.find_all(“title”)
# [<title>The Dormouse’s story</title>]

搜索 ​​name​​ 参数的值可以使任一类型的 过滤器 ,字符窜,正则表达式,列表,方法或是 ​​True​​ .

<1> 传字符串

最简单的过滤器是字符串.在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配的内容,下面的例子用于查找文档中所有的<b>标签

soup.find_all(‘b’)
# [<b>The Dormouse’s story</b>]

<2> 传正则表达式

如果传入正则表达式作为参数,Beautiful Soup会通过正则表达式的 match() 来匹配内容.下面例子中找出所有以b开头的标签,这表示<body>和<b>标签都应该被找到

import re
for tag in soup.find_all(re.compile(“^b”)):
print(tag.name)
# body
# b

<3> 传列表

如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有<a>标签和<b>标签

soup.find_all([“a”, “b”])
# [<b>The Dormouse’s story</b>,
# <a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>,
# <a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>,
# <a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>]

<4> 传 True

True 可以匹配任何值,下面代码查找到所有的tag,但是不会返回字符串节点

Python爬虫学习笔记之爬虫基础库_html_62

Python爬虫学习笔记之爬虫基础库_子节点_63

for tag in soup.find_all(True):
print(tag.name)

”’
html
head
title
body
p
b
p
a
a
a
p

”’

Python爬虫学习笔记之爬虫基础库_字符串_64

Python爬虫学习笔记之爬虫基础库_html_65

<5> 传方法

如果没有合适过滤器,那么还可以定义一个方法,方法只接受一个元素参数,如果这个方法返回 True 表示当前元素匹配并且被找到,如果不是则反回 False

下面方法校验了当前元素,如果包含 class 属性却不包含 id 属性,那么将返回 True:

def has_class_but_no_id(tag):
return tag.has_attr(‘class’) and not tag.has_attr(‘id’)

将这个方法作为参数传入 find_all() 方法,将得到所有<p>标签:

Python爬虫学习笔记之爬虫基础库_子节点_66

Python爬虫学习笔记之爬虫基础库_html_67

print(soup.find_all(has_class_but_no_id))


”’
[
<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 class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>,
<a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a> and
<a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>;
and they lived at the bottom of a well.
</p>,
<p class=”story”>…</p>
]
”’

Python爬虫学习笔记之爬虫基础库_html_68

Python爬虫学习笔记之爬虫基础库_字符串_69

keyword 参数

如果一个指定名字的参数不是搜索内置的参数名,搜索时会把该参数当作指定名字tag的属性来搜索,如果包含一个名字为 ​​id​​ 的参数,Beautiful Soup会搜索每个tag的”id”属性.

Python爬虫学习笔记之爬虫基础库_子节点_70

soup.find_all(id=’link2′)
# [<a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>]

import re
print(soup.find_all(href=re.compile(“elsie”)))
# [<a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>]

Python爬虫学习笔记之爬虫基础库_html_71

搜索指定名字的属性时可以使用的参数值包括 字符串 , 正则表达式 , 列表, True .

下面的例子在文档树中查找所有包含 ​​id​​ 属性的tag,无论 ​​id​​ 的值是什么:

soup.find_all(id=True)
# [<a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>,
# <a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>,
# <a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>]

使用多个指定名字的参数可以同时过滤tag的多个属性:

soup.find_all(href=re.compile(“elsie”), id=’link1′)
# [<a class=”sister” href=”http://example.com/elsie” id=”link1″>three</a>]

在这里我们想用 class 过滤,不过 class 是 python 的关键词,这怎么办?加个下划线就可以

Python爬虫学习笔记之爬虫基础库_子节点_72

Python爬虫学习笔记之爬虫基础库_子节点_73

print(soup.find_all(“a”, class_=”sister”))

”’
[<a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>,
<a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>,
<a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>
]

”’

Python爬虫学习笔记之爬虫基础库_html_74

Python爬虫学习笔记之爬虫基础库_html_75

通过 ​​find_all()​​ 方法的 ​​attrs​​ 参数定义一个字典参数来搜索包含特殊属性的tag:

data_soup.find_all(attrs={“data-foo”: “value”})
# [<div data-foo=”value”>foo!</div>]

text 参数

通过 text 参数可以搜搜文档中的字符串内容.与 name 参数的可选值一样, text 参数接受 字符串 , 正则表达式 , 列表, True

Python爬虫学习笔记之爬虫基础库_子节点_76

Python爬虫学习笔记之爬虫基础库_子节点_77

import re

print(soup.find_all(text=”Elsie”))
# [‘Elsie’]

print(soup.find_all(text=[“Tillie”, “Elsie”, “Lacie”]))
# [‘Elsie’, ‘Lacie’, ‘Tillie’]

print(soup.find_all(text=re.compile(“Dormouse”)))
# [“The Dormouse’s story”, “The Dormouse’s story”]

Python爬虫学习笔记之爬虫基础库_html_78

Python爬虫学习笔记之爬虫基础库_爬虫库_79

limit 参数

find_all() 方法返回全部的搜索结构,如果文档树很大那么搜索会很慢.如果我们不需要全部结果,可以使用 limit 参数限制返回结果的数量.效果与SQL中的limit关键字类似,当搜索到的结果数量达到 limit 的限制时,就停止搜索返回结果.

Python爬虫学习笔记之爬虫基础库_子节点_80

print(soup.find_all(“a”,limit=2))

”’
[<a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>,
<a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>]
”’

Python爬虫学习笔记之爬虫基础库_爬虫库_81

recursive 参数

调用tag的 find_all() 方法时,Beautiful Soup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False .

print(soup.html.find_all(“title”)) # [<title>The Dormouse’s story</title>]
print(soup.html.find_all(“title”,recursive=False)) # []

find()

​​?​​

1

​​find( name , attrs , recursive , string , **kwargs )​​

​​find_all()​​ 方法将返回文档中符合条件的所有tag,尽管有时候我们只想得到一个结果.比如文档中只有一个<body>标签,那么使用 ​​find_all()​​ 方法来查找<body>标签就不太合适, 使用 ​​find_all​​ 方法并设置 ​​limit=1​​ 参数不如直接使用 ​​find()​​ 方法.下面两行代码是等价的:

soup.find_all(‘title’, limit=1)
# [<title>The Dormouse’s story</title>]

soup.find(‘title’)
# <title>The Dormouse’s story</title>

唯一的区别是 ​​find_all()​​ 方法的返回结果是值包含一个元素的列表,而 ​​find()​​ 方法直接返回结果.​​find_all()​​ 方法没有找到目标是返回空列表, ​​find()​​ 方法找不到目标时,返回 ​​None​​ .

print(soup.find(“nosuchtag”))
# None

​​soup.head.title​​ 是 tag的名字 方法的简写.这个简写的原理就是多次调用当前tag的 ​​find()​​ 方法:

soup.head.title
# <title>The Dormouse’s story</title>

soup.find(“head”).find(“title”)
# <title>The Dormouse’s story</title>

find_parents() 和 find_parent()

Python爬虫学习笔记之爬虫基础库_爬虫库_82

Python爬虫学习笔记之爬虫基础库_爬虫库_83

a_string = soup.find(string=”Lacie”)
print(a_string) # Lacie

print(a_string.find_parent())
# <a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>
print(a_string.find_parents())
print(a_string.find_parent(“p”))
”’
<p class=”story”>
Once upon a time there were three little sisters; and their names were
<a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>,
<a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a> and
<a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>;
and they lived at the bottom of a well.
</p>

”’

Python爬虫学习笔记之爬虫基础库_爬虫库_84

Python爬虫学习笔记之爬虫基础库_html_85

find_next_siblings() 和 find_next_sibling()

find_next_siblings( name , attrs , recursive , string , **kwargs )

find_next_sibling( name , attrs , recursive , string , **kwargs )

这2个方法通过 .next_siblings 属性对当tag的所有后面解析的兄弟tag节点进行迭代, ​​find_next_siblings()​​ 方法返回所有符合条件的后面的兄弟节点, ​​find_next_sibling()​​ 只返回符合条件的后面的第一个tag节点.

Python爬虫学习笔记之爬虫基础库_子节点_86

Python爬虫学习笔记之爬虫基础库_字符串_87

first_link = soup.a

print(first_link.find_next_sibling(“a”))
# <a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>

print(first_link.find_next_siblings(“a”))
”’
[<a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>,
<a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>
]
”’

Python爬虫学习笔记之爬虫基础库_爬虫库_88

Python爬虫学习笔记之爬虫基础库_html_89

find_previous_siblings() 和 find_previous_sibling()的使用类似于find_next_sibling和find_next_siblings。

find_all_next() 和 find_next()

find_all_next( name , attrs , recursive , string , **kwargs )

find_next( name , attrs , recursive , string , **kwargs )

这2个方法通过 .next_elements 属性对当前tag的之后的tag和字符串进行迭代, ​​find_all_next()​​ 方法返回所有符合条件的节点, ​​find_next()​​ 方法返回第一个符合条件的节点:  

first_link = soup.a
print(first_link.find_all_next(string=True))
# [‘Elsie’, ‘,
‘, ‘Lacie’, ‘ and
‘, ‘Tillie’, ‘;
and they lived at the bottom of a well.’, ‘
‘, ‘…’, ‘
‘]
print(first_link.find_next(string=True)) # Elsie

find_all_previous() 和 find_previous()的使用类似于find_all_next() 和 find_next()。

​​回到顶部​​

beautifulsoup的css选择器

我们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list

(1)通过标签名查找

print(soup.select(“title”)) #[<title>The Dormouse’s story</title>]
print(soup.select(“b”)) #[<b>The Dormouse’s story</b>]

(2)通过类名查找

Python爬虫学习笔记之爬虫基础库_html_90

Python爬虫学习笔记之爬虫基础库_爬虫库_91

print(soup.select(“.sister”))

”’
[<a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>,
<a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>,
<a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>]

”’

Python爬虫学习笔记之爬虫基础库_字符串_92

Python爬虫学习笔记之爬虫基础库_爬虫库_93

(3)通过 id 名查找

print(soup.select(“#link1”))
# [<a class=”sister” href=”http://example.com/elsie” id=”link1″>Elsie</a>]

(4)组合查找

组合查找即和写 class 文件时,标签名与类名、id名进行的组合原理是一样的,例如查找 p 标签中,id 等于 link1的内容,二者需要用空格分开

print(soup.select(“p #link2”))

#[<a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>]

直接子标签查找

print(soup.select(“p > #link2”))
# [<a class=”sister” href=”http://example.com/lacie” id=”link2″>Lacie</a>]

(5)属性查找

查找时还可以加入属性元素,属性需要用中括号括起来,注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。

print(soup.select(“a[href=’http://example.com/tillie’]”))
#[<a class=”sister” href=”http://example.com/tillie” id=”link3″>Tillie</a>]

select 方法返回的结果都是列表形式,可以遍历形式输出,然后用 get_text() 方法来获取它的内容:

Python爬虫学习笔记之爬虫基础库_字符串_94

Python爬虫学习笔记之爬虫基础库_字符串_95

for title in soup.select(‘a’):
print (title.get_text())

”’
Elsie
Lacie
Tillie
”’

Python爬虫学习笔记之爬虫基础库_html_96

Python爬虫学习笔记之爬虫基础库_字符串_97

豆瓣网改写

Python爬虫学习笔记之爬虫基础库_子节点_98

Python爬虫学习笔记之爬虫基础库_字符串_99

from bs4 import BeautifulSoup
soup = BeautifulSoup(s, ‘html.parser’)

s=soup.find_all(class_=”item”)
for item in s:
print(item.find(class_=”pic”).a.get(“href”))
print(item.find(class_=”pic”).em.string)
print(item.find(class_=”info”).contents[1].a.span.string)
print(item.find(class_=”info”).contents[3].contents[3].contents[3].string)
print(item.find(class_=”info”).contents[3].contents[3].contents[7].string)

Python爬虫学习笔记之爬虫基础库_爬虫库_100

Python爬虫学习笔记之爬虫基础库_爬虫库_101

总结

本篇内容比较多,把 Beautiful Soup 的方法进行了大部分整理和总结,不过这还不算完全,仍然有 Beautiful Soup 的修改删除功能,不过这些功能用得比较少,只整理了查找提取的方法,希望对大家有帮助!

 

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python教程2022-11-01发表,共计19701字。
新手QQ群:570568346,欢迎进群讨论 Python51学习