欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解BeautifulSoup獲取特定標(biāo)簽下內(nèi)容的方法

 更新時(shí)間:2020年12月07日 11:40:41   作者:qianc6350528  
這篇文章主要介紹了詳解BeautifulSoup獲取特定標(biāo)簽下內(nèi)容的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

以下是個(gè)人在學(xué)習(xí)beautifulSoup過程中的一些總結(jié),目前我在使用爬蟲數(shù)據(jù)時(shí)使用的方法的是:先用find_all()找出需要內(nèi)容所在的標(biāo)簽,如果所需內(nèi)容一個(gè)find_all()不能滿足,那就用兩個(gè)或者多個(gè)。接下來遍歷find_all的結(jié)果,用get_txt()、get(‘href')、得到文本或者鏈接,然后放入各自的列表中。這樣做有一個(gè)缺點(diǎn)就是txt的數(shù)據(jù)是一個(gè)單獨(dú)的列表,鏈接的數(shù)據(jù)也是一個(gè)單獨(dú)的列表,一方面不能體現(xiàn)這些數(shù)據(jù)之間的結(jié)構(gòu)性,另一方面當(dāng)想要獲得更多的內(nèi)容時(shí),就要創(chuàng)建更多的空列表。

遍歷所有標(biāo)簽:

soup.find_all('a')

找出所有頁面中含有標(biāo)簽a的html語句,結(jié)果以列表形式存儲。對找到的標(biāo)簽可以進(jìn)一步處理,如用for對結(jié)果遍歷,可以對結(jié)果進(jìn)行purify,得到如鏈接,字符等結(jié)果。

# 創(chuàng)建空列表
links=[] 
txts=[]
tags=soup.find_all('a')
for tag in tags:
  links.append(tag.get('href')
  txts.append(tag.txt)         #或者txts.append(tag.get_txt())

得到html的屬性名:

atr=[]
tags=soup.find_all('a')
for tag in tags:
  atr.append(tag.p('class')) # 得到a 標(biāo)簽下,子標(biāo)簽p的class名稱 

find_all()的相關(guān)用法實(shí)例:

實(shí)例來自BeautifulSoup中文文檔

1. 字符串

最簡單的過濾器是字符串.在搜索方法中傳入一個(gè)字符串參數(shù),Beautiful Soup會查找與字符串完整匹配的內(nèi)容,下面的例子用于查找文檔中所有的標(biāo)簽:

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

2.正則表達(dá)式

如果傳入正則表達(dá)式作為參數(shù),Beautiful Soup會通過正則表達(dá)式的 match() 來匹配內(nèi)容.下面例子中找出所有以b開頭的標(biāo)簽,這表示和標(biāo)簽都應(yīng)該被找到:

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

下面代碼找出所有名字中包含”t”的標(biāo)簽:

for tag in soup.find_all(re.compile("t")):
  print(tag.name)
# html
# title

3.列表

如果傳入列表參數(shù),Beautiful Soup會將與列表中任一元素匹配的內(nèi)容返回.下面代碼找到文檔中所有標(biāo)簽和標(biāo)簽:

soup.find_all(["a", "b"])
# [<b>The Dormouse's story</b>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

4.方法(自定義函數(shù),傳入find_all)

如果沒有合適過濾器,那么還可以定義一個(gè)方法,方法只接受一個(gè)元素參數(shù) [4] ,如果這個(gè)方法返回 True 表示當(dāng)前元素匹配并且被找到,如果不是則反回 False
下面方法校驗(yàn)了當(dāng)前元素,如果包含 class 屬性卻不包含 id 屬性,那么將返回 True:

def has_class_but_no_id(tag):
  return tag.has_attr('class') and not tag.has_attr('id')```

返回結(jié)果中只有

標(biāo)簽沒有標(biāo)簽,因?yàn)闃?biāo)簽還定義了”id”,沒有返回和,因?yàn)楹椭袥]有定義”class”屬性.
下面代碼找到所有被文字包含的節(jié)點(diǎn)內(nèi)容:

from bs4 import NavigableString
def surrounded_by_strings(tag):
  return (isinstance(tag.next_element, NavigableString)
      and isinstance(tag.previous_element, NavigableString))

for tag in soup.find_all(surrounded_by_strings):
  print tag.name
# p
# a
# a
# a
# p

5.按照CSS搜索

按照CSS類名搜索tag的功能非常實(shí)用,但標(biāo)識CSS類名的關(guān)鍵字 class 在Python中是保留字,使用 class 做參數(shù)會導(dǎo)致語法錯(cuò)誤.從Beautiful Soup的4.1.1版本開始,可以通過 class_ 參數(shù)搜索有指定CSS類名的tag:

soup.find_all("a", class_="sister")
# [<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

或者:

soup.find_all("a", attrs={"class": "sister"})
# [<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

6.按照text參數(shù)查找

通過 text 參數(shù)可以搜搜文檔中的字符串內(nèi)容.與 name 參數(shù)的可選值一樣, text 參數(shù)接受 字符串 , 正則表達(dá)式 , 列表, True . 看例子:

soup.find_all(text="Elsie")
# [u'Elsie']

soup.find_all(text=["Tillie", "Elsie", "Lacie"])
# [u'Elsie', u'Lacie', u'Tillie']

soup.find_all(text=re.compile("Dormouse"))
[u"The Dormouse's story", u"The Dormouse's story"]

def is_the_only_string_within_a_tag(s):
  ""Return True if this string is the only child of its parent tag.""
  return (s == s.parent.string)

soup.find_all(text=is_the_only_string_within_a_tag)
# [u"The Dormouse's story", u"The Dormouse's story", u'Elsie', u'Lacie', u'Tillie', u'...']

雖然 text 參數(shù)用于搜索字符串,還可以與其它參數(shù)混合使用來過濾tag.Beautiful Soup會找到 .string 方法與 text 參數(shù)值相符的tag.下面代碼用來搜索內(nèi)容里面包含“Elsie”的標(biāo)簽:

soup.find_all("a", text="Elsie")
# [<a  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1">Elsie</a>]

7.只查找當(dāng)前標(biāo)簽的子節(jié)點(diǎn)

調(diào)用tag的 find_all() 方法時(shí),Beautiful Soup會檢索當(dāng)前tag的所有子孫節(jié)點(diǎn),如果只想搜索tag的直接子節(jié)點(diǎn),可以使用參數(shù) recursive=False .

一段簡單的文檔:

<html>
 <head>
 <title>
  The Dormouse's story
 </title>
 </head>
...

是否使用 recursive 參數(shù)的搜索結(jié)果:

soup.html.find_all("title")
# [<title>The Dormouse's story</title>]

soup.html.find_all("title", recursive=False)
# []

到此這篇關(guān)于詳解BeautifulSoup獲取特定標(biāo)簽下內(nèi)容的方法的文章就介紹到這了,更多相關(guān)BeautifulSoup獲取特定標(biāo)簽內(nèi)容內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論