在爬虫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. 【Java必修课】一图说尽排序,一文细说Sorting(Array、List、Stream的排序)

    简说排序 排序是极其常见的使用场景,因为在生活中就有很多这样的实例.国家GDP排名.奥运奖牌排名.明星粉丝排名等,各大排行榜,给人的既是动力,也是压力. 而讲到排序,就会有各种排序算法和相关实现,本文 ...

  2. CS184.1X 计算机图形学导论 作业0

    1.框架下载 在网站上下载了VS2012版本的作业0的框架,由于我的电脑上的VS是2017版的,根据提示安装好C++的版本,并框架的解决方案 重定解决方案目标为2017版本. 点击运行,可以出来界面. ...

  3. 第一天 hello world 启程

    #include<stdio.h> int main() { printf(" Hello world"); return 0; }

  4. 渗透测试-基于白名单执行payload--Regsvr32

    复现亮神课程 基于白名单执行payload--Regsvr32 0x01 Regsvr32 Regsvr32命令用于注册COM组件,是 Windows 系统提供的用来向系统注册控件或者卸载控件的命令, ...

  5. [Luogu1313][NOIP2011提高组]计算系数

    题目描述 给定一个多项式 (by+ax)k(by+ax)^k(by+ax)k ,请求出多项式展开后 xn×ymx^n \times y^mxn×ym 项的系数. 输入输出格式 输入格式: 共一行,包含 ...

  6. swap()函数的几种写法及优劣

    试用几种方法实现swap函数,比较效率高低. 首先说结果,最快的是赋值交换. 原因分析 gcc开启O2优化后,三个函数的汇编代码一样.是的,除了第一行的文件名,一模一样. 附代码 void swap1 ...

  7. Halcon一日一练:图像设备介绍

    Halcon在设计之初就提供了完整的图像采集方案,适应了多种图像设备采集图像,以及各种不同环境的采集方案. 通常情况下,图像的采集应该是所有机器视觉项目首要解决的任务,不幸的是,需要解决图像采集的问题 ...

  8. Kubernetes WebSSH终端窗口自适应Resize

    追求完美不服输的我,一直在与各种问题斗争的路上痛并快乐着 上一篇文章Django实现WebSSH操作Kubernetes Pod最后留了个问题没有解决,那就是terminal内容窗口的大小没有办法调整 ...

  9. Linux下yum与apt-get

    linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debian.Ubuntu等 RedHat 系列 1 常见的安装包格式 rpm包 ...

  10. 让button的文字换行和左对齐

    btn.titleLabel.numberOfLines = 0;   btn.contentHorizontalAlignment = UIControlContentHorizontalAlign ...