爬豆瓣网分析《敦刻尔克》这部电影值得我们花钱去看吗(1)

ok,走起,我们先把网址贴出来 要爬的网址是:https://movie.douban.com/cinema/nowplaying/chengdu/

先把这个网页先爬下来

import urllib2
 
resq = urllib2.Request("https://movie.douban.com/cinema/nowplaying/chengdu/")
response = urllib2.urlopen(resq)
html_data = response.read()
print html_data

我们来看看打印出来是什么鬼东西先
这里写图片描述
OK,看title,应该是没问题了,但这只是一个关于html代码的字符串,我们下一步还需要对该字符串加工,提取出我们需要的数据
先去代码里把该电影相关的标签找到先,不多说,F12,搜索走起
这里写图片描述
活捉该电影位置了,走起,就先把这个div里面的东西先全部搞下来打印出来瞧瞧

from bs4 import BeautifulSoup as bs

soup = bs(html_data, 'html_parser')
nowplaying = soup.find_all('div',id = 'nowplaying')
print nowplaying[0].encode('utf8')

这里写图片描述

OK,要的东西就在这里面了,我们继续再去里面精简一下我们要的东西

nowplaying_list = nowplaying.find_all('li',class_='list-item')
nowplaying_result = []
for item in nowplaying_list:
	result = lambda x:(x['data-subject'].encode('utf8'),x['data-title'].encode('utf8'))
    nowplaying_list.append(result(item))
for i in nowplaying_list:
    print i[0],i[1]

看看都有哪些电影正在上映呢
这里写图片描述
OK,又搞定了一部,要到了id和电影名称,一对应,欧拉,我们要找的敦刻尔克的id是26607693,行,接着下一步走起!

我们先去评论的地址看看
https://movie.douban.com/subject/26607693/comments?start=0&limit=20&sort=new_score&status=P
发现subject后面跟的不就是我们的id么
OK,直接带入这个id,我们就能找到该电影相关的评论信息了

for i in nowplaying_list:
    if i[1] == u'敦刻尔克':
        movie_id = i[0]
url = 'https://movie.douban.com/subject/'+movie_id+'/comments?start=0&limit=20&sort=new_score&status=P'
comment_web = urllib2.Request(url)
comment_response = urllib2.urlopen(comment_web)
comment_data = comment_response.read()
comment_soup = bs(comment_data, 'html.parser')
comment_list = comment_soup.find_all('div', class_='comment')
result = []
for i in range(len(comment_list)):
    result.append((comment_list[i].find_all('p'))[0].text)
    print result[i]

OK,打印出来看看第一页的短评都是些啥玩意
这里写图片描述
还不错,正好是我们需要的东西,全在result这个list里面了
然而还有个问题出现了,这tm只有一页啊,这短评可是有好几万呢,怎么办,这个留到下一章再写了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值