** 學(xué)完第二課后的練習(xí)成果:**
'star': 5, 'review': '65 reviews', 'price': '$24.99', 'title': 'EarPod', 'image': 'img/pic_0000_073a9256d9624c92a05dc680fc28865f.jpg'}
{'star': 4, 'review': '12 reviews', 'price': '$64.99', 'title': 'New Pocket', 'image': 'img/pic_0005_828148335519990171_c234285520ff.jpg'}
{'star': 4, 'review': '31 reviews', 'price': '$74.99', 'title': 'New sunglasses', 'image': 'img/pic_0006_949802399717918904_339a16e02268.jpg'}
{'star': 3, 'review': '6 reviews', 'price': '$84.99', 'title': 'Art Cup', 'image': 'img/pic_0008_975641865984412951_ade7a767cfc8.jpg'}
{'star': 4, 'review': '18 reviews', 'price': '$94.99', 'title': 'iphone gamepad', 'image': 'img/pic_0001_160243060888837960_1c3bcd26f5fe.jpg'}
{'star': 4, 'review': '18 reviews', 'price': '$214.5', 'title': 'Best Bed', 'image': 'img/pic_0002_556261037783915561_bf22b24b9e4e.jpg'}
{'star': 4, 'review': '35 reviews', 'price': '$500', 'title': 'iWatch', 'image': 'img/pic_0011_1032030741401174813_4e43d182fce7.jpg'}
{'star': 4, 'review': '8 reviews', 'price': '$15.5', 'title': 'Park tickets', 'image': 'img/pic_0010_1027323963916688311_09cc2d7648d9.jpg'}
CODE:
#!/usr/bin/env python
#-- coding: utf-8 --
from bs4 import BeautifulSoup
path = 'Q:/test/Plan-for-combating-master/Plan-for-combating- master/week1/1_2/1_2answer_of_homework/index.html'
with open(path, 'r') as wb_data:
content = wb_data.read()
soup = BeautifulSoup(content, "html5lib")
titles = soup.select("body > div > div > div.col-md-9 > div > div > div > div.caption > h4 > a")
images = soup.select("body > div > div > div.col-md-9 > div > div > div > img")
reviews = soup.select("body > div > div > div.col-md-9 > div > div > div > div.ratings > p.pull-right")
prices = soup.select("body > div > div > div.col-md-9 > div > div > div > div.caption > h4.pull-right")
stars = soup.select("body > div > div > div.col-md-9 > div > div > div > div.ratings > p:nth-of-type(2)")
#print(stars)
stars_of_number = []
infos = []
for title, image, review, price, star in zip(titles, images, reviews, prices, stars):
#print(title, image, review, price, str(star))
info = {
'title':title.get_text(),
'image':image.get('src'),
'review':review.get_text(),
'price':price.get_text(),
'star':len(star.find_all('span','glyphicon glyphicon-star'))
}
print(info)
心得體會(huì):
做作業(yè)時(shí)最大的問(wèn)題出在抓取的路徑上废境,這節(jié)課的“星星數(shù)量”是抓取時(shí)候遇到的大問(wèn)題雨女,開(kāi)始時(shí)抓取到p下面的span,后來(lái)發(fā)現(xiàn)原網(wǎng)頁(yè)代碼中所有的5個(gè)span都統(tǒng)一放在父節(jié)點(diǎn)p標(biāo)簽中潜支,只需要抓取一個(gè)p:nth-type(2)就可以了。
熟悉網(wǎng)頁(yè)元素是關(guān)鍵崇棠。還要繼續(xù)努力买优。