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. jq制作tab栏

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. dos taskkill 命令

    C:\Users\asn\Desktop>taskkill /? TASKKILL [/S system [/U username [/P [password]]]] { [/FI filter ...

  3. servicemix-4.5.3 启动日志

    karaf@root> log:display 2015-01-12 10:48:03,950 | WARN  | rint Extender: 3 | XBeanNamespaceHandle ...

  4. 通过页码直接跳转 html

    <?php namespace Admin\TagLib; class BootstrapPage{ public $firstRow; // 起始行数 public $listRows; // ...

  5. js基础——对象和数组

    1.Object类型 1)使用new运算符    var box = new Object();===>等同于 var box = Object();(省略new关键字)    box.name ...

  6. UVa 1152 -4 Values whose Sum is 0—[哈希表实现]

    The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute ...

  7. Educational Codeforces Round 54 (Rated for Div. 2) D Edge Deletion (SPFA + bfs)

    题目大意:给定你一个包含n个点m条边的无向图,现在最多在图中保留k条边,问怎么删除多的边,使得图中良好的节点数最多,求出保留在图中的边的数量和编号. 良好的节点定义为:删除某条边后该点到点1的最短距离 ...

  8. 看到两道小学数学题,实在是解不动,用js写了一下

    把一个自然数的约数(除去它本身)按照从小到大的顺序写在它的左边,可以得到一个多位数,比如6的约数是1,2,3,写成一个多位数是1236,假如这个多位数中,没有直复数字,那么我们你这个多位数是唯一的.请 ...

  9. Linux 内核设备属性

    sysfs 中的设备入口可有属性. 相关的结构是: struct device_attribute { struct attribute attr; ssize_t (*show)(struct de ...

  10. 类(class)和继承

    .继承之前的写法 ↓ ----------------------------------------------------------------------------------------- ...