在爬虫py文件下

class TopSpider(scrapy.Spider):
name = 'top'
allowed_domains = ['maoyan.com']
start_urls = ['https://maoyan.com/board/4'] def parse(self, response):
dds = response.xpath('//dl/dd')
for dd in dds:
dic = MaoyanItem()
# dic = {}
dic['name'] = dd.xpath('.//p[@class="name"]//text()').extract_first()
dic['star'] = dd.xpath('.//p[@class="star"]/text()').extract_first().replace('\n', '').replace(' ', '')
dic['releasetime'] = dd.xpath('.//p[@class="releasetime"]/text()').extract_first()
score1 = dd.xpath('.//p[@class="score"]/i[1]/text()').extract_first()
score2 = dd.xpath('.//p[@class="score"]/i[2]/text()').extract_first()
dic['score'] = score1 + score2
# 详情页
xqy_url = 'https://maoyan.com' + dd.xpath('.//p[@class="name"]/a/@href').extract_first()
yield scrapy.Request(xqy_url, callback=self.xqy_parse, meta={'dic': dic})
# 翻页
next_url = response.xpath('//a[text()="下一页"]/@href').extract_first()
if next_url:
url = 'https://maoyan.com/board/4' + next_url
yield scrapy.Request(url, callback=self.parse) def xqy_parse(self,response):
dic = response.meta['dic']
dic['type'] = response.xpath('//ul/li[@class="ellipsis"][1]/text()').extract_first()
dic['area_time'] = response.xpath('//ul/li[@class="ellipsis"][2]/text()').extract_first().replace('\n', '').replace(' ', '')
yield dic

在items.py 文件中写入要展示的字段

class DoubanItem(scrapy.Item):
title = scrapy.Field()
inf = scrapy.Field()
score = scrapy.Field()
peo = scrapy.Field()
brief = scrapy.Field()

在pipelines.py文件写入要打印的文本

class DoubanPipeline(object):
def open_spider(self, spider):
self.file = open('douban.txt', 'a', encoding='utf-8') def process_item(self, item, spider):
self.file.write(str(item)+'\n') def close_spider(self, spider):
self.file.close()

pipelines.py文件也可用MongoDB书写

 from pymongo import MongoClient

 class DoubanPipeline(object):
def open_spider(self,spider):
# self.file = open('douban.txt','a',encoding='utf8')
self.client = MongoClient()
self.collection = self.client['库名']['集合名']
self.count = 0 def process_item(self, item, spider):
# self.file.write(str(item)+'\n')
item['_id'] = self.count
self.count += 1
self.collection.insert_one(item)
return item def close_spider(self, spider):
# self.file.close()
self.client.close()

另外,记得在setting.py文件中配置一些信息,如

或者ROBOTS协议以及其他

爬虫之scrapy简单案例之猫眼的更多相关文章

  1. python自动化之爬虫原理及简单案例

    [爬虫案例]动态地图里的数据如何抓取:以全国PPP综合信息平台网站为例  http://mp.weixin.qq.com/s/BXWTf5hmq8vp91ZvgaphEw [爬虫案例]动态页面的抓取! ...

  2. 爬虫框架Scrapy之案例二

    新浪网分类资讯爬虫 爬取新浪网导航页所有下所有大类.小类.小类里的子链接,以及子链接页面的新闻内容. 效果演示图: items.py import scrapy import sys reload(s ...

  3. 爬虫框架Scrapy之案例三图片下载器

    items.py class CoserItem(scrapy.Item): url = scrapy.Field() name = scrapy.Field() info = scrapy.Fiel ...

  4. 爬虫框架Scrapy之案例一

    阳光热线问政平台 http://wz.sun0769.com/index.php/question/questionType?type=4 爬取投诉帖子的编号.帖子的url.帖子的标题,和帖子里的内容 ...

  5. 爬虫之CrawlSpider简单案例之读书网

    项目名py文件下 class DsSpider(CrawlSpider): name = 'ds' allowed_domains = ['dushu.com'] start_urls = ['htt ...

  6. scrapy爬虫学习系列二:scrapy简单爬虫样例学习

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  7. Python爬虫框架--Scrapy安装以及简单实用

    scrapy框架 框架 ​ -具有很多功能且具有很强通用性的一个项目模板 环境安装: Linux: ​        pip3 install scrapy ​ ​ ​  Windows: ​     ...

  8. Python 爬虫之Scrapy框架

    Scrapy框架架构 Scrapy框架介绍: 写一个爬虫,需要做很多的事情.比如:发送网络请求.数据解析.数据存储.反反爬虫机制(更换ip代理.设置请求头等).异步请求等.这些工作如果每次都要自己从零 ...

  9. Python逆向爬虫之scrapy框架,非常详细

    爬虫系列目录 目录 Python逆向爬虫之scrapy框架,非常详细 一.爬虫入门 1.1 定义需求 1.2 需求分析 1.2.1 下载某个页面上所有的图片 1.2.2 分页 1.2.3 进行下载图片 ...

随机推荐

  1. ActiveMQ学习总结------入门篇01

    注:*这篇博文文章主要介绍ActiveMQ是什么原理性的内容和如何安装和简易操作 一. ActiveMQ  简介 1 ActiveMQ是什么呢?看起来好碉堡的东西哇! ActiveMQ 是 Apach ...

  2. JVM(一)内存分配

    方法区: ①存储被虚拟机加载的类信息.常量.静态变量.即时编译器编译后的代码数据 ②又称为永久代,仅对于Hotspot来讲,JRockit和IBM J9里面没有永久代的概念,1.8以后是元空间,直接使 ...

  3. 【USACO 5.3.1】量取牛奶

    农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少,他就给多少,从不有任何误差. ...

  4. Hive 官方手册翻译 -- Hive DDL(数据定义语言)

    Hive DDL(数据定义语言) Confluence Administrator创建, Janaki Lahorani修改于 2018年9月19日 原文链接 https://cwiki.apache ...

  5. 安装高可用Hadoop生态 (二) 安装Zookeeper

    2.    安装Zookeeper 2.1. 解压程序 ※ 3台服务器分别执行 .tar.gz -C/opt/cloud/packages /opt/cloud/bin/zookeeper /conf ...

  6. CentOS 7 环境下修改主机名

    本篇文章简单介绍在CentOS 7的环境下更改主机名的方法步骤. 首先我们开启虚拟机,用root账户进行登陆,并且打开终端.我们看到默认的主机名是我们新建虚拟机时自定义的名称. 接下来我们用命令更改主 ...

  7. 解决vue组件内前置路由守卫beforeRouteEnter无法获取上下文this

    问题描述 vue框架,只有在报名页面报名成功,然后自动跳转到订单详情,才弹出一个引流弹窗,其他情况均不弹出,我就想到使用vue 的组件内前置守卫beforeRouteEnter来实现.beforeRo ...

  8. ride工具使用

    1.新建project,suite, testcase (1) 新建project:file->new project  ,输入工程名,选择Type选directory,选择工程存放路径,ok ...

  9. Ubuntu安装scrcpy手机投屏和控制(Ubuntu用QQ微信的另一种方法)

    Scrcpy 安装 snap install scrcpy adb服务安装 sudo apt-get install android-tools-adb adb配置 查看手机的USB识别号 手机通过U ...

  10. 电脑扫描不出u盘的解决办法

    现象:u盘已插上但是设备和驱动器里却找不到 解决办法: 首先记下u盘名称,然后 我的电脑-右键-管理-设备管理器,找到u盘,卸载设备后重新插入u盘即可