程序運(yùn)行結(jié)果
from bs4 import BeautifulSoup
with open('./1_2_homework_required/index.html', 'r') as f:
soup = BeautifulSoup(f, 'lxml')
pics = soup.select('div.col-md-9 > div > div > div > img')
titles = soup.select('h4 a')
prices = soup.select('h4.pull-right')
views = soup.select('div.ratings p.pull-right')
starts = soup.select('div.ratings > p:nth-of-type(2)')
for pic, title, price, view, start in zip(pics, titles, prices, views, starts):
data = {
# 注意不要丟掉前面pic title 等鍵的引號(hào)
'pic': pic.get('src'),
'title': title.get_text(),
'price': price.get_text(),
'view': view.get_text(),
#find_all(name, attrs, recursive, text, **kwargs)
# names:標(biāo)簽名字躏吊;attrs:標(biāo)簽屬性
'start': len(start.find_all('span','glyphicon glyphicon-star'))
}
print(data)