python爬蟲---beautifulsoup(1)

來源:互聯網
上載者:User

標籤:sub   val   代碼   print   code   parser   text   代碼執行   rom   

  beautifulsoup是用於對爬下來的內容進行解析的工具,其find和find_all方法都很有用。並且按照其解析完之後,會形成樹狀結構,對於網頁形成了類似於json格式的key - value這種樣子,更容易並且更方便對於網頁的內容進行操作。

  

  下載庫就不用多說,使用python的pip,直接在cmd裡面執行pip install beautifulsoup即可

  

  首先仿照其文檔說明,講代碼拷貝過來,如下

from bs4 import BeautifulSouphtml_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>"""soup = BeautifulSoup(html_doc,‘html.parser‘)print soup.find_all(‘a‘)

  html_doc即是我們爬下來的東西,這裡方便直接使用了文檔裡面提供的內容。

  我們直接對html_doc執行解析,使用的是html.parser這個解析器。

  在sublime敲完之後ctrl+B即可運行(推薦下載python的SublimePythonIDE這個外掛程式包,可以直接編譯無需使用cmd)

  

[<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>][Finished in 0.2s]

  代碼執行結果如上,將帶有a的行數執行出來了。

  我們按照文檔要求改寫一下,改寫soup的內容,並且答應出結果。(直接黏貼官網內容,不在重複)

  

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.p[‘class‘]# u‘title‘soup.a# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>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>]soup.find(id="link3")# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

  如上,可以很明顯的看出來,解析完畢的soup,形成了key-value格式的資料,使用soup.title等方法可以分別列印出需要的內容。(#為打出內容)

  還有其他的一些方法。

for link in soup.find_all(‘a‘):    print(link.get(‘href‘))# http://example.com/elsie# http://example.com/lacie# http://example.com/tillie

  使用foreach即可很輕鬆的對於複雜父容器的子控制項進行操作。(#為打出內容)

 

  官網最後一個內容是將該網頁的所有的內容去掉符號直接顯示內容。方法如下

  

print(soup.get_text())# The Dormouse‘s story## The Dormouse‘s story## Once upon a time there were three little sisters; and their names were# Elsie,# Lacie and# Tillie;# and they lived at the bottom of a well.## ...

  也很方便的直接把文本的內容打出來了。

 

  以上為beautifulsoup的比較簡單的使用。

python爬蟲---beautifulsoup(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.