结果图###
result.png代码###
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
star_alls = []
star_all = []
infos = []
num = 0
path = 'D:/Users/Jessy zhou/Plan-for-combating-master/1_week/1.2 web-element/1.2homework/webpage/index.html'
def getText(item):
return '' if item == 0 else ''.join(item.get_text())
def getSrc(item):
return '' if item == 0 else ''.join(item.get('src'))
with open(path, 'r') as web_data:
soup = BeautifulSoup(web_data.read(), 'html.parser')
imgs = soup.select('div.thumbnail > img')
titles = soup.select('div.caption > h4 > a')
descs = soup.select('div.caption > p')
prices = soup.select('div.caption > h4.pull-right')
reviews = soup.select('div.ratings > p.pull-right')
# stars =soup.select('div.ratings > p:nth-of-type(2)')
stars = soup.find_all('span', 'glyphicon')
for star in stars:
span = str(star)
if 'empty' in span:
span_star = span.replace('<span class="glyphicon glyphicon-star-empty"></span>', u'☆')
else:
span_star = span.replace('<span class="glyphicon glyphicon-star"></span>', u'★')
star_alls.append(span_star)
star_alls = star_alls[2:]
limit = len(star_alls) / 5
while num < limit:
star_all.append(star_alls[num * 5:(num + 1) * 5])
num += 1
for img, title, desc, price, review, star_c in zip(imgs, titles, descs, prices, reviews, star_all):
data = {
'img': getSrc(img),
'title': getText(title),
'desc': getText(desc),
'price': getText(price),
'review': getText(review),
'star': star_c
}
infos.append(data)
总结###
1.为了显示★,需要加u以显示
2.find_all的方法也不是非常的简单,而且replace的字符串过长,应该可以得到简化
网友评论