爬虫爬取豆瓣影院的相关电影信息
主要的是将相关的电影信息爬取到之后存储到excel中:
主要的代码入下:
import requests from bs4 import BeautifulSoup import openpyxl headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'} lst=['编号','名称','推荐语','评分','电影链接'] for i in range (10): url = 'https://movie.douban.com/top250?start='+str(i*25)+'&filter=' resp = requests.get(url, headers=headers) bs=BeautifulSoup(resp.text,'html.parser') grid_view=bs.find('ol',class_='grid_view')#里面的每个li表示一个影片数据 all_li=grid_view.find_all('li') #对其进行遍历 for item in all_li: no=item.find('em').text#电影的序号 title=item.find('span',class_='title').text#索取标题 inq=item.find('span',class_='inq')#推荐 rat=item.find('span',class_='rating_num')#评分 url_films=item.find('a')['href'] lst.append([no,title,inq.text if inq!=None else '',rat.text,url_films]) #存储到excel中 wb=openpyxl.Workbook() sheet=wb.active sheet.title="我的电影" for item in lst: sheet.append(item) wb.save('豆瓣电影')