Python 實戰(zhàn):week1 爬取商品信息

輸出結(jié)果:

每個商品的信息存入字典,使用列表保存所有商品信息

$ python 1.2.py
[{'star_num': u'65 reviews', 'price': u'$24.99', 'star_level': 5, 'img': 'img/pic_0000_073a9256d9624c92a05dc680fc28865f.jpg', 'title': u'EarPod'}, {'star_num': u'12 reviews', 'price': u'$64.99', 'star_level': 4, 'img': 'img/pic_0005_828148335519990171_c234285520ff.jpg', 'title': u'New Pocket'}, {'star_num': u'31 reviews', 'price': u'$74.99', 'star_level': 4, 'img': 'img/pic_0006_949802399717918904_339a16e02268.jpg', 'title': u'New sunglasses'}, {'star_num': u'6 reviews', 'price': u'$84.99', 'star_level': 3, 'img': 'img/pic_0008_975641865984412951_ade7a767cfc8.jpg', 'title': u'Art Cup'}, {'star_num': u'18 reviews', 'price': u'$94.99', 'star_level': 4, 'img': 'img/pic_0001_160243060888837960_1c3bcd26f5fe.jpg', 'title': u'iphone gamepad'}, {'star_num': u'18 reviews', 'price': u'$214.5', 'star_level': 4, 'img': 'img/pic_0002_556261037783915561_bf22b24b9e4e.jpg', 'title': u'Best Bed'}, {'star_num': u'35 reviews', 'price': u'$500', 'star_level': 4, 'img': 'img/pic_0011_1032030741401174813_4e43d182fce7.jpg', 'title': u'iWatch'}, {'star_num': u'8 reviews', 'price': u'$15.5', 'star_level': 4, 'img': 'img/pic_0010_1027323963916688311_09cc2d7648d9.jpg', 'title': u'Park tickets'}]

作業(yè) github 地址

功能,獲取:

  • 圖片地址
  • 價格
  • 商品標(biāo)題
  • 評分量
  • 評分星級

小結(jié)

--

安裝第三方庫

系統(tǒng):MAC OS 10.10.5

  • beautifulsoup4

網(wǎng)頁解析工具

pip install beautifulsoup4
  • requests

HTTP 請求工具

pip install requests
  • lxml

HTML 頁面解析器

需要先安裝C語言庫:

 xcode-select --install
pip install lxml

BeautifulSoup

需要被解析的 HTML 文件

可以通過 open 打開本地的 HTML 文件,作為參數(shù)傳遞給 BeautifulSoup

soup = BeautifulSoup(open("index.html"), 'lxml')

也可以先通過web_data = requests.get(url)向一個 url 發(fā)送 HTTP 請求,獲得HTTP 響應(yīng),然后將響應(yīng)中的 HTML 頁面內(nèi)容web_data.text傳遞給 Beautifulsoup

soup = Beautifulsoup(html,'lxml')

'lxml'是解析器,Beautifulsoup 使用解析器對 HTML 頁面進(jìn)行結(jié)構(gòu)化,用于后續(xù)的處理。

如果在使用 BeautifulSoup 解析時,沒有指定解析器,BeautifulSoup會自動查找并使用當(dāng)前系統(tǒng)中最合適的。

$ python 1.2.py
/lib/python2.7/site-packages/bs4/__init__.py:166: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

To get rid of this warning, change this:

 BeautifulSoup([your markup])

to this:

 BeautifulSoup([your markup], "lxml")

解析器有五種:lxml,html.parser,lxml HTML,lxml XML,html5lib

幾種解析器的比較

Beautiful Soup 4.2.0 文檔

BeautifulSoup 學(xué)習(xí)筆記

BeautifulSoup 的選擇器得到的是列表

.find_all

抓取星級的時候,先直接定位到span

stars = soup.select("div.ratings > p > span")
print stars

這樣打印出來的列表,包括了<span class="glyphicon glyphicon-star-empty"></span>(空的星星)和<span class="glyphicon glyphicon-star"></span>(實的星星)

于是按照作業(yè)提示,使用find_all

find_all( name , attrs , recursive , text , **kwargs )

元素名: "span",屬性: "glyphicon glyphicon-star",遞歸,內(nèi)容

篩選出了所有的實心星星

stars = soup.find_all("span", "glyphicon glyphicon-star")

<span class="glyphicon glyphicon-star"></span>

但是這樣,所有商品的星級都在一個列表中,為了對每個商品的星星單獨(dú)用列表計算,先獲取星級的父級元素

多對一的關(guān)系:想獲取一個層級的所有元素,應(yīng)該在父級元素就停下來

parents = soup.select("div.ratings")

使用.find_all獲取一個父級標(biāo)簽下所有子標(biāo)簽內(nèi)的文本信息,是處理多個文本的高級的.get_text()

star_level = parent.find_all("span", "glyphicon glyphicon-star")

可以獲取父元素下span元素 屬性為"glyphicon glyphicon-star"的列表

[<span class="glyphicon glyphicon-star"></span>, <span class="glyphicon glyphicon-star"></span>, <span class="glyphicon glyphicon-star"></span>, <span class="glyphicon glyphicon-star"></span>]

計算列表長度即可

len(star_level)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容