在爬虫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. 用深度学习做命名实体识别(六)-BERT介绍

    什么是BERT? BERT,全称是Bidirectional Encoder Representations from Transformers.可以理解为一种以Transformers为主要框架的双 ...

  2. Creator3D长什么样?看看官方惊艳的DEMO就知道了,附在线体验!

    Shawn 这两天在学习 Creator3D 的官方案例,由于是刚接触 Creator3D 很多东西在没弄清楚之前还是以简单的编辑介绍为主,先了解一下3D场景的基本操作: 观查场景:按住鼠标右键以自己 ...

  3. MySQL日期和时间类型笔记

    最近在看<MySQL技术内幕:SQL编程>并做了笔记,这是一篇笔记类型博客,分享出来方便自己复习,也可以帮助其他人 一.日期时间类型所占空间对比 各种日期时间数据类型所占的空间: 类型 所 ...

  4. SpringBootSecurity学习(24)前后端分离版之OAuth2.0 应用登记

    应用登记 一个应用要求 OAuth 授权,必须先到对方网站登记,让对方知道是谁在请求.举个例子,下面是github的登记页面: https://github.com/settings/applicat ...

  5. 无人机基于Matlab/Simulink的模型开发(连载一)

    "一切可以被控制的对象,都需要被数学量化" 这是笔者从事多年研发工作得出的道理,无论是车辆控制,机器人控制,飞机控制,还是无人机控制,所有和机械运动相关的控制,如果不能被很好的数学 ...

  6. hibernate之小白一

    关于hibernate框架,以下是我自己的见解,每个人的理解各不同,希望各位读者根据自己的需要来查询自己想要的.以下我来给你们分享我学习hibernate的一些理论和实践: 首先我们来了解一下hibe ...

  7. HTML5远程工具

    因为有从网页直接远程其他windows电脑的需求,于是通过网上搜索找到下面几个解决方案,分享一下: 1.windows的远程桌面web连接tsweb 下载地址https://www.microsoft ...

  8. boost::asio::io_service::定时器任务队列

    使用io_service和定时器写的一个同步和异步方式的任务队列 #pragma once #include <string> #include <iostream> #inc ...

  9. asp.net core mvc中自定义ActionResult

    在GitHub上有个项目,本来是作为自己研究学习.net core的Demo,没想到很多同学在看,还给了很多星,所以觉得应该升成3.0,整理一下,写成博分享给学习.net core的同学们. 项目名称 ...

  10. Https工作流程图