1. 程式人生 > >python下很帥氣的爬蟲包 - Beautiful Soup 示例

python下很帥氣的爬蟲包 - Beautiful Soup 示例

如何 lan linux下 csdn bottom 數量 ... 安裝包 一個

先發一下官方文檔地址。http://www.crummy.com/software/BeautifulSoup/bs4/doc/

建議有時間可以看一下python包的文檔。

Beautiful Soup 相比其他的html解析有個非常重要的優勢。html會被拆解為對象處理。全篇轉化為字典和數組。

相比正則解析的爬蟲,省略了學習正則的高成本。

相比xpath爬蟲的解析,同樣節約學習時間成本。雖然xpath已經簡單點了。(爬蟲框架Scrapy就是使用xpath)

安裝

linux下可以執行

apt-get install python-bs4  

也可以用python的安裝包工具來安裝

easy_install beautifulsoup4  
  
pip install beautifulsoup4  

使用簡介

下面說一下BeautifulSoup 的使用。

解析html需要提取數據。其實主要有幾點

1:獲取指定tag的內容。

<p>hello, watsy</p><br><p>hello, beautiful soup.</p>  

2:獲取指定tag下的屬性。

<a href="http://blog.csdn.net/watsy">watsy‘s blog</
a>

3:如何獲取,就需要用到查找方法。

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)  
  
print(soup.prettify())  
# <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="link2">  
#     Tillie  
#    </a>  
#    ; and they lived at the bottom of a well.  
#   </p>  
#   <p class="story">  
#    ...  
#   </p>  
#  </body>  
# </html>  

獲取指定tag的內容

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.a  
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>  

上面示例給出了4個方面

1:獲取tag

soup.title

2:獲取tag名稱

soup.title.name

3:獲取title tag的內容

soup.title.string

4:獲取title的父節點tag的名稱

soup.title.parent.name

怎麽樣,非常對象化的使用吧。

提取tag屬性

下面要說一下如何提取href等屬性。

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

獲取屬性。方法是

soup.tag[‘屬性名稱‘]

<a href="http://blog.csdn.net/watsy">watsy‘s blog</a>  

常見的應該是如上的提取聯接。

代碼是

soup.a[‘href‘] 

相當easy吧。

查找與判斷

接下來進入重要部分。全文搜索查找提取.

soup提供find與find_all用來查找。其中find在內部是調用了find_all來實現的。因此只說下find_all

def find_all(self, name=None, attrs={}, recursive=True, text=None,  
                 limit=None, **kwargs): 

看參數。

第一個是tag的名稱,第二個是屬性。第3個選擇遞歸,text是判斷內容。limit是提取數量限制。**kwargs 就是字典傳遞了。。

舉例使用。

tag名稱  
soup.find_all(‘b‘)  
# [<b>The Dormouse‘s story</b>]  
  
正則參數  
import re  
for tag in soup.find_all(re.compile("^b")):  
    print(tag.name)  
# body  
# b  
  
for tag in soup.find_all(re.compile("t")):  
    print(tag.name)  
# html  
# title  
  
列表  
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>]  
  
函數調用  
def has_class_but_no_id(tag):  
    return tag.has_attr(‘class‘) and not tag.has_attr(‘id‘)  
  
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...</p>,  
#  <p class="story">...</p>]  
  
tag的名稱和屬性查找  
soup.find_all("p", "title")  
# [<p class="title"><b>The Dormouse‘s story</b></p>]  
  
tag過濾  
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>]  
  
tag屬性過濾  
soup.find_all(id="link2")  
# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]  
  
text正則過濾  
import re  
soup.find(text=re.compile("sisters"))  
# u‘Once upon a time there were three little sisters; and their names were\n‘  

獲取內容和字符串

獲取tag的字符串
title_tag.string  
# u‘The Dormouse‘s story‘ 
註意在實際使用中應該使用 unicode(title_tag.string)來轉換為純粹的string對象 使用strings屬性會返回soup的構造1個叠代器,叠代tag對象下面的所有文本內容
for string in soup.strings:  
    print(repr(string))  
# u"The Dormouse‘s story"  
# u‘\n\n‘  
# u"The Dormouse‘s story"  
# u‘\n\n‘  
# u‘Once upon a time there were three little sisters; and their names were\n‘  
# u‘Elsie‘  
# u‘,\n‘  
# u‘Lacie‘  
# u‘ and\n‘  
# u‘Tillie‘  
# u‘;\nand they lived at the bottom of a well.‘  
# u‘\n\n‘  
# u‘...‘  
# u‘\n‘  
獲取內容 .contents會以列表形式返回tag下的節點。
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‘]  
想想,應該沒有什麽其他的了。。其他的也可以看文檔學習使用。 總結 其實使用起主要是
soup = BeatifulSoup(data)  
soup.title  
soup.p.[‘title‘]  
divs = soup.find_all(‘div‘, content=‘tpc_content‘)  
divs[0].contents[0].string  

python下很帥氣的爬蟲包 - Beautiful Soup 示例