BeautifulSoup安裝及其應用

BeautifulSoup 安裝及其使用javascript

BeautifulSoup 是個好東東。css

官網見這裏: http://www.crummy.com/software/BeautifulSoup/html

下載地址見這裏:http://www.crummy.com/software/BeautifulSoup/bs4/download/4.1/ 附件有4.1.2的安裝源碼html5

文檔見這裏: http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html ,是中文翻譯的,不過文檔有點舊,是 3.0 的文檔版本,看起來沒有什麼意思。java

我推薦你們看個: http://www.crummy.com/software/BeautifulSoup/bs4/doc/ ,這個是 python 的官網英文版,看起來要舒服,清晰不少。python

在 python 下,你想按照 jquery 格式來讀取網頁,免除網頁格式、標籤的不規範的困擾,那麼 BeautifulSoup 是個不錯的選擇。按照官網所說, BeautifulSoup 是 Screen-Scraping 應用,旨在節省你們處理 HTML 標籤,而且從網絡中得到信息的工程。 BeautifulSoup 有這麼幾個優勢,使得其功能尤爲強大:jquery

1 : Beautiful Soup provides a few simple methods and Pythonic idioms for navigating, searching, and modifying a parse tree: a toolkit for dissecting a document and extracting what you need. It doesn't take much code to write an application 。關鍵詞: python 風格、提供簡單方法程序員

2 : Beautiful Soup automatically converts incoming documents to Unicode and outgoing documents to UTF-8. You don't have to think about encodings, unless the document doesn't specify an encoding and Beautiful Soup can't autodetect one. Then you just have to specify the original encoding 。關鍵詞:編碼轉換,使用 Python 的同窗都會認同Python 編碼格式的繁瑣, BeautifulSoup 能簡化這一點。ajax

3 : Beautiful Soup sits on top of popular Python parsers like lxml and html5lib , allowing you to try out different parsing strategies or trade speed for flexibility 。關鍵詞:兼容其它 html 解析器,可以讓你隨心替換。api

看完這幾個特性,想必有人心動了吧,咱們先看下 BeautifulSoup 的安裝:

安裝方法:

1 : apt-get install python-bs4

2 : easy_install beautifulsoup4

3 : pip install beautifulsoup4

4 :源碼安裝: python setup.py install

 

根據不一樣的操做系統,選用不一樣的安裝方法,這些方法都能安裝成功,不一樣點在於安裝的工具不一樣。我本身的系統採用的是第四種安裝方法,下面我來簡要介紹下第四種安裝方法:

 

Python代碼  收藏代碼

  1. curl http://www.crummy.com/software/BeautifulSoup/bs4/download/4.1/beautifulsoup4-4.1.2.tar.gz >> beautifulsoup4-4.1.2.tar.gz  

  2. tar zxvf beautifulsoup4-4.1.2.tar.gz  

  3. cd beautifulsoup4-4.1.2  

  4. python setup.py install  

 

Ok ,你就能看到安裝信息,提示安裝成功。

 

安裝成功,確定想火燒眉毛的使用,你打開 python command 窗口,你很 happy 的輸入:

 

Python代碼  收藏代碼

  1. from beautifulsoup import beautifulsoup  

 

sorry , ImportError ,爲何會有這個 import error ,我都安裝好了的。打開官網,從新看下說明,原來安裝的是BeautifulSoup 4.1 版本,這個 import 是 3.x 的說法。從新打開 command ,輸入:

 

Python代碼  收藏代碼

  1. import bs4

  2. from bs4 import BeautifulSoup  

咦,沒有輸出提示。恭喜你, BeautifulSoup 包引入成功。

看文上篇博客, http://isilic.iteye.com/blog/1733560 ,想試下 dir 命令,看看 BeautifulSoup 提供了哪些方法:

 

Python代碼  收藏代碼

  1. dir(BeautifulSoup)  

 看到一堆的方法,有點頭大,將方法列出來會方便看許多。

 

Python代碼  收藏代碼

  1. >>> for method in dir(BeautifulSoup):  

  2. ...        print method  

  3. ...  

 

       請仔細看下其中的 findXxx , nextXxx , previousXxx 方法,這些方法提供了 html 頁面的遍歷、回溯、查找、匹配功能;這些功能已經可以提供獲取頁面信息的方法了。

咱們以百度首頁爲例,試用下 BeautifulSoup 的強大功能。

 

Python代碼  收藏代碼

  1. >>> import urllib2  

  2. >>> page=urllib2.urlopen('http://www.baidu.com')  

  3. >>> soup=BeautifulSoup(page)  

  4. >>> print soup.title  

  5. >>> soup.title.string  

 

看到結果顯示不錯, helloworld 的教程讓人內心真是舒服啊。

想進一步試用功能,我想找出百度首頁上全部的連接,這個貌似很難,須要各類正則匹配,各類處理;等等,咱們如今是在談論這個 BeautifulSoup ,看看 BeautifulSoup 怎麼實現這個功能。

Python代碼  收藏代碼

  1. >>> for lind in soup.find_all('a'):  

  2. ...   print lind['href']  

  3. ...  

 

看到輸出了嗎?是否是很簡單。

 

對於熟悉 Jquery 和 CSS 的同窗,這種操做就是個折磨,須要不停的根據選擇出來的結果進行遍歷。看到上面的輸出,看到有不少的 # 這些非正常的 URL ,如今想把這些 URL 所有過濾掉,使用 select 語法就很簡單了。

Python代碼  收藏代碼

  1. >>> for link in soup.select('a[href^=http]'):  

  2. ...   print link['href'];  

  3. ...  

 

有人說我根據判斷出來的 URL 作處理不行嘛,固然能夠,我這裏只是想試下 select 的語法,至於 select 中的語法定義,你們能夠自行度之。準確的說,這個 select 語法都能從新開篇文章了。

再進一步,鏈接中的 / 或者 /duty 連接都是有含義的,是相對於本站的絕對地址,這些 / 開頭的怎麼不被過濾掉?若是是絕對地址的話,又該怎麼防止被過濾掉? href 標籤裏面是個 javascript 又該怎麼過濾?若是考慮 css 文件和js 文件的話,怎麼把這些文件的 url 也給找出來?還有更進一步的,怎麼分析出 js 中 ajax 的請求地址?這些都是能夠進一步擴展的一些要求。

好吧,我認可後面這些 URL 過濾已經超出了 BeautifulSoup 的能力範圍了,可是單純考慮功能的話,這些都是要考慮的內容,這些疑問你們考慮下實現原理就行,若是能作進一步的學習的話,算是本文額外的功勞了。

 

下面簡單過下 BeautifulSoup 的用法:

 

Python代碼  收藏代碼

  1. DEFAULT_BUILDER_FEATURES  

  2. FORMATTERS  

  3. ROOT_TAG_NAME  

  4. STRIP_ASCII_SPACES:BeautifulSoup的內置屬性  

  5. __call__  

  6. __class__  

  7. __contains__  

  8. __delattr__  

  9. __delitem__  

  10. __dict__  

  11. __doc__  

  12. __eq__  

  13. __format__  

  14. __getattr__  

  15. __getattribute__  

  16. __getitem__  

  17. __hash__  

  18. __init__  

  19. __iter__  

  20. __len__  

  21. __module__  

  22. __ne__  

  23. __new__  

  24. __nonzero__  

  25. __reduce__  

  26. __reduce_ex__  

  27. __repr__  

  28. __setattr__  

  29. __setitem__  

  30. __sizeof__  

  31. __str__  

  32. __subclasshook__  

  33. __unicode__  

  34. __weakref__  

  35. _all_strings  

  36. _attr_value_as_string  

  37. _attribute_checker  

  38. _feed  

  39. _find_all  

  40. _find_one  

  41. _lastRecursiveChild  

  42. _last_descendant  

  43. _popToTag:BeautifulSoup的內置方法,關於這些方法使用須要瞭解Python更深些的內容。  

  44. append:修改element tree  

  45. attribselect_re  

  46. childGenerator  

  47. children  

  48. clear:清除標籤內容  

  49. decode  

  50. decode_contents  

  51. decompose  

  52. descendants  

  53. encode  

  54. encode_contents  

  55. endData  

  56. extract:這個方法很關鍵,後面有介紹  

  57. fetchNextSiblings下一兄弟元素  

  58. fetchParents:父元素集  

  59. fetchPrevious:前一元素  

  60. fetchPreviousSiblings:前一兄弟元素:這幾個可以對當前元素的父級別元素和兄弟級別進行查找。  

  61. find:只找到limit爲1的結果  

  62. findAll  

  63. findAllNext  

  64. findAllPrevious  

  65. findChild  

  66. findChildren:子集合  

  67. findNext:下一元素  

  68. findNextSibling:下一個兄弟  

  69. findNextSiblings:下一羣兄弟  

  70. findParent:父元素  

  71. findParents:全部的父元素集合  

  72. findPrevious  

  73. findPreviousSibling  

  74. findPreviousSiblings:對當前元素和子元素進行遍歷查找。  

  75. find_all_next  

  76. find_all_previous  

  77. find_next  

  78. find_next_sibling  

  79. find_next_siblings  

  80. find_parent  

  81. find_parents  

  82. find_previous  

  83. find_previous_sibling  

  84. find_previous_siblings:這些下劃線方法命名是bs4方法,推薦使用這類  

  85. format_string  

  86. get  

  87. getText  

  88. get_text:獲得文檔標籤內的內容,不包括標籤和標籤屬性  

  89. handle_data  

  90. handle_endtag  

  91. handle_starttag  

  92. has_attr  

  93. has_key  

  94. index  

  95. insert  

  96. insert_after  

  97. insert_before:修改element tree  

  98. isSelfClosing  

  99. is_empty_element  

  100. new_string  

  101. new_tag  

  102. next  

  103. nextGenerator  

  104. nextSibling  

  105. nextSiblingGenerator  

  106. next_elements  

  107. next_siblings  

  108. object_was_parsed  

  109. parentGenerator  

  110. parents  

  111. parserClass  

  112. popTag  

  113. prettify:格式化HTML文檔  

  114. previous  

  115. previousGenerator  

  116. previousSibling  

  117. previousSiblingGenerator  

  118. previous_elements  

  119. previous_siblings  

  120. pushTag  

  121. recursiveChildGenerator  

  122. renderContents  

  123. replaceWith  

  124. replaceWithChildren  

  125. replace_with  

  126. replace_with_children:修改element tree 元素內容  

  127. reset  

  128. select:適用於jquery和css的語法選擇。  

  129. setup  

  130. string  

  131. strings  

  132. stripped_strings  

  133. tag_name_re  

  134. text  

  135. unwrap  

  136. wrap  

 

 

須要注意的是,在BeautifulSoup中的方法有些有兩種寫法,有些是駝峯格式的寫法,有些是下劃線格式的寫法,可是看其方法的含義是同樣的,這主要是BeautifulSoup爲了兼容3.x的寫法。前者是3.x的寫法,後者是4.x的寫法,推薦使用後者,也就是下劃線的方法。

 

根據這些方法,應該可以獲得遍歷、抽取、修改、規範化文檔的一系列方法。你們若是能在工做中使用BeautifulSoup ,必定會理解更深。

 

BeautifulSoup 支持不一樣的 parser ,默認是 Html 格式解析,還有 xml parser 、 lxml parser 、 html5lib parser 、html.parser ,這些 parser 都須要響應的解析器支持。

 html,這個是默認的解析器

Python代碼  收藏代碼

  1. BeautifulSoup("<a><b /></a>")  

  2. # <html><head></head><body><a><b></b></a></body></html>  

 

 xml格式解析器

Python代碼  收藏代碼

  1. BeautifulSoup("<a><b /></a>""xml")  

  2. # <?xml version="1.0" encoding="utf-8"?>  

  3. # <a><b /></a>  

 

 lxml格式解析器

Python代碼  收藏代碼

  1. BeautifulSoup("<a></p>""lxml")  

  2. # <html><body><a></a></body></html>  

 

 html5lib格式解析器

Python代碼  收藏代碼

  1. BeautifulSoup("<a></p>""html5lib")  

  2. # <html><head></head><body><a><p></p></a></body></html>  

 

 html.parser解析器

Python代碼  收藏代碼

  1. BeautifulSoup("<a></p>""html.parser")  

  2. # <a></a>  

 

 

其中 parser 的區別你們看下這幾個例子就知道了。

 

在使用 BeautifulSoup 解析文檔的時候,會將整個文檔以一顆大又密集的數據載入到內存中,若是你只是從數據結構中得到一個字符串,內存中保存一堆數據感受就不划算了。而且若是你要得到指向某個 Tag 的內容,這個 Tag又會指向其它的 Tag 對象,所以你須要保存這棵樹的全部部分,也就是說整棵樹都在內存中。 extract 方法能夠破壞掉這些連接,它會將樹的鏈接部分斷開,若是你獲得某個 Tag ,這個 Tag 的剩餘部分會離開這棵樹而被垃圾收集器捕獲;固然,你也能夠實現其它的功能:如文檔中的某一塊你自己就不關心,你能夠直接把它 extract 出樹結構,扔給垃圾收集器,優化內存使用的同時還能完成本身的功能。

 

正如 BeautifulSoup 的做者 Leonard 所說,寫 BeautifulSoup 是爲了幫助別人節省時間,減少工做量。一旦習慣使用上 BeautifulSoup 後,一些站點的內容很快就能搞定。這個就是開源的精神,將工做盡量的自動化,減少工做量;從某個程度上來講,程序員應該是比較懶惰的,可是這種懶惰正好又促進了軟件行業的進步。


導入模塊時按照原始博文http://isilic.iteye.com/blog/1741918 老是不對,我嘗試import bs4   from bs4 import beautifulsoup  就可一了

相關文章
相關標籤/搜索