Week1-2 的代码
from bs4 import BeautifulSoup
with open('C://personal/study/Python/Plan-for-combating-master/Plan-for-combating-master/week1/1_2/1_2answer_of_homework/index.html','r') as wb_data:
Soup = BeautifulSoup(wb_data,'lxml')
images = Soup.select("body > div > div > div.col-md-9 > div > div > div > img")
titles = Soup.select('body > div > div > div.col-md-9 > div > div > div > div.caption > h4 > a')
prices = Soup.select('body > div > div > div.col-md-9 > div > div > div > div.caption > h4.pull-right')
reviews = Soup.select("body > div > div > div.col-md-9 > div > div > div > div.ratings > p.pull-right")
stars = Soup.select('body > div > div > div.col-md-9 > div > div > div > div.ratings > p > span')
# print(images,titles,prices,reviews,stars,sep='\n-----------------\n')
for image,title,price,review,star in zip(images,titles,prices,reviews,stars):
data = {
'title':title.get_text(),
'image':image.get('src'),
'price':price.get_text(),
'review':review.get_text(),
'star':len(star.find_all('span','glyphicon glyphicon-star'))
}
print(data)
输出如下,star 都是0,请老师点评。
{'image': 'img/pic_0000_073a9256d9624c92a05dc680fc28865f.jpg', 'review': '65 reviews', 'price': '$24.99', 'title': 'EarPod', 'star': 0}
{'image': 'img/pic_0005_828148335519990171_c234285520ff.jpg', 'review': '12 reviews', 'price': '$64.99', 'title': 'New Pocket', 'star': 0}
{'image': 'img/pic_0006_949802399717918904_339a16e02268.jpg', 'review': '31 reviews', 'price': '$74.99', 'title': 'New sunglasses', 'star': 0}
{'image': 'img/pic_0008_975641865984412951_ade7a767cfc8.jpg', 'review': '6 reviews', 'price': '$84.99', 'title': 'Art Cup', 'star': 0}
{'image': 'img/pic_0001_160243060888837960_1c3bcd26f5fe.jpg', 'review': '18 reviews', 'price': '$94.99', 'title': 'iphone gamepad', 'star': 0}
{'image': 'img/pic_0002_556261037783915561_bf22b24b9e4e.jpg', 'review': '18 reviews', 'price': '$214.5', 'title': 'Best Bed', 'star': 0}
{'image': 'img/pic_0011_1032030741401174813_4e43d182fce7.jpg', 'review': '35 reviews', 'price': '$500', 'title': 'iWatch', 'star': 0}
{'image': 'img/pic_0010_1027323963916688311_09cc2d7648d9.jpg', 'review': '8 reviews', 'price': '$15.5', 'title': 'Park tickets', 'star': 0}
Process finished with exit code 0
网友评论