beautifulSoup庫的使用案例

2021-10-07 04:33:53 字數 1335 閱讀 1682

from urllib.request import urlopen

from bs4 import beautifulsoup

url = ''

html = urlopen(url)

bs = beautifulsoup(html, 'html.parser')

for child in bs.find('table', ).children:

print(child)

find_all 獲取到包含乙個便簽的所有列表

bs.find_all('table')[4].find_all('tr')[2].find('td').find_all('div')[1].find('a')
find_all 獲取指定類名的標籤的所有結果

# 獲取 span 標籤 類名為 green 的所有結果

namelist = bs.find_all('span', )

get_text 清除所有標籤,只返回包含文字的部分

namelist = bs.find_all('span',).get_text()
獲取滿足多個條件中的乙個時的結果也可以用find_all

.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])

.find_all('span',})

查詢包含 the prince 內容的標籤

namelist = bs.find_all(text='the prince')
返回第乙個在class_屬性中包含單詞text並且在id屬性中包含titile的標籤

title = bs.find_all(id='title', class_='text')
注意一下兩種方式完全一樣

bs.find_all(id='text')

bs.find_all('',)

from urllib.request import urlopen

from bs4 import beautifulsoup

url = ''

html = urlopen(url)

bs = beautifulsoup(html, 'html.parser')

images = bs.find_all('img', )

for image in images:

print(image['src'])

BeautifulSoup庫的使用

解析器 使用方法 優勢劣勢 python標準庫 beautifulsoup markup,html.parser python的內建標準庫 執行速度適中 文件容錯能力強 python 2.7.3 or 3.2.2 前的版本中文容錯能力差 lxml html 解析器 beautifulsoup mar...

BeautifulSoup庫的使用

五.總結 from bs4 import beautifulsoup 例項化bs4物件 soup beautifulsoup res.text,lxml bs4解析出來的結果會是乙個列表 tag soup.select css選擇器表示式 css選擇器 1.根據節點名及節點層次關係定位標籤 標籤選擇...

BeautifulSoup庫的使用

beautifulsoup 解析庫解析器 使用方法 python標準庫 beautifulsoup markup,html.parser lxml html解析器 beautifulsoup markup,lxml lxml xml解析器 beautifulsoup markup,xml html5...