Python BeautifulSoup4 使用指南

前言:

 

昨天把傳說中的BeautifulSoup4裝上了,還沒有裝好的童鞋,請看本人的上一篇博客:

Python3 Win7安裝 BeautifulSoup,按照裏面簡單的步驟就可以把BeautifulSoup裝上啦,很簡單的,表害怕

 

裝好BeautifulSoup4之後,就讓我們來好好享受這碗BeautifulSoup吧,哈哈

 

入門:

 

下面就來介紹一下BeautifulSoup吧,BeautifulSoup是一個可以從HTML或XML文件中提取數據的Python庫.它能夠通過你喜歡的轉換器實現慣用的文檔導航,查找,修改文檔的方式.Beautiful Soup會幫你節省數小時甚至數天的工作時間

 

在你知道有BeautifulSoup這貨之前,如果你從一個文本中提取出自己想要的東西,那麼估計你應該使用re模塊,但先在如果給你一個HTML文件讓你提取出一些關鍵信息,如果再使用re模塊,雖然也可以把信息提取出來,但是捏,你可能會絞盡腦汁苦思冥想查閱好多資料,現在,我們有了BeautifulSoup,一切變得超級簡單起來,對,就是這麼簡單

 

實踐:

 

學習bs4最好的還是查看bs4的官方文檔,有中文版的哦,猛點這裏官方文檔,看起來會很快,筆者花了大概一個下午的時間,把bs4的官方文檔看了一遍,順手也寫了寫裏面的示例程序,如果你沒多少時間的話,看看我下面的代碼,估計你會很快上手的,相信我 (*^_^*) 


__author__ = 'MrChen'

from bs4 import BeautifulSoup
#這是示例
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>
"""
#初始化,實例化一個BeautifulSoup對象,參數可以是一個字符串,也可以是一個打開的文件比如open('mydoc.html')
soup = BeautifulSoup(html_doc)

print(soup.title)
#輸出:<title>The Dormouse's story</title>

print(soup.title.parent)
#輸出:<head><title>The Dormouse's story</title></head>

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

print(soup.title.name)
#輸出:title

print(soup.title.parent.name)
#輸出:head

print(soup.title.parent.parent.name)
#輸出:html

print(soup.p)
#輸出:<p class="title"><b>The Dormouse's story</b></p>

print(soup.p['class'])
#輸出:['title']

print(soup.a)
#輸出:<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

print(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>]

print(soup.find(id = 'link3'))
#輸出:<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

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

print(soup.getText())
#輸出:
# 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.
# ...

print('all tags : <<<<<<')
for tag in soup.find_all(True):
    print(tag.name)
#輸出:
#html
#head
#title
#body
#p
#b
#p
#a
#a
#a
#p






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章