天天看點

BeautifulSoup

寫文章之前先吐槽幾句:Python這個玩意哪都好,又簡單又直覺,對于我這種程式設計新人來說的确很不錯,但是python有緻命的坑點就是更新太快。現在比較流行的是2.7版本和3.5版本,偏偏2.7版本有些指令不支援在3.5裡,比如import sys,reload(sys),2.7可以直接使用,而3.5就不行。比如reduce指令,2.7可以直接使用,3.5就不行。我也是開了眼界,竟然還有進階版本不容納低級版本的!

但是很多人又說3.5是形勢所趨,可2.7又有很多地方很友善。現在網絡課程滿大街都是,很多老師上來就開始講python,講倒是無所謂,講了半天就是不講自己的浏覽器型号,也不講自己的python編譯器版本号,一個破pycharm,一年裡能更新好幾百次,每一次更新不但網頁變樣,裡面東西的位置也跟着變樣。對于初學者來說,特别容易蒙圈。

好,吐槽完畢,畢竟再怎麼吐槽人也得活着。那就克服困難吧。

BeautifulSoup子產品在成功安裝之後,使用from bs4 import BeautifulSoup啟動子產品。啟動完畢之後,就可以輸入想要搜尋的“内容”,BeautifulSoup主要是面對網頁檔案的,因為網頁的源代碼是一對一對出現的,BeautifulSoup能很快的正确定位。

假如,我們要搜尋的内容是:

html_doc="""<html>

 <head>

    <title>爬蟲測試</title>

 </head>

 <body>

  <div class="topic"><a href="www.51cto.com/welcome.html">歡迎來到這裡!</a>

    <div class="list">

     <url>

    <li><a href="http://www.51cto.com/1.html">這是第一頁</a></li>

    <li><a href="http://www.51cto.com/2.html">這是第二頁</a></li>

     <li><a href="http://www.51cto.com/3.html">這是第三頁</a></li>

     </url>

     </div>

   </div>

 </body>

</html>"""

輸入完畢之後,需要搞一個Soup的子產品;

soup=BeautifulSoup(html_doc,"html.parser",from_encoding="utf-8")

其中括号裡html_doc是“需要搜尋的範圍”,"html.parser"是用來解析的工具,from_encoding="utf-8"是代碼的格式。

那麼我觀察,他們都是處于<a href="blablabla">文字</a>這樣的樣式裡。那麼我們給所有的連結取一個變量名叫links。

link=soup.find_all("a")

這裡使用的是find_all而不是find,是因為find_all有點類似re.findall,刨根問底攔不住。而find是re.search,查到一個就收手了。

for each in BBB:

  print(each.name,each["href"],each.get_text())

這樣的輸出結果會是:

a www.51cto.com/welcome.html 歡迎來到這裡!

a http://www.51cto.com/1.html 這是第一頁

a http://www.51cto.com/2.html 這是第二頁

a http://www.51cto.com/3.html 這是第三頁

=================================分割線=====================================

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<code>from</code> <code>bs4 </code><code>import</code> <code>BeautifulSoup</code>

<code>AAA</code><code>=</code> <code>"""</code>

<code>&lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse's story&lt;/title&gt;&lt;/head&gt;</code>

<code>&lt;body&gt;</code>

<code>&lt;p class="title"&gt;&lt;b&gt;The Dormouse's story&lt;/b&gt;&lt;/p&gt;</code>

<code>&lt;p class="story"&gt;Once upon a time there were three little sisters; and their names were</code>

<code>&lt;a href="http://example.com/elsie" class="sister" id="link1"&gt;Elsie&lt;/a&gt;,</code>

<code>&lt;a href="http://example.com/lacie" class="sister" id="link2"&gt;Lacie&lt;/a&gt; and</code>

<code>&lt;a href="http://example.com/tillie" class="sister" id="link3"&gt;Tillie&lt;/a&gt;;</code>

<code>and they lived at the bottom of a well.&lt;/p&gt;</code>

<code>&lt;p class="story"&gt;...&lt;/p&gt;</code>

<code>"""</code>

<code>soup</code><code>=</code><code>BeautifulSoup(AAA,</code><code>"html.parser"</code><code>,from_encoding</code><code>=</code><code>"utf-8"</code><code>)</code>

<code>AAA</code><code>=</code><code>soup.find(</code><code>"a"</code><code>,href</code><code>=</code><code>"http://example.com/tillie"</code><code>)  </code>

<code>#通過&lt;a href="http://example.com/tillie"&gt;進行了精準的定位</code>

<code>print</code><code>(AAA.get_text())</code>

這個程式運作的結果就是 Tillie

================================分割線======================================

17

18

19

20

21

<code>AAA</code><code>=</code><code>"""&lt;html&gt;</code>

<code> </code><code>&lt;head&gt;</code>

<code>    </code><code>&lt;title&gt;爬蟲測試&lt;/title&gt;</code>

<code> </code><code>&lt;/head&gt;</code>

<code> </code><code>&lt;body&gt;</code>

<code>  </code><code>&lt;div class="topic"&gt;&lt;a href="www.51cto.com/welcome.html"&gt;歡迎來到這裡!&lt;/a&gt;</code>

<code>    </code><code>&lt;div class="list"&gt;</code>

<code>     </code><code>&lt;url&gt;</code>

<code>    </code><code>&lt;li&gt;&lt;a href="http://www.51cto.com/1.html"&gt;這是第一頁&lt;/a&gt;&lt;/li&gt;</code>

<code>    </code><code>&lt;li&gt;&lt;a href="http://www.51cto.com/2.html"&gt;這是第二頁&lt;/a&gt;&lt;/li&gt;</code>

<code>     </code><code>&lt;li&gt;&lt;a href="http://www.51cto.com/3.html"&gt;這是第三頁&lt;/a&gt;&lt;/li&gt;</code>

<code>     </code><code>&lt;/url&gt;</code>

<code>     </code><code>&lt;/div&gt;</code>

<code>   </code><code>&lt;/div&gt;</code>

<code> </code><code>&lt;/body&gt;</code>

<code>&lt;/html&gt;"""</code>

<code>BBB</code><code>=</code><code>soup.find_all(</code><code>"div"</code><code>)</code>

<code>for</code> <code>each </code><code>in</code> <code>BBB:</code>

<code>  </code><code>print</code><code>(each.name,each.get_text())</code>

這個程式跟第一個實驗結構一樣,但是卻将關鍵字"a"改成"div"。做一下試驗看看這個結果是什麼?

如果關鍵字再由"div"換成了"url",那麼結果又是什麼?對比一下關鍵字是"a"的時候,再對比一下關鍵字是"url"的時候,為何會有這樣的不同結果呢?

如果最後一句是 print(each.name,each["href"],each.get_text()),這個結果又是什麼?

 本文轉自 蘇幕遮618 51CTO部落格,原文連結:http://blog.51cto.com/chenx1242/1730367