keep-stupid

 

需求:抓取《纯洁心灵·逐梦演艺圈》的短评,并通过词云分析高频词语

(一)分析页面

 

1.首先访问《纯洁心灵·逐梦演艺圈》链接:

https://movie.douban.com/subject/26322774/comments?status=P

发现页面渲染并不是rest风格的request,在network中没有返回XHR格式的response,所以需要请求页面,解析html页面抓取数据

 

 

 2.查看短评在页面中的位置,位于class=\' comment-content\'的p标签的span标签的文本中(注意\' comment-content\'前有一个空格)

所以xpath表达式为:"//p[@class=\' comment-content\']/span"

 

 

 

 3.首页短评url为:https://movie.douban.com/subject/26322774/comments?status=P

后续每页短评url为:https://movie.douban.com/subject/26322774/comments?start=20&limit=20&status=P&sort=new_score 

所以抓取首页短评时需要特殊处理

 

(二)准备所需的python module

1)requests库

pip install requests

2)lxml库

pip install lxml

(三)代码


# encoding=utf-8

"""
豆瓣短评spider
"""
import os
import random
import sys
from time import sleep

import requests
from lxml import etree

reload(sys)
sys.setdefaultencoding(\'utf8\')

headers = {
\'User-Agent\': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36",
\'Host\': \'movie.douban.com\',
\'Connection\': \'keep-alive\',
\'Accept\': \'application/json, text/plain, */*\'
}


# 随机睡眠
def random_sleep():
t = random.randint(1, 3)
sleep(t)


def spider_page(url, xpath):
"""
根据xpath表达式解析url指定html页面
:param url:
:param xpath: xpath表达式
:return:
"""
random_sleep()
response = requests.request(method=\'GET\', url=url, headers=headers)
selector = etree.HTML(response.text)
return selector.xpath(xpath)


def spider_douban(subject, path):
"""
将短评写入指定路径文件
:param subject: 豆瓣subject id
:param path: 指定路径
:return:
"""
url = \'https://movie.douban.com/\'
url_fisrt_page = url + \'subject/\' + str(subject) + \'/comments?status=P\'

comment_list = spider_page(url_fisrt_page, "//p[@class=\' comment-content\']/span")
fd = os.open(path, os.O_RDWR | os.O_CREAT)

for data in comment_list:
os.write(fd, data.text) # 写入text
print \'第一页爬取完毕\'
try:
for i in xrange(0, 20):
print \'第\' + str(i + 2) + \'页写入中...\'
url_next_page = url + \'subject/\' + str(subject) + \'/comments?start=\' + str(
i * 20) + \'&limit=20&status=P&sort=new_score\'

comment_list = spider_page(url_next_page, "//p[@class=\' comment-content\']/span")
for comment in comment_list:
os.write(fd, comment.text) # 写入text
print \'写入数据条数:{}\'.format(len(comment_list))
except RuntimeError as e:
print e

os.close(fd)


if __name__ == \'__main__\':
spider_douban(26322774, \'/Users/gcl/tools/os/zhumeng.txt\')


output:

第1页写入中...
写入数据条数:20
第2页写入中...
写入数据条数:20
第3页写入中...
写入数据条数:20
第4页写入中...
写入数据条数:20
第5页写入中...
写入数据条数:20
第6页写入中...
写入数据条数:20
第7页写入中...
写入数据条数:20
第8页写入中...
写入数据条数:20
第9页写入中...
写入数据条数:20
第10页写入中...
写入数据条数:20
第11页写入中...
写入数据条数:20
第12页写入中...
写入数据条数:0
第13页写入中...
写入数据条数:0
第14页写入中...
写入数据条数:0
第15页写入中...

发现在13页之后,抓取的数据量变为0

 

访问url:https://movie.douban.com/subject/26322774/comments?start=500&limit=20&status=P&sort=new_score

显示

 

 

豆瓣的反爬虫机制:当爬取前几百条数据时,继续爬取时会提醒没有权限。

解决思路:模拟登陆豆瓣,豆瓣的验证码机制是滑动验证码,在本文中不涉及,后续再讨论

通过selenium解决

(四)词云分析

将抓取的数据放入第三方词云分析库

 

 

如有问题,欢迎讨论哦

 

分类:

技术点:

相关文章:

  • 2021-03-31
  • 2022-12-23
  • 2022-12-23
  • 2022-01-01
  • 2021-09-21
  • 2021-08-26
  • 2021-04-27
  • 2021-04-06
猜你喜欢
  • 2021-12-04
  • 2021-06-12
  • 2021-12-10
  • 2021-04-23
  • 2022-12-23
  • 2021-04-05
  • 2021-04-04
相关资源
相似解决方案