BeautifulSoup庫詳解

什麼是BeautifulSoup

Beautiful Soup 是一個能夠從HTML或XML文件中提取數據的Python庫.它可以經過你喜歡的轉換器實現慣用的文檔導航,查找,修改文檔的方式.css

多看官方文檔https://beautifulsoup.readthedocs.io/zh_CN/latest/html

經過例子來說解bs庫的用法

from bs4 import BeautifulSoup  
​  
html = '''  
<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>  
'''
  • soup = BeautifulSoup(html,'lxml') //將代碼進行格式化
output:  
 <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\>

詳解bs4對html的解析

Tag

bs的使用和字典的使用極爲類似,用.來進行運算瀏覽器

  • print(soup.title) 獲取title標籤(靈活變話:soup.a soup.li等

<title>The Dormouse's story</title>函數

  • print(soup.title.string) 獲取title中的內容(能夠靈活變化,得到其餘標籤中的內容)

The Dormouse's storyspa

  • print(soup.p["class"]) 獲取class的值 (也能夠靈活變換獲取id或者data-id的值)

\['title'\]code

  • print(soup.find(id = 'link3')) 利用find函數找到相應的標籤

bs能夠屢次調用獲得須要的標籤內容orm

<p class\="title"\>  
 <b\>  
 The Dormouse's story  
 </b\>  
 </p\>  
   
 input:print(soup.p.b.string)  
 output: The Dormouse's story

find_all

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

  • name: 根據標籤名來進行查詢(經常使用)htm

    • soup.find_all('a') find_all返回的是一個列表,相應的能夠soup.find_all('li')
經常使用方法是將列表中的元素提取出來進行處理  
alist = soup.find\_all('a')  
for a in alist:  
 function(a)
  • 根據attrs來進行查詢,用傳入字典的方式來精準查詢
html\='''  
<div class="panel">  
 <div class="panel-heading">  
 <h4>Hello</h4>  
 </div>  
 <div class="panel-body">  
 <ul class="list" id="list-1" name="elements">  
 <li class="element">Foo</li>  
 <li class="element">Bar</li>  
 <li class="element">Jay</li>  
 </ul>  
 <ul class="list list-small" id="list-2">  
 <li class="element">Foo</li>  
 <li class="element">Bar</li>  
 </ul>  
 </div>  
</div>  
'''  
from bs4 import BeautifulSoup  
soup = BeautifulSoup(html, 'lxml')  
print(soup.find\_all(attrs\={'id': 'list-1'}))  
print(soup.find\_all(attrs\={'name': 'elements'}))  
​  
上面兩句的output:  
\[<ul class\="list" id\="list-1" name\="elements"\>  
<li class\="element"\>Foo</li\>  
<li class\="element"\>Bar</li\>  
<li class\="element"\>Jay</li\>  
</ul\>\]
  • keyword參數

用tag的屬性來進行搜索,搜索每一個tag的id屬性three

soup.find_all(id = 'list-2')

class是特殊字,用下面方法進行處理

soup.find_all('',{"class":"element"})

  • 按照CSS搜索

能夠用class_ = ...... 來對class屬性進行搜索新屬性

soup.find_all("div",class_ = "panel-body")

谷歌瀏覽器快速得到標籤CSS選擇器方法

用選擇器對組件選擇---->找到相應的語句----->右鍵------>

屏幕快照 2020-02-17 下午3.29.10.png

能夠根據須要進行copy,selector即爲CSS的路徑

find_all()若沒有找到相應的數據返回一個空的列表

find()則返回一個None

相關文章
相關標籤/搜索