Week1 Lesson2

###感想

那些星星着实让我头疼

###代码

frombs4importBeautifulSoup

path='/Users/Emma/Desktop/Python/Plan-for-combating-master/week1/1_2/1_2answer_of_homework/1_2_homework_required/index.html'

data=[]

withopen(path,'r')asweb_data:

soup=BeautifulSoup(web_data,'lxml')

pictures=soup.select('div.col-md-9 > div:nth-of-type(2) > div > div > img')

prices=soup.select('div.col-md-9 > div:nth-of-type(2) > div > div > div.caption > h4.pull-right')

reviews=soup.select('div.col-md-9 > div:nth-of-type(2) > div > div > div.ratings > p.pull-right')

stars=soup.select(' div.ratings > p:nth-of-type(2) > span')

titiles=soup.select('div.col-md-9 > div:nth-of-type(2) > div > div > div.caption > h4:nth-of-type(2) > a')

stars_list=[]

forstarinstars:

star=str(star)

stars_list.append(star)

stars_list=[star_list.replace('','★')forstar_listinstars_list]

stars_list=[star_list.replace('','☆')forstar_listinstars_list]

rates=[]

whilestars_list!=[]:

rate=stars_list[0:5]

rates.append(rate)

delstars_list[0:5]

forpic,price,review,rate,titileinzip(pictures,prices,reviews,rates,titiles):

info={

'pic':pic.get('src'),

'price':price.get_text(),

'review':review.get_text(),

'title':titile.get_text(),

'rate':rate

}

data.append(info)

print(data)

###总结

- 用string.replace for string in list替换改变list中的元素

你可能感兴趣的:(Week1 Lesson2)