有时候一部电影比一本书更能激励人。如果能拿到很高的分数,就说明这部电影得到了全球观众的认可,当生活遇到迷茫的时候,一部高分的电影可以帮助人们走出困境。为了防止大家家里缺剧,小编用python爬取了豆瓣的豆瓣电影排行榜TOP250。首先让我们输入网址:https://movie.douban.com/top2...然后我们分析一下链接的规则:https://movie.douban.com/top2……https://movie.douban.com/top2…………https://movie.douban.com/top2……综上,起始值为0,25,50...225,可以看出该值的步长为25,最大值为225。找到链接的规律后,我们来查看元素的获取。同样的F12评论元素,从图中我们可以看出里面的每部电影都被ol标签下的li标签包裹起来,我们可以直接获取li的值,获取标签如下:ol=soup.find('ol')li=ol.find_all('li')接下来让我们进入编码部分:']num=0num1=0lst=[]name_lst=[]dy_lst=[]zy_lst=[]time_lst=[]country_lst=[]leixing_lst=[]pj_lst=[]people_lst=[]quote_lst=[]headers={"接受":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","User-Agent":"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/81.0.4044.129Safari/537.36Edg/81.0.416.68",}whilenum<=225:url='https://movie.douban.com/top250?start='+str(num)+'&filter='withrequests.get(url=url,headers=headers)asr:ifr.status_code==200:r.encoding=r.apparent_encodingsoup=BeautifulSoup(r.text,'html.parser')ol=soup.find('ol')li=ol.find_all('li')foriinli:name=i.find('span').textp=i.find('p')p=str(p).split('
',1)fst=p[0].split('>',1)[1]sec=p[1].split('<',1)[0]daoyan=fst.split('主',1)[0]daoyan=daoyan.replace('导演:','')daoyan=daoyan.replace('\xa0','')daoyan=daoyan.replace('\n','')try:zhuyan=fst.split('主角:',1)[1]除了:zhuyan=''time=sec.split('/',2)[0]time=time.replace('\xa0','')time=time.replace('\n','')time=re.findall(r'\d{4}',time)[-1]country=sec.split('\xa0/\xa0',2)[1]country=country.replace('\xa0','')类型=sec.split('\xa0/\xa0',2)[2]类型=type.replace('\xa0','')type=type.replace('\n','')star=i.find('div',attrs={'class':"star"})span=star.find_all('span')pingjia=span[1].textpeople=span[3].text.split('评价',1)[0]try:quote=i.find('p',attrs={'class':"quote"}).textquote=quote.replace('\n','')except:quote=''name_lst.append(name)dy_lst.append(daoyan)zy_lst.append(zhuyan)time_lst.append(time)country_lst.append(country)leixing_lst.append(type)pj_lst.append(pingjia)people_lst.append(people)quote_lst.append(quote)num1+=1print('第{}页抓取完成!'.format(num1))ifnum==225:print('爬行结束,开始写入excel..')paiming=list(range(1,251))lst.追加(支付)lst。附加(name_lst)lst。追加(dy_lst)lst。追加(zy_lst)lst。追加(time_lst)lst。append(country_lst)lst.append(leixing_lst)lst.append(pj_lst)lst.append(people_lst)lst.append(quote_lst)head=['排名','电影名称','导演','主演','年份','region','Type','Rating','NumberofReviewers','ABriefIntroduction']ws.append(head)foriinrange(len(lst)):forjinrange(len(lst[0])):ws.cell(j+2,i+1).value=lst[i][j]print('写入excel完成!')forcellinws['1']:cell.alignment=Alignment(horizo??ntal='center',vertical='center')forcellinws['A']:cell.alignment=Alignment(horizo??ntal='center',vertical='center')对于ws['B']中的单元格:cell.alignment=Alignment(horizo??ntal='center',vertical='center')对于ws['E']中的单元格:cell.alignment=Alignment(horizo??ntal='center',vertical='center')对于ws['H']中的单元格:cell.alignment=Alignment(horizo??ntal='center',vertical='center')forcellinws['I']:cell.alignment=Alignment(horizo??ntal='center',vertical='center')ws.column_dimensions['B'].width=25ws.column_dimensions['I'].width=13wb.save('豆瓣电影top250.xlsx')num+=25else:print('失败!')if__name__=='__main__':print('开始爬行!')top250()右键运行代码执行即可。当前文件夹下会生成一个豆瓣电影top250的xlsx文件,至此所有电影信息爬取成功。下图代表程序运行成功。以上图片就是我今天要和大家分享的。
