scrapy (三) : 请求传参
scrapy 请求传参
1.定义数据结构item.py文件
'''
field: item.py
'''
# -*- coding: utf-8 -*-
# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html
import scrapy
class MovieprojectItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
# 电影海报
# 一级页面要抓取的内容
post = scrapy.Field()
name = scrapy.Field()
_type = scrapy.Field()
# 二级页面要抓取的内容
director = scrapy.Field()
design = scrapy.Field()
actor = scrapy.Field()
info = scrapy.Field()
2.爬虫文件
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import scrapy
from movieproject.items import MovieprojectItem
class MovieSpider(scrapy.Spider):
name = 'movie'
allowed_domains = ['www.id97.com']
start_urls = ['http://www.id97.com/movie/']
url = 'http://www.id97.com/movie/?page={}'
page = 1
'''
(1)只需要提取页码链接,只提取第一页的信息即可
(2)需要写两个规则,一个规则提取详情页面,一个规则是提取页码链接
'''
def parse(self, response):
# 先查找所有的movie_div
movie_div_list = response.xpath('//div[starts-with(@class,"col-xs-1-5")]')
# 遍历所有的div,去获取每一个详细的信息
for odiv in movie_div_list:
item = MovieprojectItem()
# 获取电影海报
item['post'] = odiv.xpath(".//img/@data-original").extract_first()
# 获取电影名字
item['name'] = odiv.xpath("./div/div/h1/a/text()").extract_first()
# 获取电影类型
item['_type'] = odiv.xpath("./div/div/div/a/text()").extract()
# 获取详情页面
detail_href = odiv.xpath('./div/a/@href').extract_first()
'''
向详情页面发送请求
将item向二级传递过去,到二级页面接受并且接着提取其他的信息
请求二级详情页面,解析二级页面中的相应内容,通过meta参数进行Request的数据传
'''
yield scrapy.Request(url=detail_href,callback=self.parse_detail, meta={'item': item})
# 爬取其他页面
if self.page <= 5:
self.page += 1
url = self.url.format(self.page)
print(url)
yield scrapy.Request(url=url, callback=self.parse)
def parse_detail(self,response):
# 首先获取到上一级传递过来的item
item = response.meta['item']
# 在这个页面中接着提取电影的其它信息即可
# 获取导演
item['director'] = response.xpath("//div[starts-with(@class,'col-xs-8')]/table/tbody/tr/td[2]/a/text()").extract()
# 获取编剧
item['design'] = response.xpath("//div[starts-with(@class,'col-xs-8')]/table/tbody/tr[2]/td[2]/a/text()").extract()
# 获取主演
item['actor'] = response.xpath("//div[starts-with(@class,'col-xs-8')]/table/tbody/tr[3]/td[2]/a/text()").extract()
# 获取电影介绍
item['info'] = response.xpath("//div[@class='col-xs-12 movie-introduce']/p/text()").extract_first()
#提交item到管道
yield item
3.管道文件
# -*- coding: utf-8 -*-
'''
filed: pipelines.py
'''
# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
from scrapy.utils.project import get_project_settings
import pymysql
class MovieprojectPipeline(object):
def open_spider(self,spider):
self.fp = open("movie.json","w",encoding="utf8")
def process_item(self, item, spider):
obj = dict(item)
string = json.dumps(obj,ensure_ascii=False)
self.fp.write(string+'\n')
# print("写入成功")
return item
def close_spider(self,spider):
self.fp.close()
class MovieMysqlPipeline(object):
def open_spider(self,spider):
# 获取所有的配置信息
settings = get_project_settings()
# 链接数据库
host = settings['DB_HOST']
port = settings['DB_PORT']
user = settings['DB_USER']
pwd = settings['DB_PWD']
name = settings['DB_NAME']
charset = settings['DB_CHARSET']
self.conn = pymysql.connect(host=host, port=port, user=user, password=pwd, db=name, charset=charset)
def process_item(self, item, spider):
# 拼接sql语句
sql = 'insert into movie(post, name, type, director, design, actor, info) values("%s","%s","%s","%s","%s","%s","%s")' % (item['post'], item['name'], item['_type'], item['director'], item['design'], item['actor'], item['info'])
# 获取游标
cursor = self.conn.cursor()
# 执行sql语句
try:
cursor.execute(sql)
self.conn.commit()
except Exception as e:
self.conn.rollback()
return item
def close_spider(self,spider):
# 关闭数据库
self.conn.close()
scrapy (三) : 请求传参的更多相关文章
- 爬虫scrapy组件 请求传参,post请求,中间件
post请求 在scrapy组件使用post请求需要调用 def start_requests(self): 进行传参再回到 yield scrapy.FormRequest(url=url,form ...
- scrapy基于请求传参实现深度爬取
请求传参实现深度爬取 请求传参: 实现深度爬取:爬取多个层级对应的页面数据 使用场景:爬取的数据没有在同一张页面中 在手动请求的时候传递item:yield scrapy.Request(url,ca ...
- 13.scrapy框架的日志等级和请求传参
今日概要 日志等级 请求传参 如何提高scrapy的爬取效率 今日详情 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是s ...
- scrapy框架的日志等级和请求传参
日志等级 请求传参 如何提高scrapy的爬取效率 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息 ...
- scrapy框架之日志等级和请求传参-cookie-代理
一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息. - 日志信息的种类: ERROR : 一般错误 ...
- scrapy框架post请求发送,五大核心组件,日志等级,请求传参
一.post请求发送 - 问题:爬虫文件的代码中,我们从来没有手动的对start_urls列表中存储的起始url进行过请求的发送,但是起始url的确是进行了请求的发送,那这是如何实现的呢? - 解答: ...
- 13,scrapy框架的日志等级和请求传参
今日概要 日志等级 请求传参 如何提高scrapy的爬取效率 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy ...
- python爬虫---scrapy框架爬取图片,scrapy手动发送请求,发送post请求,提升爬取效率,请求传参(meta),五大核心组件,中间件
# settings 配置 UA USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, l ...
- 12 Scrapy框架的日志等级和请求传参
一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息. - 日志信息的种类: ERROR : 一般错误 ...
随机推荐
- Mbatis使用
Mybatis的搭建过程 导入jar 创建mybatis的核心(全局)配置文件mybatis-config.xml,并配置 <?xml version="1.0" encod ...
- electron-vue报错:Webpack ReferenceError: process is not defined
electron-vue报错:Webpack ReferenceError: process is not defined 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总 ...
- Clumsy Keke【模拟+三维数组】
Clumsy Keke 题目链接(点击) Problem Description Keke is currently studying engineering drawing courses, and ...
- 分析ThreadLocal的弱引用与内存泄漏问题
目录 一.介绍 二.问题提出 2.1内存原理图 2.2几个问题 三.回答问题 3.1为什么会出现内存泄漏 3.2若Entry使用弱引用 3.3弱引用配合自动回收 四.总结 一.介绍 之前使用Threa ...
- tcpdump抓包工具的基本使用
为了更好的深入理解计算机网络等相关知识,例如TCP\UDP\IP等,我们就必须利用tcpdump.Wireshark等工具对网络进行分析.本篇博文主要记录一下tcpdump这个网络分析利器的一些基本使 ...
- 007.OpenShift管理应用部署
一 REPLICATION CONTROLLERS 1.1 RC概述 RC确保pod指定数量的副本一直运行.如果pod被杀死或被管理员显式删除,复制控制器将自动部署相应的pod.类似地,如果运行的po ...
- c++构造函数的调用方法
#include<iostream> using namespace std; class Base { public: Base(){ cout<<"hello&q ...
- JVM生命周期与运行过程
1. Java虚拟机的生命周期 Java虚拟机的生命周期 一个运行中的Java虚拟机有着一个清晰的任务:执行Java程序.程序开始执行时他才运行,程序结束时他就停止.你在同一台机器上运行三个程序,就会 ...
- disruptor架构四 多生产者多消费者执行
1.首先介绍下那个时候使用RingBuffer,那个时候使用disruptor ringBuffer比较适合场景比较简单的业务,disruptor比较适合场景较为复杂的业务,很多复杂的结果必须使用di ...
- 云计算课程实验之安装Hadoop及配置伪分布式模式的Hadoop
一.实验目的 1. 掌握Linux虚拟机的安装方法. 2. 掌握Hadoop的伪分布式安装方法. 二.实验内容 (一)Linux基本操作命令 Linux常用基本命令包括: ls,cd,mkdir,rm ...