** 学完第二课后的练习成果:**
'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)
心得体会:
做作业时最大的问题出在抓取的路径上,这节课的“星星数量”是抓取时候遇到的大问题,开始时抓取到p下面的span,后来发现原网页代码中所有的5个span都统一放在父节点p标签中,只需要抓取一个p:nth-type(2)就可以了。
熟悉网页元素是关键。还要继续努力。