用Beautiful Soup抓取數據的小例子

        這是以前的一個小例子,如今記下。愈來愈以爲學習編程開發,不寫筆記不寫博客簡直就是白學,因此如今把這些記下來。這個抓取的網站無需登陸。主要是想記住幾個bs函數的用法。
python

        代碼以下:
編程

        

import urllib2
import re
from BeautifulSoup import BeautifulSoup

url="http://www.realestate.com.au/neighbourhoods/brendale-4500-qld"
response=urllib2.urlopen(url)               #獲取網站源碼
data=response.read()
soup=BeautifulSoup(''.join(data))           #bs的用法,解析網站結構
a=soup.findAll('div',{'class':'slide-section median-price-subsections trend'},text=None)   #find及findAll 在bs中特別有用。能夠根據標籤和屬性找到相應目錄
b=a[0].get('data-trend')                    #解析到的結果是一個數組,用get方法能夠獲得每個條目的具體屬性值
print b
相關文章
相關標籤/搜索