python爬取豆瓣电影250_Python 爬取豆瓣TOP250实战

学习爬虫之路,必经的一个小项目就是爬取豆瓣的TOP250了,首先我们进入TOP250的界面看看。

1785492-20191025190520780-812860019.png

可以看到每部电影都有比较全面的简介。其中包括电影名、导演、评分等。

接下来,我们就爬取这些数据,并将这些数据制成EXCEL表格方便查看。

首先,我们用requests库请求一下该网页,并返回他的text格式。

1785492-20191025191512651-160011872.png

请求并返回成功!

接下来,我们提取我们所需要的网页元素。

点击“肖申克救赎”的检查元素。

1785492-20191025191802808-1981790321.png

发现它在div class = "hd" -> span class = "title"里,所以我们import beautifulsoup,来定位该元素。

同时,用相同的方法定位电影的评价人数和评分以及短评。

代码如下:

soup = BeautifulSoup(res.text, 'html.parser')

names=[]

scores=[]

comments=[]

result=[]#获取电影的所有名字

res_name = soup.find_all('div',class_="hd")for i inres_name:

a=i.a.span.text

names.append(a)#获取电影的评分

res_scores = soup.find_all('span',class_='rating_num')for i inres_scores:

a=i.get_text()

scores.append(a)#获取电影的短评

ol = soup.find('ol', class_='grid_view')for i in ol.find_all('li'):

info= i.find('span', attrs={'class': 'inq'}) #短评

ifinfo:

comments.append(info.get_text())else:

comments.append("无")return names,scores,comments

Ok,现在,我们所需要的数据都存在三个列表里面,names,scores,comments。

我们将这三个列表存入EXCEL文件里,方便查看。

调用WorkBook方法

wb =Workbook()

filename= 'top250.xlsx'ws1=wb.active

ws1.title= 'TOP250'

for (i, m, o) inzip(names,scores,comments):

col_A= 'A%s' % (names.index(i) + 1)

col_B= 'B%s' % (names.index(i) + 1)

col_C= 'C%s' % (names.index(i) + 1)

ws1[col_A]=i

ws1[col_B]=m

ws1[col_C]=o

wb.save(filename=filename)

运行结束后,会生成一个.xlsx的文件,我们来看看效果:

1785492-20191025192249318-1204306707.png

Very Beatuful! 以后想学习之余想放松一下看看好的电影,就可以在上面直接查找啦。

以下是我的源代码:

importrequestsfrom bs4 importBeautifulSoupfrom openpyxl importWorkbookdefopen_url(url):

res=requests.get(url)returnresdefget_movie(res):

soup= BeautifulSoup(res.text, 'html.parser')

names=[]

scores=[]

comments=[]

result=[]#获取电影的所有名字

res_name = soup.find_all('div',class_="hd")for i inres_name:

a=i.a.span.text

names.append(a)#获取电影的评分

res_scores = soup.find_all('span',class_='rating_num')for i inres_scores:

a=i.get_text()

scores.append(a)#获取电影的短评

ol = soup.find('ol', class_='grid_view')for i in ol.find_all('li'):

info= i.find('span', attrs={'class': 'inq'}) #短评

ifinfo:

comments.append(info.get_text())else:

comments.append("无")returnnames,scores,commentsdefget_page(res):

soup= BeautifulSoup(res.text,'html.parser')#获取页数

page_num = soup.find('span',class_ ='next').previous_sibling.previous_sibling.textreturnint(page_num)defmain():

host= 'https://movie.douban.com/top250'res=open_url(host)

pages=get_page(res)#print(pages)

names =[]

scores=[]

comments=[]for i inrange(pages):

url= host + '?start='+ str(25*i)+'&filter='

#print(url)

result =open_url(url)#print(result)

a,b,c =get_movie(result)#print(a,b,c)

names.extend(a)

scores.extend(b)

comments.extend(c)#print(names)

#print(scores)

#print(comments)

wb =Workbook()

filename= 'top250.xlsx'ws1=wb.active

ws1.title= 'TOP250'

for (i, m, o) inzip(names,scores,comments):

col_A= 'A%s' % (names.index(i) + 1)

col_B= 'B%s' % (names.index(i) + 1)

col_C= 'C%s' % (names.index(i) + 1)

ws1[col_A]=i

ws1[col_B]=m

ws1[col_C]=o

wb.save(filename=filename)if __name__ == '__main__':

main()

生成EXCEL文件还有很多种方法,下次分享Pandas生成EXCEL文件的方法~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值