標籤:license 函數 安裝 iss build help ant mis pac
今天介紹一個非常好用的python爬蟲庫——beautifulsoup4。beautifulsoup4的中文文檔參考網址是:http://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/
首先使用pip安裝這個庫,當然還要用到lxml這個解析器,配合使用可以很方便的協助我們處理html文檔,提取所需要的資訊。可以使用pip list命令查看你已經安裝好的包。提醒大家注意一點!一定是pip install beautifulsoup4 ,這個4千萬別忘記了,否則會出現如下報錯資訊:
print "Unit tests have failed!"
SyntaxError: Missing parentheses in call to ‘print‘
Command "python setup.py egg_info" failed with error code 1 in C:\Users\ADMINI~1\AppData\Local\Temp\pip-build-4g6q3fil\...
因為python中的print函數,在python3中是需要加括弧的,所以我們可以知道報錯是因為版本不相容導致的。python3使用的beautifulsoup4,我之前安裝就是出現了這個問題,好在很快發現瞭解決了。安裝成功會出現successfully。
1 C:\Users\Administrator\Desktop 2 λ ipython 3 Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] 4 Type ‘copyright‘, ‘credits‘ or ‘license‘ for more information 5 IPython 6.2.1 -- An enhanced Interactive Python. Type ‘?‘ for help. 6 #匯入這個包 7 In [1]: from bs4 import BeautifulSoup 8 9 In [2]: html="""10 ...: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>我的部落格(CCColby.com)</title> </head> <body> <video width="320" height="240" controls> <source src="m11 ...: ovie.mp4" type="video/mp4"> <source src="movie.ogg" type="video/ogg"> 你的瀏覽器不支援 video 標籤。 </video> </body> </html>12 ...: """13 #建立對象,如果不指定解析方式,會出現警告14 In [3]: soup=BeautifulSoup(html)15 c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages\bs4\__init__.py:181: UserWarning: No parser was explicitly specified, so I‘m using the best available HTML parser for this system ("lxml"). This usually isn‘t a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.16 17 The code that caused this warning is on line 193 of the file c:\users\administrator\appdata\local\programs\python\python36\lib\runpy.py. To get rid of this warning, change code that looks like this:18 19 BeautifulSoup(YOUR_MARKUP})20 21 to this:22 23 BeautifulSoup(YOUR_MARKUP, "lxml")24 25 markup_type=markup_type))26 #我們制定解析方式為‘lxml‘27 In [4]: soup=BeautifulSoup(html,"lxml")28 #格式化輸出soup對象29 In [5]: print(soup.prettify())30 <!DOCTYPE HTML>31 <html>32 <head>33 <meta charset="utf-8"/>34 <title>35 我的部落格(CCColby.com)36 </title>37 </head>38 <body>39 <video controls="" height="240" width="320">40 <source src="movie.mp4" type="video/mp4">41 <source src="movie.ogg" type="video/ogg">42 你的瀏覽器不支援 video 標籤。43 </source>44 </source>45 </video>46 </body>47 </html>
beautifulsoup將複雜的HTML文檔歸結為一個樹形結構,每個節點都是python對象。這些對象分成四種:Tag、NavigableString、BeautifulSoup、Comment。
可以利用soup加上標籤名,可以輕鬆的擷取標籤內容
1 In [6]: print(soup.title)2 <title>我的部落格(CCColby.com)</title>3 4 In [7]: print(soup.head)5 <head> <meta charset="utf-8"/> <title>我的部落格(CCColby.com)</title> </head>6 7 In [8]: print(soup.source)8 <source src="movie.mp4" type="video/mp4"> <source src="movie.ogg" type="video/ogg"> 你的瀏覽器不支援 video 標籤。 </source></source>
如果我們要擷取標籤內部的文字怎麼辦?很簡單
1 In [9]: print(soup.titie.string)2 3 我的部落格(CCColby.com)
關於beautifulsoup的遍曆文檔樹,可以用contents方法、children方法。如果要遍曆所有子節點,則用descendants屬性。具體的用法在執行個體中學習就可以了。
搜尋文檔樹find_all(name ,attrs,recursive,text,**kwargs)
其中name參數可以尋找所有名字為name的Tag,字串對象會被自動忽略;可以傳入字串、Regex(re.compile()編譯過的)、傳列表。text參數是尋找文檔中的字元內容。
還有一種尋找方法CSS選取器。
1 #通過標籤名尋找 2 3 print(soup.select(‘title‘)) 4 5 #通過屬性尋找 6 7 print(sou.select(a[class="name"]‘)) 8 9 10 #以上select返回的結果都是列表形式,要用遍曆輸出,然後用get_text()方法來擷取它的內容11 12 for title in soup.select(‘title‘):13 print(title.get_text())
下一篇文章,講一個用beautifulsoup執行個體來加深理解。
python爬蟲(7)——BeautifulSoup