from bs4 import BeautifulSoup
dic = {}
info = []
with open('./1_2_homework_required/index.html', 'r') as wb_data:
soup = BeautifulSoup(wb_data, 'lxml')
'''
body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > img
body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.caption > h4:nth-child(2) > a
body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.caption > h4.pull-right
body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.caption > p
body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.ratings > p:nth-child(2) > span:nth-child(1)
body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.ratings > p.pull-right
'''
images = soup.select('body > div > div > div.col-md-9 > div > div > div > img')
#print images
#images1 = soup.select('body > div.container > div.row > div.col-md-9 > div.row > div.col-sm-4.col-lg-4.col-md-4 > div.thumbnail > img')
#print images1
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')
#details = soup.select('body > div > div > div.col-md-9 > div > div > div > div.caption > p')
score_counts = 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:nth-of-type(2)')
#for image, title, price, detail, star, score_count in zip(images, titles, prices, details, stars, score_counts):
for image, title, price, star, score_count in zip(images, titles, prices, stars, score_counts):
dic = {
'image':image.get('src'),
'title':title.get_text(),
'price':price.get_text(),
#'detail':detail.get_text(),
'star':len(star.find_all(class_='glyphicon glyphicon-star')),
'score_count':score_count.get_text()
}
info.append(dic)
wb_data.close()
print info
总结
- 相对于css path,用html结构层级更适合我
- soup.select( )出来的是bs4.element.Tag类型,可使用soup对象的方法