1. 海王评论数据爬取前分析

海王上映了,然后口碑炸了,对咱来说,多了一个可爬可分析的电影,美哉~

摘录一个评论

零点场刚看完,温导的电影一直很不错,无论是速7,电锯惊魂还是招魂都很棒。打斗和音效方面没话说非常棒,特别震撼。总之,DC扳回一分( ̄▽ ̄)。比正义联盟好的不止一点半点(我个人感觉)。还有艾梅伯希尔德是真的漂亮,温导选的人都很棒。
真的第一次看到这么牛逼的电影 转场特效都吊炸天

2. 海王案例开始爬取数据

数据爬取的依旧是猫眼的评论,这部分内容咱们用把牛刀,scrapy爬取,一般情况下,用一下requests就好了

抓取地址、交流群:1029344413 分享视频资料

http://m.maoyan.com/mmdb/comments/movie/249342.json?_v_=yes&offset=15&startTime=2018-12-11%2009%3A58%3A43

关键参数

url:http://m.maoyan.com/mmdb/comments/movie/249342.json
offset:15
startTime:起始时间

scrapy 爬取猫眼代码特别简单,我分开几个py文件即可。Haiwang.py

 

import scrapy
import json
from haiwang.items import HaiwangItem class HaiwangSpider(scrapy.Spider):
name = 'Haiwang'
allowed_domains = ['m.maoyan.com']
start_urls = ['http://m.maoyan.com/mmdb/comments/movie/249342.json?_v_=yes&offset=0&startTime=0'] def parse(self, response):
print(response.url)
body_data = response.body_as_unicode() js_data = json.loads(body_data)
item = HaiwangItem()
for info in js_data["cmts"]: item["nickName"] = info["nickName"]
item["cityName"] = info["cityName"] if "cityName" in info else ""
item["content"] = info["content"]
item["score"] = info["score"]
item["startTime"] = info["startTime"]
item["approve"] = info["approve"]
item["reply"] = info["reply"]
item["avatarurl"] = info["avatarurl"] yield item yield scrapy.Request("http://m.maoyan.com/mmdb/comments/movie/249342.json?_v_=yes&offset=0&startTime={}".format(item["startTime"]),callback=self.parse)

setting.py

设置需要配置headers

DEFAULT_REQUEST_HEADERS = {
"Referer":"http://m.maoyan.com/movie/249342/comments?_v_=yes",
"User-Agent":"Mozilla/5.0 Chrome/63.0.3239.26 Mobile Safari/537.36",
"X-Requested-With":"superagent"
}

需要配置一些抓取条件

# Obey robots.txt rules
ROBOTSTXT_OBEY = False
# See also autothrottle settings and docs
DOWNLOAD_DELAY = 1
# Disable cookies (enabled by default)
COOKIES_ENABLED = False

开启管道

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'haiwang.pipelines.HaiwangPipeline': 300,
}

items.py
获取你想要的数据

import scrapy

class HaiwangItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
nickName = scrapy.Field()
cityName = scrapy.Field()
content = scrapy.Field()
score = scrapy.Field()
startTime = scrapy.Field()
approve = scrapy.Field()
reply =scrapy.Field()
avatarurl = scrapy.Field()

pipelines.py
保存数据,数据存储到csv文件中

import os
import csv class HaiwangPipeline(object):
def __init__(self):
store_file = os.path.dirname(__file__) + '/spiders/haiwang.csv'
self.file = open(store_file, "a+", newline="", encoding="utf-8")
self.writer = csv.writer(self.file) def process_item(self, item, spider):
try:
self.writer.writerow((
item["nickName"],
item["cityName"],
item["content"],
item["approve"],
item["reply"],
item["startTime"],
item["avatarurl"],
item["score"]
)) except Exception as e:
print(e.args) def close_spider(self, spider):
self.file.close()

begin.py
编写运行脚本

from scrapy import cmdline
cmdline.execute(("scrapy crawl Haiwang").split())

搞定,等着数据来到,就可以了

Python爬虫入门教程 33-100 电影评论数据抓取 scrapy的更多相关文章

  1. Python爬虫入门教程 31-100 36氪(36kr)数据抓取 scrapy

    1. 36氪(36kr)数据----写在前面 今天抓取一个新闻媒体,36kr的文章内容,也是为后面的数据分析做相应的准备的,预计在12月底,爬虫大概写到50篇案例的时刻,将会迎来一个新的内容,系统的数 ...

  2. Python爬虫入门教程 30-100 高考派大学数据抓取 scrapy

    1. 高考派大学数据----写在前面 终于写到了scrapy爬虫框架了,这个框架可以说是python爬虫框架里面出镜率最高的一个了,我们接下来重点研究一下它的使用规则. 安装过程自己百度一下,就能找到 ...

  3. Python爬虫入门教程石家庄链家租房数据抓取

    1. 写在前面 这篇博客爬取了链家网的租房信息,爬取到的数据在后面的博客中可以作为一些数据分析的素材.我们需要爬取的网址为:https://sjz.lianjia.com/zufang/ 2. 分析网 ...

  4. Python爬虫入门教程 22-100 CSDN学院课程数据抓取

    1. CSDN学院课程数据-写在前面 今天又要抓取一个网站了,选择恐惧症使得我不知道该拿谁下手,找来找去,算了,还是抓取CSDN学院吧,CSDN学院的网站为 https://edu.csdn.net/ ...

  5. Python爬虫入门教程 20-100 慕课网免费课程抓取

    写在前面 美好的一天又开始了,今天咱继续爬取IT在线教育类网站,慕课网,这个平台的数据量并不是很多,所以爬取起来还是比较简单的 准备爬取 打开我们要爬取的页面,寻找分页点和查看是否是异步加载的数据. ...

  6. Python爬虫入门教程 3-100 美空网数据爬取

    美空网数据----简介 从今天开始,我们尝试用2篇博客的内容量,搞定一个网站叫做"美空网"网址为:http://www.moko.cc/, 这个网站我分析了一下,我们要爬取的图片在 ...

  7. Python爬虫入门教程 33-100 《海王》评论数据抓取 scrapy

    1. 海王评论数据爬取前分析 海王上映了,然后口碑炸了,对咱来说,多了一个可爬可分析的电影,美哉~ 摘录一个评论 零点场刚看完,温导的电影一直很不错,无论是速7,电锯惊魂还是招魂都很棒.打斗和音效方面 ...

  8. Python爬虫入门教程 32-100 B站博人传评论数据抓取 scrapy

    1. B站博人传评论数据爬取简介 今天想了半天不知道抓啥,去B站看跳舞的小姐姐,忽然看到了评论,那就抓取一下B站的评论数据,视频动画那么多,也不知道抓取哪个,选了一个博人传跟火影相关的,抓取看看.网址 ...

  9. Python爬虫入门教程 12-100 半次元COS图爬取

    半次元COS图爬取-写在前面 今天在浏览网站的时候,忽然一个莫名的链接指引着我跳转到了半次元网站 https://bcy.net/ 打开之后,发现也没有什么有意思的内容,职业的敏感让我瞬间联想到了 c ...

随机推荐

  1. The 'decorators' plugin requires a 'decoratorsBeforeExport' option, ...(npm start报错)

    问题描述: 在npm start启动react项目的时候,出现了如下报错: The 'decorators' plugin requires a 'decoratorsBeforeExport' op ...

  2. SpringData Jpa、Hibernate、Jpa 三者之间的关系

    JPA规范与ORM框架之间的关系是怎样的呢? JPA规范本质上就是一种ORM规范,注意不是ORM框架--因为JPA并未提供ORM实现,它只是制订了一些规范,提供了一些编程的API接口,但具体实现则由服 ...

  3. svn 冲突解决办法(黄色感叹号)

    右键:"TortoiseSVN"->"Resolved..."已解决的..., 选中全部的文件,然后OK,,然后就可以commit提交了.

  4. Spring Security原理篇(一) 启动原理

    1.概述 spring security有参考的中文翻译文档https://springcloud.cc/spring-security-zhcn.html 在学习spring security的时候 ...

  5. java笔试题及其答案

    1:下列哪个工具可以编译源文件(A) A:javac B:jdb C:javadoc D:junit 2:String b = new String("1"+"2&quo ...

  6. tensorflow在文本处理中的使用——词袋

    代码来源于:tensorflow机器学习实战指南(曾益强 译,2017年9月)——第七章:自然语言处理 代码地址:https://github.com/nfmcclure/tensorflow-coo ...

  7. 【codeforces 749A】Bachgold Problem

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【codeforces 764A】Taymyr is calling you

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. ES6/ES7/ES8新特性

    ES6 变量的改变 let const 2. 字符串新增方法 let str = 'react'; str.includes('re') // true str.repeat(3) // reactr ...

  10. 2018.11.23 浪在ACM 集训队第六次测试赛

    2018.11.23 浪在ACM 集训队第六次测试赛 整理人:刘文胜 div 2: A: Jam的计数法 参考博客:[1] 万众 B:数列 参考博客: [1] C:摆花 参考博客: [1] D:文化之 ...