beautifulSoup基本用法及find選擇器


   總結來源於官方文檔:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-all

 

示例代碼段

復制代碼
html_doc = """
<html>
<head><title>The Dormouse's story <!--Hey, buddy. Want to buy a used parser?-->
<a><!--Hey, buddy. Want to buy a used parser?--></a></title>
</head>
<body>
<p class="title">
<b>The Dormouse's story</b>
<a><!--Hey, buddy. Want to buy a used parser?--></a>
</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 link4">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>
"""
復制代碼

 

  1、快速操作:

復制代碼
soup.title  == soup.find('title')
# <title>The Dormouse's story</title>

soup.title.name
# u'title'

soup.title.string  == soup.title.text  == soup.title.get_text()
# u'The Dormouse's story'

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

soup.p   == soup.find('p')  # . 點屬性,只能獲取當前標簽下的第一個標簽
# <p class="title"><b>The Dormouse's story</b></p>

soup.p['class']
# u'title'

soup.a  == soup.find('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_all(['a','b']) # 查找所有的a標簽和b標簽
soup.find_all(id=["link1","link2"]) # 查找所有id=link1 和id=link2的標簽
soup.find(id
="link3") # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>


復制代碼

  2、Beautiful Soup對象有四種類型:

    1、BeautifulSoup

    2、tag:標簽

    3、NavigableString  : 標簽中的文本,可包含注釋內容

    4、Comment :標簽中的注釋,純注釋,沒有正文內容

 

  標簽屬性的操做跟字典是一樣一樣的

  html多值屬性(xml不適合):

    意思為一個屬性名稱,它是多值的,即包含多個屬性值,即使屬性中只有一個值也返回值為list,

    如:class,rel , rev , accept-charset , headers , accesskey

    其它屬性為單值屬性,即使屬性值中有多個空格隔開的值,也是反回一個字符串

soup.a['class']  #['sister']


id_soup = BeautifulSoup('<p id="my id"></p>')
id_soup.p['id']  #'my id'

 

  3、html中tag內容輸出: 

    string:輸出單一子標簽文本內容或注釋內容(選其一,標簽中包含兩種內容則輸出為None)

    strings: 返回所有子孫標簽的文本內容的生成器(不包含注釋)

    stripped_strings:返回所有子孫標簽的文本內容的生成器(不包含注釋,並且在去掉了strings中的空行和空格)

    text:只輸出文本內容,可同時輸出多個子標簽內容

    get_text():只輸出文本內容,可同時輸出多個子標簽內容

  string:

markup = "<b><!--Hey, buddy. Want to buy a used parser?--></b>"
soup = BeautifulSoup(markup, 'html.parser')
comm = soup.b.string
print(comm)  # Hey, buddy. Want to buy a used parser?
print(type(comm))  #<class 'bs4.element.Comment'>

   strings:

復制代碼
head_tag = soup.body
for s in head_tag.strings:
    print(repr(s))

結果:
'\n'
"The Dormouse's story"
'\n'
'Once upon a time there were three little sisters; and their names were\n        '
'Elsie'
',\n        '
'Lacie'
' and\n        '
'Tillie'
';\n        and they lived at the bottom of a well.\n    '
'\n'
'...'
'\n'
復制代碼

  stripped_strings:

復制代碼
head_tag = soup.body
for s in head_tag.stripped_strings:
    print(repr(s))

結果:
"The Dormouse's story"
'Once upon a time there were three little sisters; and their names were'
'Elsie'
','
'Lacie'
'and'
'Tillie'
';\n        and they lived at the bottom of a well.'
'...'
復制代碼

  text:

復制代碼
soup = BeautifulSoup(html_doc, 'html.parser')
head_tag = soup.body
print(head_tag.text)

結果:
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.
    
...
復制代碼
復制代碼
soup = BeautifulSoup(html_doc, 'html.parser')
head_tag = soup.body
print(repr(head_tag.text))

結果:
"\nThe Dormouse's story\nOnce upon a time there were three little sisters; and their names were\n        Elsie,\n        Lacie and\n        Tillie;\n        and they lived at the bottom of a well.\n    \n...\n"
復制代碼

 

 

  4、返回子節點列表:

    .contents: 以列表的方式返回節點下的直接子節點

    .children:以生成器的方式反回節點下的直接子節點

復制代碼
soup = BeautifulSoup(html_doc, 'html.parser')
head_tag = soup.head
print(head_tag)
print(head_tag.contents)
print(head_tag.contents[0])
print(head_tag.contents[0].contents)

for ch in head_tag.children:
    print(ch)

結果:
<head><title>The Dormouse's story</title></head>
[<title>The Dormouse's story</title>]
<title>The Dormouse's story</title>
["The Dormouse's story"]
<title>The Dormouse's story</title>
復制代碼

 

  5、返回子孫節點的生成器:

     .descendants: 以列表的方式返回標簽下的子孫節點

復制代碼
for ch in head_tag.descendants:
    print(ch)

結果:
<title>The Dormouse's story</title>
The Dormouse's story
復制代碼

 

  6、父標簽(parent):如果是bs4對象,不管本來是標簽還是文本都可以找到其父標簽,但是文本對象不能找到父標簽

復制代碼
soup = BeautifulSoup(html_doc, 'html.parser')
tag_title = soup.b  # b標簽
print(tag_title.parent)  # b標簽的父標簽 p
print(type(tag_title.string))  # b標簽中的文本的類型,文本中有注釋時結果為None <class 'bs4.element.NavigableString'>
print(tag_title.string.parent)  # b標簽中文本的父標簽 b
print(type(tag_title.text))  # b 標簽中的文本類型為str,無bs4屬性找到父標簽
復制代碼

 

  7、遞歸父標簽(parents):遞歸得到元素的所有父輩節點

復制代碼
soup = BeautifulSoup(html_doc, 'html.parser')
link = soup.a
for parent in link.parents:
    print(parent.name)

結果:

p
body
html
[document]

復制代碼

 

  8、前后節點查詢(不是前后標簽哦,文本也是節點之一):previous_sibling,next_sibling

 

   9、以生成器的方式迭代返回所有兄弟節點

復制代碼
for sib in soup.a.next_siblings:
    print(sib)
    print("---------")

結果:
-------------
,
        
---------
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
---------


---------
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
---------
;
        and they lived at the bottom of a well.
    
---------
復制代碼

 

  10、搜索文檔樹

    過濾器:

      1、字符串

      2、正則表達式

      3、列表

      4、True

      5、方法

復制代碼
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</p>
<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 class="story">...</p>
</body>
"""
from bs4 import BeautifulSoup
import re
soup = BeautifulSoup(html_doc, 'html.parser')
soup.find_all("a")  # 字符串參數
soup.find_all(re.compile("^b"))  # 正則參數
soup.find_all(re.compile("a"))  # 正則參數
soup.find_all(re.compile("l$"))  # 正則參數
soup.find_all(["a", "b"])  # 標簽的列表參數
soup.find_all(True)  # 返回所有標簽
def has_class_no_id(tag):
    return tag.has_attr("class") and not tag.has_attr("id")
soup.find_all(has_class_no_id)  # 方法參數
復制代碼

 

  11、find選擇器:

    語法 :

    # find_all( name , attrs , recursive , text , **kwargs )
    #  name :要查找的標簽名
    #  attrs: 標簽的屬性
    #  recursive: 遞歸
    #  text: 查找文本
    # **kwargs :其它 鍵值參數

  特殊情況:
    data-foo="value",因中橫杠不識別的原因,只能寫成attrs={"data-foo":"value"},
    class="value",因class是關鍵字,所以要寫成class_="value"或attrs={"class":"value"}
復制代碼
from bs4 import BeautifulSoup
import re
html_doc = """
<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>
"""

# find_all( name , attrs , recursive , text , **kwargs )
#  name :要查找的標簽名(字符串、正則、方法、True)
#  attrs: 標簽的屬性
#  recursive: 遞歸
#  text: 查找文本
# **kwargs :其它 鍵值參數
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.find_all('p', 'title')) # p標簽且class="title"
soup.find_all('title')  # 以列表形式返回 所有title標簽a
soup.find_all(attrs={"class":"sister"})  # 以列表形式返回 所有class屬性==sister的標簽
soup.find_all(id='link2')  # 返回所有id屬性==link2的標簽
soup.find_all(href=re.compile("elsie")) # 返回所有href屬性包含elsie的標簽
soup.find_all(id=True)  # 返回 所有包含id屬性的標簽
soup.find_all(id="link1", href=re.compile('elsie'))  #  id=link1且href包含elsie
復制代碼

關於class的搜索
復制代碼
soup = BeautifulSoup(html_doc, 'html.parser')
css_soup = BeautifulSoup('<p class="body strikeout"></p>', 'html.parser')
css_soup.find_all("p", class_="body")  # 多值class,指定其中一個即可
css_soup.find_all("p", class_="strikeout")
css_soup.find_all("p", class_="body strikeout")  # 精確匹配
# text 參數可以是字符串,列表、方法、True
soup.find_all("a", text="Elsie")  # text="Elsie"的a標簽
復制代碼

 

  12、父節點方法:

    find_parents( name , attrs , recursive , text , **kwargs )

    find_parent( name , attrs , recursive , text , **kwargs )

復制代碼
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</p>
    <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
    <p>
        <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    </p>
    <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    and they lived at the bottom of a well.
    <p class="story">...</p>
</body>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
a_string = soup.find(text="Lacie")  # 文本為Lacie的節點
type(a_string), a_string  # <class 'bs4.element.NavigableString'> Lacie
a_parent = a_string.find_parent()  # a_string的父節點中的第一個節點
a_parent = a_string.find_parent("p")  # a_string的父節點中的第一個p節點
a_parents = a_string.find_parents()  # a_string的父節點
a_parents = a_string.find_parents("a")  # a_string的父點中所有a節點
復制代碼

 

  13、后面的鄰居節點:

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

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

復制代碼
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</p>
    <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
    <b href="http://example.com/elsie" class="sister" id="link1">Elsie</b>,
    <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 class="story">...</p>
</body>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
first_link = soup.a  # 第一個a標簽
a_sibling = first_link.find_next_sibling()  # 后面鄰居的第一個
a_sibling = first_link.find_next_sibling("a")  # 后面鄰居的第一個a
a_siblings = first_link.find_next_siblings()  # 后面的所有鄰居
a_siblings = first_link.find_next_siblings("a")  # 后面鄰居的所有a鄰居
復制代碼

 

   14、前面的鄰居節點:

    find_previous_siblings( name , attrs , recursive , text , **kwargs )

    find_previous_sibling( name , attrs , recursive , text , **kwargs )

 

  15、后面的節點:

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

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

復制代碼
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</p>
    <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
    <p>
        <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    </p>
    <p>
        <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    </p>
        and they lived at the bottom of a well.
    <p class="story">...</p>
</body>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
a_string = soup.find(text="Lacie")
a_next = a_string.find_next()  # 后面所有子孫標簽的第一個
a_next = a_string.find_next('a')  # 后面所有子孫標簽的第一個a標簽
a_nexts = a_string.find_all_next()  # 后面的所有子孫標簽
a_nexts = a_string.find_all_next('a')  # 后面的所有子孫標簽中的所有a標簽
復制代碼

 

   16、前面的節點:

    find_all_previous( name , attrs , recursive , text , **kwargs )

    find_previous( name , attrs , recursive , text , **kwargs )

 

  17、解析部分文檔:

    如果僅僅因為想要查找文檔中的<a>標簽而將整片文檔進行解析,實在是浪費內存和時間.最快的方法是從一開始就把<a>標簽以外的東西都忽略掉. SoupStrainer 類可以定義文檔的某段內容,這樣搜索文檔時就不必先解析整篇文檔,只會解析在 SoupStrainer 中定義過的文檔. 創建一個 SoupStrainer 對象並作為 parse_only 參數給 BeautifulSoup 的構造方法即可。

  SoupStrainer 類參數:name , attrs , recursive , text , **kwargs

復制代碼
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>;
    </p>
        and they lived at the bottom of a well.
    <p class="story">...</p>
</body>
"""
from bs4 import SoupStrainer
a_tags = SoupStrainer('a')  # 所有a標簽
id_tags = SoupStrainer(id="link2")  # id=link2的標簽
def is_short_string(string):
    return len(string) < 10  # string長度小於10,返回True
short_string = SoupStrainer(text=is_short_string)  # 符合條件的文本

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser', parse_only=a_tags).prettify()
soup = BeautifulSoup(html_doc, 'html.parser', parse_only=id_tags).prettify()
soup = BeautifulSoup(html_doc, 'html.parser', parse_only=short_string).prettify()
復制代碼

<div id="cnblogs_post_body" class="blogpost-body"><p>&nbsp;</p><p>  總結來源於官方文檔:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-all</p><p>&nbsp;</p><p>示例代碼段</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>html_doc = """<br>&lt;html&gt;<br>    &lt;head&gt;&lt;title&gt;The Dormouse's story &lt;!--Hey, buddy. Want to buy a used parser?--&gt;<br>    &lt;a&gt;&lt;!--Hey, buddy. Want to buy a used parser?--&gt;&lt;/a&gt;&lt;/title&gt;<br>    &lt;/head&gt;<br>&lt;body&gt;<br>    &lt;p class="title"&gt;<br>        &lt;b&gt;The Dormouse's story&lt;/b&gt;<br>        &lt;a&gt;&lt;!--Hey, buddy. Want to buy a used parser?--&gt;&lt;/a&gt;<br>    &lt;/p&gt;<br>    &lt;p class="story"&gt;Once upon a time there were three little sisters; and their names were<br>        &lt;a href="http://example.com/elsie" class="sister" id="link1 link4"&gt;Elsie&lt;/a&gt;,<br>        &lt;a href="http://example.com/lacie" class="sister" id="link2"&gt;Lacie&lt;/a&gt; and<br>        &lt;a href="http://example.com/tillie" class="sister" id="link3"&gt;Tillie&lt;/a&gt;;<br>        and they lived at the bottom of a well.<br>    &lt;/p&gt;<br>    &lt;p class="story"&gt;...&lt;/p&gt;<br>"""</pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>&nbsp;</p><p>  1、快速操作:</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>soup.title  == soup.find(<span style="color: #800000">'</span><span style="color: #800000">title</span><span style="color: #800000">'</span><span style="color: #000000">)# </span>&lt;title&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/title&gt;</span><span style="color: #000000">soup.title.name# u</span><span style="color: #800000">'</span><span style="color: #800000">title</span><span style="color: #800000">'</span><span style="color: #000000">
soup.title.</span><span style="color: #0000ff">string</span>  == soup.title.text  ==<span style="color: #000000"> soup.title.get_text()# u</span><span style="color: #800000">'</span><span style="color: #800000">The Dormouse</span><span style="color: #800000">'</span>s story<span style="color: #800000">'</span><span style="color: #000000">soup.title.parent.name# u</span><span style="color: #800000">'</span><span style="color: #800000">head</span><span style="color: #800000">'</span><span style="color: #000000">
soup.p   </span>== soup.find(<span style="color: #800000">'</span><span style="color: #800000">p</span><span style="color: #800000">'</span><span style="color: #000000">)  # . 點屬性,只能獲取當前標簽下的第一個標簽# </span>&lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">title</span><span style="color: #800000">"</span>&gt;&lt;b&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/b&gt;&lt;/p&gt;</span><span style="color: #000000">soup.p[</span><span style="color: #800000">'</span><span style="color: #800000">class</span><span style="color: #800000">'</span><span style="color: #000000">]# u</span><span style="color: #800000">'</span><span style="color: #800000">title</span><span style="color: #800000">'</span><span style="color: #000000">
soup.a  </span>== soup.find(<span style="color: #800000">'</span><span style="color: #800000">a</span><span style="color: #800000">'</span><span style="color: #000000">)# </span>&lt;a <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/elsie</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link1</span><span style="color: #800000">"</span>&gt;Elsie&lt;/a&gt;<span style="color: #000000">
soup.find_all(</span><span style="color: #800000">'</span><span style="color: #800000">a</span><span style="color: #800000">'</span><span style="color: #000000">)# [</span>&lt;a <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/elsie</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link1</span><span style="color: #800000">"</span>&gt;Elsie&lt;/a&gt;<span style="color: #000000">,#  </span>&lt;a <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/lacie</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link2</span><span style="color: #800000">"</span>&gt;Lacie&lt;/a&gt;<span style="color: #000000">,#  </span>&lt;a <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/tillie</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link3</span><span style="color: #800000">"</span>&gt;Tillie&lt;/a&gt;<span style="color: #000000">]<br>soup.find_all(['a','b'])  # 查找所有的a標簽和b標簽<br>soup.find_all(id=["link1","link2"])  # 查找所有id=link1 和id=link2的標簽<br>soup.find(id</span>=<span style="color: #800000">"</span><span style="color: #800000">link3</span><span style="color: #800000">"</span><span style="color: #000000">)# </span>&lt;a <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/tillie</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link3</span><span style="color: #800000">"</span>&gt;Tillie&lt;/a&gt;<br><br><br></pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>  2、Beautiful Soup對象有四種類型:</p><p>    1、BeautifulSoup</p><p>    2、tag:標簽</p><p>    3、NavigableString&nbsp; : 標簽中的文本,可包含注釋內容</p><p>    4、Comment :標簽中的注釋,純注釋,沒有正文內容</p><p>&nbsp;</p><p>  標簽屬性的操做跟字典是一樣一樣的</p><p>  html多值屬性(xml不適合):</p><p>    意思為一個屬性名稱,它是多值的,即包含多個屬性值,即使屬性中只有一個值也返回值為list,</p><p>    如:class,<tt class="docutils literal"><span class="pre">rel</span></tt>&nbsp;,&nbsp;<tt class="docutils literal"><span class="pre">rev</span></tt>&nbsp;,&nbsp;<tt class="docutils literal"><span class="pre">accept-charset</span></tt>&nbsp;,&nbsp;<tt class="docutils literal"><span class="pre">headers</span></tt>&nbsp;,&nbsp;<tt class="docutils literal"><span class="pre">accesskey</span></tt></p><p>    其它屬性為單值屬性,即使屬性值中有多個空格隔開的值,也是反回一個字符串</p><div class="cnblogs_code"><pre>soup.a[<span style="color: #800000">'</span><span style="color: #800000">class</span><span style="color: #800000">'</span>]  #[<span style="color: #800000">'</span><span style="color: #800000">sister</span><span style="color: #800000">'</span><span style="color: #000000">]

id_soup </span>= BeautifulSoup(<span style="color: #800000">'</span><span style="color: #800000">&lt;p id="my id"&gt;&lt;/p&gt;</span><span style="color: #800000">'</span><span style="color: #000000">)id_soup.p[</span><span style="color: #800000">'</span><span style="color: #800000">id</span><span style="color: #800000">'</span>]  #<span style="color: #800000">'</span><span style="color: #800000">my id</span><span style="color: #800000">'</span></pre></div><p>&nbsp;</p><p>  3、html中tag內容輸出: </p><p>    string:輸出單一子標簽文本內容或注釋內容(選其一,標簽中包含兩種內容則輸出為None)</p><p>    strings: 返回所有子孫標簽的文本內容的生成器(不包含注釋)</p><p>    stripped_strings:返回所有子孫標簽的文本內容的生成器(不包含注釋,並且在去掉了strings中的空行和空格)</p><p>    text:只輸出文本內容,可同時輸出多個子標簽內容</p><p>    get_text():只輸出文本內容,可同時輸出多個子標簽內容</p><p>  string:</p><div class="cnblogs_code"><pre>markup = <span style="color: #800000">"</span><span style="color: #800000">&lt;b&gt;&lt;!--Hey, buddy. Want to buy a used parser?--&gt;&lt;/b&gt;</span><span style="color: #800000">"</span><span style="color: #000000">soup </span>= BeautifulSoup(markup, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span><span style="color: #000000">)comm </span>= soup.b.<span style="color: #0000ff">string</span><span style="color: #000000">print(comm)  # Hey, buddy. Want to buy a used parser?print(type(comm))  #&lt;class 'bs4.element.Comment'&gt;</span></pre></div><p>&nbsp;  strings:</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>head_tag =<span style="color: #000000"> soup.body</span><span style="color: #0000ff">for</span> s <span style="color: #0000ff">in</span><span style="color: #000000"> head_tag.strings:    print(repr(s))
結果:</span><span style="color: #800000">'</span><span style="color: #800000">\n</span><span style="color: #800000">'</span><span style="color: #800000">"</span><span style="color: #800000">The Dormouse's story</span><span style="color: #800000">"</span><span style="color: #800000">'</span><span style="color: #800000">\n</span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">Once upon a time there were three little sisters; and their names were\n        </span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">Elsie</span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">,\n        </span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">Lacie</span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000"> and\n        </span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">Tillie</span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">;\n        and they lived at the bottom of a well.\n    </span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">\n</span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">...</span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">\n</span><span style="color: #800000">'</span></pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>  stripped_strings:</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>head_tag =<span style="color: #000000"> soup.body</span><span style="color: #0000ff">for</span> s <span style="color: #0000ff">in</span><span style="color: #000000"> head_tag.stripped_strings:    print(repr(s))
結果:</span><span style="color: #800000">"</span><span style="color: #800000">The Dormouse's story</span><span style="color: #800000">"</span><span style="color: #800000">'</span><span style="color: #800000">Once upon a time there were three little sisters; and their names were</span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">Elsie</span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">,</span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">Lacie</span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">and</span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">Tillie</span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">;\n        and they lived at the bottom of a well.</span><span style="color: #800000">'</span><span style="color: #800000">'</span><span style="color: #800000">...</span><span style="color: #800000">'</span></pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>  text:</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>soup = BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span><span style="color: #000000">)head_tag </span>=<span style="color: #000000"> soup.bodyprint(head_tag.text)
結果:The Dormouse</span><span style="color: #800000">'</span><span style="color: #800000">s story</span><span style="color: #000000">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.    ...</span></pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>soup = BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span><span style="color: #000000">)head_tag </span>=<span style="color: #000000"> soup.bodyprint(repr(head_tag.text))
結果:</span><span style="color: #800000">"</span><span style="color: #800000">\nThe Dormouse's story\nOnce upon a time there were three little sisters; and their names were\n        Elsie,\n        Lacie and\n        Tillie;\n        and they lived at the bottom of a well.\n    \n...\n</span><span style="color: #800000">"</span></pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>&nbsp;</p><p>&nbsp;</p><p>  4、返回子節點列表:</p><p>    .contents: 以列表的方式返回節點下的直接子節點</p><p>    .children:以生成器的方式反回節點下的直接子節點</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>soup = BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span><span style="color: #000000">)head_tag </span>=<span style="color: #000000"> soup.headprint(head_tag)print(head_tag.contents)print(head_tag.contents[</span><span style="color: #800080">0</span><span style="color: #000000">])print(head_tag.contents[</span><span style="color: #800080">0</span><span style="color: #000000">].contents)
</span><span style="color: #0000ff">for</span> ch <span style="color: #0000ff">in</span><span style="color: #000000"> head_tag.children:    print(ch)
結果:</span>&lt;head&gt;&lt;title&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/title&gt;&lt;/head&gt;</span>[&lt;title&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/title&gt;]</span>&lt;title&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/title&gt;</span>[<span style="color: #800000">"</span><span style="color: #800000">The Dormouse's story</span><span style="color: #800000">"</span><span style="color: #000000">]</span>&lt;title&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/title&gt;</span></pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>&nbsp;</p><p>  5、返回子孫節點的生成器:</p><p>     .descendants: 以列表的方式返回標簽下的子孫節點</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre><span style="color: #0000ff">for</span> ch <span style="color: #0000ff">in</span><span style="color: #000000"> head_tag.descendants:    print(ch)
結果:</span>&lt;title&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/title&gt;</span>The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story</span></pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>&nbsp;</p><p>  6、父標簽(parent):如果是bs4對象,不管本來是標簽還是文本都可以找到其父標簽,但是文本對象不能找到父標簽</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>soup = BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span><span style="color: #000000">)tag_title </span>=<span style="color: #000000"> soup.b  # b標簽print(tag_title.parent)  # b標簽的父標簽 pprint(type(tag_title.</span><span style="color: #0000ff">string</span>))  # b標簽中的文本的類型,文本中有注釋時結果為None &lt;<span style="color: #0000ff">class</span> <span style="color: #800000">'</span><span style="color: #800000">bs4.element.NavigableString</span><span style="color: #800000">'</span>&gt;<span style="color: #000000">print(tag_title.</span><span style="color: #0000ff">string</span><span style="color: #000000">.parent)  # b標簽中文本的父標簽 bprint(type(tag_title.text))  # b 標簽中的文本類型為str,無bs4屬性找到父標簽</span></pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>&nbsp;</p><p>  7、遞歸父標簽(parents):遞歸得到元素的所有父輩節點</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>soup = BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span><span style="color: #000000">)link </span>=<span style="color: #000000"> soup.a</span><span style="color: #0000ff">for</span> parent <span style="color: #0000ff">in</span><span style="color: #000000"> link.parents:    print(parent.name)<br><br>結果:<br></span></pre><p>p<br>body<br>html<br>[document]</p>















<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>&nbsp;</p><p>  8、前后節點查詢(不是前后標簽哦,文本也是節點之一):previous_sibling,next_sibling</p><p><img src="https://images2017.cnblogs.com/blog/931154/201801/931154-20180124082140694-1377077553.png" alt=""></p><p>&nbsp;</p><p>&nbsp;  9、以生成器的方式迭代返回所有兄弟節點</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre><span style="color: #0000ff">for</span> sib <span style="color: #0000ff">in</span><span style="color: #000000"> soup.a.next_siblings:    print(sib)    print(</span><span style="color: #800000">"</span><span style="color: #800000">---------</span><span style="color: #800000">"</span><span style="color: #000000">)
結果:</span>-------------<span style="color: #000000">,        </span>---------&lt;a <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/lacie</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link2</span><span style="color: #800000">"</span>&gt;Lacie&lt;/a&gt;---------

---------&lt;a <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/tillie</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link3</span><span style="color: #800000">"</span>&gt;Tillie&lt;/a&gt;---------<span style="color: #000000">;        and they lived at the bottom of a well.    </span>---------</pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>&nbsp;</p><p>  10、搜索文檔樹</p><p>    過濾器:</p><p>      1、字符串</p><p>      2、正則表達式</p><p>      3、列表</p><p>      4、True</p><p>      5、方法</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>html_doc = <span style="color: #800000">"""</span><span style="color: #800000">&lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse's story&lt;/title&gt;&lt;/head&gt;</span>&lt;body&gt;&lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">title</span><span style="color: #800000">"</span>&gt;&lt;b&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/b&gt;&lt;/p&gt;</span>&lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">story</span><span style="color: #800000">"</span>&gt;Once upon a time there were three little sisters; and their names were&lt;/p&gt;&lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/elsie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link1</span><span style="color: #800000">"</span>&gt;Elsie&lt;/a&gt;<span style="color: #000000">,</span>&lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/lacie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link2</span><span style="color: #800000">"</span>&gt;Lacie&lt;/a&gt;<span style="color: #000000"> and</span>&lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/tillie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link3</span><span style="color: #800000">"</span>&gt;Tillie&lt;/a&gt;<span style="color: #000000">;and they lived at the bottom of a well.
</span>&lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">story</span><span style="color: #800000">"</span>&gt;...&lt;/p&gt;&lt;/body&gt;<span style="color: #800000">"""</span><span style="color: #0000ff">from</span><span style="color: #000000"> bs4 import BeautifulSoupimport resoup </span>= BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span><span style="color: #000000">)soup.find_all(</span><span style="color: #800000">"</span><span style="color: #800000">a</span><span style="color: #800000">"</span><span style="color: #000000">)  # 字符串參數soup.find_all(re.compile(</span><span style="color: #800000">"</span><span style="color: #800000">^b</span><span style="color: #800000">"</span><span style="color: #000000">))  # 正則參數soup.find_all(re.compile(</span><span style="color: #800000">"</span><span style="color: #800000">a</span><span style="color: #800000">"</span><span style="color: #000000">))  # 正則參數soup.find_all(re.compile(</span><span style="color: #800000">"</span><span style="color: #800000">l$</span><span style="color: #800000">"</span><span style="color: #000000">))  # 正則參數soup.find_all([</span><span style="color: #800000">"</span><span style="color: #800000">a</span><span style="color: #800000">"</span>, <span style="color: #800000">"</span><span style="color: #800000">b</span><span style="color: #800000">"</span><span style="color: #000000">])  # 標簽的列表參數soup.find_all(True)  # 返回所有標簽def has_class_no_id(tag):    </span><span style="color: #0000ff">return</span> tag.has_attr(<span style="color: #800000">"</span><span style="color: #800000">class</span><span style="color: #800000">"</span>) and not tag.has_attr(<span style="color: #800000">"</span><span style="color: #800000">id</span><span style="color: #800000">"</span><span style="color: #000000">)soup.find_all(has_class_no_id)  # 方法參數</span></pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>&nbsp;</p><p><span style="font-family: 黑體; font-size: 14pt">  11、find選擇器:</span></p><p>    語法 :</p><pre><span>    # find_all( name , attrs , recursive , text , **<span>kwargs )    #  name :要查找的標簽名    #  attrs: 標簽的屬性    #  recursive: 遞歸    #  text: 查找文本    # **<span>kwargs :其它 鍵值參數<br><br>  特殊情況:<br>    </span></span></span><span class="s">data-foo="value",</span><span class="s">因中橫杠不識別的原因,只能寫成</span><span class="s">attrs={"data-foo":"value"},</span></pre><pre><span><span><span>    class="value",因class是關鍵字,所以要寫成class_="value"或attrs={"class":"value"}</span></span></span></pre><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre><span style="color: #0000ff">from</span><span style="color: #000000"> bs4 import BeautifulSoupimport rehtml_doc </span>= <span style="color: #800000">"""</span>&lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/title&gt;&lt;/head&gt;</span>
&lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">title</span><span style="color: #800000">"</span>&gt;&lt;b&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/b&gt;&lt;/p&gt;</span>
&lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">story</span><span style="color: #800000">"</span>&gt;<span style="color: #000000">Once upon a time there were three little sisters; and their names were</span>&lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/elsie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link1</span><span style="color: #800000">"</span>&gt;Elsie&lt;/a&gt;<span style="color: #000000">,</span>&lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/lacie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link2</span><span style="color: #800000">"</span>&gt;Lacie&lt;/a&gt;<span style="color: #000000"> and</span>&lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/tillie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link3</span><span style="color: #800000">"</span>&gt;Tillie&lt;/a&gt;<span style="color: #000000">;and they lived at the bottom of a well.</span>&lt;/p&gt;
&lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">story</span><span style="color: #800000">"</span>&gt;...&lt;/p&gt;<span style="color: #800000">"""</span><span style="color: #000000"># find_all( name , attrs , recursive , text , </span>**<span style="color: #000000">kwargs )#  name :要查找的標簽名(字符串、正則、方法、True)#  attrs: 標簽的屬性#  recursive: 遞歸#  text: 查找文本# </span>**<span style="color: #000000">kwargs :其它 鍵值參數soup </span>= BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span><span style="color: #000000">)print(soup.find_all(</span><span style="color: #800000">'</span><span style="color: #800000">p</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">title</span><span style="color: #800000">'</span>)) # p標簽且class=<span style="color: #800000">"</span><span style="color: #800000">title</span><span style="color: #800000">"</span><span style="color: #000000">soup.find_all(</span><span style="color: #800000">'</span><span style="color: #800000">title</span><span style="color: #800000">'</span><span style="color: #000000">)  # 以列表形式返回 所有title標簽asoup.find_all(attrs</span>={<span style="color: #800000">"</span><span style="color: #800000">class</span><span style="color: #800000">"</span>:<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span>})  # 以列表形式返回 所有class屬性==<span style="color: #000000">sister的標簽soup.find_all(id</span>=<span style="color: #800000">'</span><span style="color: #800000">link2</span><span style="color: #800000">'</span>)  # 返回所有id屬性==<span style="color: #000000">link2的標簽soup.find_all(href</span>=re.compile(<span style="color: #800000">"</span><span style="color: #800000">elsie</span><span style="color: #800000">"</span><span style="color: #000000">)) # 返回所有href屬性包含elsie的標簽soup.find_all(id</span>=<span style="color: #000000">True)  # 返回 所有包含id屬性的標簽soup.find_all(id</span>=<span style="color: #800000">"</span><span style="color: #800000">link1</span><span style="color: #800000">"</span>, href=re.compile(<span style="color: #800000">'</span><span style="color: #800000">elsie</span><span style="color: #800000">'</span>))  #  id=link1且href包含elsie</pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p><img src="https://images2017.cnblogs.com/blog/931154/201801/931154-20180128222706647-1457600468.png" alt=""></p><pre>關於class的搜索</pre><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>soup = BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span><span style="color: #000000">)css_soup </span>= BeautifulSoup(<span style="color: #800000">'</span><span style="color: #800000">&lt;p class="body strikeout"&gt;&lt;/p&gt;</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span><span style="color: #000000">)css_soup.find_all(</span><span style="color: #800000">"</span><span style="color: #800000">p</span><span style="color: #800000">"</span>, class_=<span style="color: #800000">"</span><span style="color: #800000">body</span><span style="color: #800000">"</span><span style="color: #000000">)  # 多值class,指定其中一個即可css_soup.find_all(</span><span style="color: #800000">"</span><span style="color: #800000">p</span><span style="color: #800000">"</span>, class_=<span style="color: #800000">"</span><span style="color: #800000">strikeout</span><span style="color: #800000">"</span><span style="color: #000000">)css_soup.find_all(</span><span style="color: #800000">"</span><span style="color: #800000">p</span><span style="color: #800000">"</span>, class_=<span style="color: #800000">"</span><span style="color: #800000">body strikeout</span><span style="color: #800000">"</span><span style="color: #000000">)  # 精確匹配# text 參數可以是字符串,列表、方法、Truesoup.find_all(</span><span style="color: #800000">"</span><span style="color: #800000">a</span><span style="color: #800000">"</span>, text=<span style="color: #800000">"</span><span style="color: #800000">Elsie</span><span style="color: #800000">"</span>)  # text=<span style="color: #800000">"</span><span style="color: #800000">Elsie</span><span style="color: #800000">"</span>的a標簽</pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>&nbsp;</p><p>  12、父節點方法:</p><p>    find_parents(&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32">name</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css">attrs</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive">recursive</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text">text</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword">**kwargs</a>&nbsp;)</p><p>    find_parent(&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32">name</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css">attrs</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive">recursive</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text">text</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword">**kwargs</a>&nbsp;)</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>html_doc = <span style="color: #800000">"""</span><span style="color: #800000">&lt;html&gt;</span>    &lt;head&gt;        &lt;title&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/title&gt;</span>    &lt;/head&gt;&lt;body&gt;    &lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">title</span><span style="color: #800000">"</span>&gt;&lt;b&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/b&gt;&lt;/p&gt;</span>    &lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">story</span><span style="color: #800000">"</span>&gt;Once upon a time there were three little sisters; and their names were&lt;/p&gt;    &lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/elsie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link1</span><span style="color: #800000">"</span>&gt;Elsie&lt;/a&gt;<span style="color: #000000">,    </span>&lt;p&gt;        &lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/lacie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link2</span><span style="color: #800000">"</span>&gt;Lacie&lt;/a&gt;<span style="color: #000000"> and    </span>&lt;/p&gt;    &lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/tillie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link3</span><span style="color: #800000">"</span>&gt;Tillie&lt;/a&gt;<span style="color: #000000">;    and they lived at the bottom of a well.    </span>&lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">story</span><span style="color: #800000">"</span>&gt;...&lt;/p&gt;&lt;/body&gt;<span style="color: #800000">"""</span><span style="color: #0000ff">from</span><span style="color: #000000"> bs4 import BeautifulSoupsoup </span>= BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span><span style="color: #000000">)a_string </span>= soup.find(text=<span style="color: #800000">"</span><span style="color: #800000">Lacie</span><span style="color: #800000">"</span><span style="color: #000000">)  # 文本為Lacie的節點type(a_string), a_string  # </span>&lt;<span style="color: #0000ff">class</span> <span style="color: #800000">'</span><span style="color: #800000">bs4.element.NavigableString</span><span style="color: #800000">'</span>&gt;<span style="color: #000000"> Laciea_parent </span>=<span style="color: #000000"> a_string.find_parent()  # a_string的父節點中的第一個節點a_parent </span>= a_string.find_parent(<span style="color: #800000">"</span><span style="color: #800000">p</span><span style="color: #800000">"</span><span style="color: #000000">)  # a_string的父節點中的第一個p節點a_parents </span>=<span style="color: #000000"> a_string.find_parents()  # a_string的父節點a_parents </span>= a_string.find_parents(<span style="color: #800000">"</span><span style="color: #800000">a</span><span style="color: #800000">"</span>)  # a_string的父點中所有a節點</pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>&nbsp;</p><p>  13、后面的鄰居節點:</p><p>    find_next_siblings(&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32">name</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css">attrs</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive">recursive</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text">text</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword">**kwargs</a>&nbsp;)</p><p>    find_next_sibling(&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32">name</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css">attrs</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive">recursive</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text">text</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword">**kwargs</a>&nbsp;)</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>html_doc = <span style="color: #800000">"""</span><span style="color: #800000">&lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse's story&lt;/title&gt;&lt;/head&gt;</span>&lt;body&gt;    &lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">title</span><span style="color: #800000">"</span>&gt;&lt;b&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/b&gt;&lt;/p&gt;</span>    &lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">story</span><span style="color: #800000">"</span>&gt;Once upon a time there were three little sisters; and their names were&lt;/p&gt;    &lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/elsie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link1</span><span style="color: #800000">"</span>&gt;Elsie&lt;/a&gt;<span style="color: #000000">,    </span>&lt;b href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/elsie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link1</span><span style="color: #800000">"</span>&gt;Elsie&lt;/b&gt;<span style="color: #000000">,    </span>&lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/lacie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link2</span><span style="color: #800000">"</span>&gt;Lacie&lt;/a&gt;<span style="color: #000000"> and    </span>&lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/tillie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link3</span><span style="color: #800000">"</span>&gt;Tillie&lt;/a&gt;<span style="color: #000000">;        and they lived at the bottom of a well.    </span>&lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">story</span><span style="color: #800000">"</span>&gt;...&lt;/p&gt;&lt;/body&gt;<span style="color: #800000">"""</span><span style="color: #0000ff">from</span><span style="color: #000000"> bs4 import BeautifulSoupsoup </span>= BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span><span style="color: #000000">)first_link </span>=<span style="color: #000000"> soup.a  # 第一個a標簽a_sibling </span>=<span style="color: #000000"> first_link.find_next_sibling()  # 后面鄰居的第一個a_sibling </span>= first_link.find_next_sibling(<span style="color: #800000">"</span><span style="color: #800000">a</span><span style="color: #800000">"</span><span style="color: #000000">)  # 后面鄰居的第一個aa_siblings </span>=<span style="color: #000000"> first_link.find_next_siblings()  # 后面的所有鄰居a_siblings </span>= first_link.find_next_siblings(<span style="color: #800000">"</span><span style="color: #800000">a</span><span style="color: #800000">"</span>)  # 后面鄰居的所有a鄰居</pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>&nbsp;</p><p>&nbsp;  14、前面的鄰居節點:</p><p>    find_previous_siblings(&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32">name</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css">attrs</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive">recursive</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text">text</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword">**kwargs</a>&nbsp;)</p><p>    find_previous_sibling(&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32">name</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css">attrs</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive">recursive</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text">text</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword">**kwargs</a>&nbsp;)</p><p>&nbsp;</p><p>  15、后面的節點:</p><p>    find_all_next(&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32">name</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css">attrs</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive">recursive</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text">text</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword">**kwargs</a>&nbsp;)</p><p>    find_next(&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32">name</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css">attrs</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive">recursive</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text">text</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword">**kwargs</a>&nbsp;)</p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>html_doc = <span style="color: #800000">"""</span><span style="color: #800000">&lt;html&gt;</span>    &lt;head&gt;        &lt;title&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/title&gt;</span>    &lt;/head&gt;&lt;body&gt;    &lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">title</span><span style="color: #800000">"</span>&gt;&lt;b&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/b&gt;&lt;/p&gt;</span>    &lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">story</span><span style="color: #800000">"</span>&gt;Once upon a time there were three little sisters; and their names were&lt;/p&gt;    &lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/elsie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link1</span><span style="color: #800000">"</span>&gt;Elsie&lt;/a&gt;<span style="color: #000000">,    </span>&lt;p&gt;        &lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/lacie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link2</span><span style="color: #800000">"</span>&gt;Lacie&lt;/a&gt;<span style="color: #000000"> and    </span>&lt;/p&gt;    &lt;p&gt;        &lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/tillie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link3</span><span style="color: #800000">"</span>&gt;Tillie&lt;/a&gt;<span style="color: #000000">;    </span>&lt;/p&gt;<span style="color: #000000">        and they lived at the bottom of a well.    </span>&lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">story</span><span style="color: #800000">"</span>&gt;...&lt;/p&gt;&lt;/body&gt;<span style="color: #800000">"""</span><span style="color: #0000ff">from</span><span style="color: #000000"> bs4 import BeautifulSoupsoup </span>= BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span><span style="color: #000000">)a_string </span>= soup.find(text=<span style="color: #800000">"</span><span style="color: #800000">Lacie</span><span style="color: #800000">"</span><span style="color: #000000">)a_next </span>=<span style="color: #000000"> a_string.find_next()  # 后面所有子孫標簽的第一個a_next </span>= a_string.find_next(<span style="color: #800000">'</span><span style="color: #800000">a</span><span style="color: #800000">'</span><span style="color: #000000">)  # 后面所有子孫標簽的第一個a標簽a_nexts </span>=<span style="color: #000000"> a_string.find_all_next()  # 后面的所有子孫標簽a_nexts </span>= a_string.find_all_next(<span style="color: #800000">'</span><span style="color: #800000">a</span><span style="color: #800000">'</span>)  # 后面的所有子孫標簽中的所有a標簽</pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>&nbsp;</p><p>&nbsp;  16、前面的節點:</p><p>    find_all_previous(&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32">name</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css">attrs</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive">recursive</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text">text</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword">**kwargs</a>&nbsp;)</p><p>    find_previous(&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32">name</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css">attrs</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive">recursive</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text">text</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword">**kwargs</a>&nbsp;)</p><p>&nbsp;</p><p>  17、解析部分文檔:</p><p>    如果僅僅因為想要查找文檔中的&lt;a&gt;標簽而將整片文檔進行解析,實在是浪費內存和時間.最快的方法是從一開始就把&lt;a&gt;標簽以外的東西都忽略掉.&nbsp;<tt class="docutils literal"><span class="pre">SoupStrainer</span></tt>&nbsp;類可以定義文檔的某段內容,這樣搜索文檔時就不必先解析整篇文檔,只會解析在&nbsp;<tt class="docutils literal"><span class="pre">SoupStrainer</span></tt>&nbsp;中定義過的文檔. 創建一個&nbsp;<tt class="docutils literal"><span class="pre">SoupStrainer</span></tt>&nbsp;對象並作為&nbsp;<tt class="docutils literal"><span class="pre">parse_only</span></tt>&nbsp;參數給&nbsp;<tt class="docutils literal"><span class="pre">BeautifulSoup</span></tt>&nbsp;的構造方法即可。</p><p><tt class="docutils literal"><span class="pre">  SoupStrainer</span></tt>&nbsp;類參數:<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32">name</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css">attrs</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive">recursive</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text">text</a>&nbsp;,&nbsp;<a class="reference internal" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword">**kwargs</a></p><div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div><pre>html_doc = <span style="color: #800000">"""</span><span style="color: #800000">&lt;html&gt;</span>    &lt;head&gt;        &lt;title&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/title&gt;</span>    &lt;/head&gt;&lt;body&gt;    &lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">title</span><span style="color: #800000">"</span>&gt;&lt;b&gt;The Dormouse<span style="color: #800000">'</span><span style="color: #800000">s story&lt;/b&gt;&lt;/p&gt;</span>    &lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">story</span><span style="color: #800000">"</span>&gt;<span style="color: #000000">Once upon a time there were three little sisters; and their names were        </span>&lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/elsie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link1</span><span style="color: #800000">"</span>&gt;Elsie&lt;/a&gt;<span style="color: #000000">,        </span>&lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/lacie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link2</span><span style="color: #800000">"</span>&gt;Lacie&lt;/a&gt;<span style="color: #000000"> and        </span>&lt;a href=<span style="color: #800000">"</span><span style="color: #800000">http://example.com/tillie</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">sister</span><span style="color: #800000">"</span> id=<span style="color: #800000">"</span><span style="color: #800000">link3</span><span style="color: #800000">"</span>&gt;Tillie&lt;/a&gt;<span style="color: #000000">;    </span>&lt;/p&gt;<span style="color: #000000">        and they lived at the bottom of a well.    </span>&lt;p <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">story</span><span style="color: #800000">"</span>&gt;...&lt;/p&gt;&lt;/body&gt;<span style="color: #800000">"""</span><span style="color: #0000ff">from</span><span style="color: #000000"> bs4 import SoupStrainera_tags </span>= SoupStrainer(<span style="color: #800000">'</span><span style="color: #800000">a</span><span style="color: #800000">'</span><span style="color: #000000">)  # 所有a標簽id_tags </span>= SoupStrainer(id=<span style="color: #800000">"</span><span style="color: #800000">link2</span><span style="color: #800000">"</span>)  # id=<span style="color: #000000">link2的標簽def is_short_string(</span><span style="color: #0000ff">string</span><span style="color: #000000">):    </span><span style="color: #0000ff">return</span> len(<span style="color: #0000ff">string</span>) &lt; <span style="color: #800080">10</span><span style="color: #000000">  # string長度小於10,返回Trueshort_string </span>= SoupStrainer(text=<span style="color: #000000">is_short_string)  # 符合條件的文本
</span><span style="color: #0000ff">from</span><span style="color: #000000"> bs4 import BeautifulSoupsoup </span>= BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span>, parse_only=<span style="color: #000000">a_tags).prettify()soup </span>= BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span>, parse_only=<span style="color: #000000">id_tags).prettify()soup </span>= BeautifulSoup(html_doc, <span style="color: #800000">'</span><span style="color: #800000">html.parser</span><span style="color: #800000">'</span>, parse_only=short_string).prettify()</pre><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="復制代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="復制代碼"></a></span></div></div><p>&nbsp;</p></div>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM