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 (三) : 请求传参的更多相关文章

  1. 爬虫scrapy组件 请求传参,post请求,中间件

    post请求 在scrapy组件使用post请求需要调用 def start_requests(self): 进行传参再回到 yield scrapy.FormRequest(url=url,form ...

  2. scrapy基于请求传参实现深度爬取

    请求传参实现深度爬取 请求传参: 实现深度爬取:爬取多个层级对应的页面数据 使用场景:爬取的数据没有在同一张页面中 在手动请求的时候传递item:yield scrapy.Request(url,ca ...

  3. 13.scrapy框架的日志等级和请求传参

    今日概要 日志等级 请求传参 如何提高scrapy的爬取效率 今日详情 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是s ...

  4. scrapy框架的日志等级和请求传参

    日志等级 请求传参 如何提高scrapy的爬取效率 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息 ...

  5. scrapy框架之日志等级和请求传参-cookie-代理

    一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息. - 日志信息的种类: ERROR : 一般错误 ...

  6. scrapy框架post请求发送,五大核心组件,日志等级,请求传参

    一.post请求发送 - 问题:爬虫文件的代码中,我们从来没有手动的对start_urls列表中存储的起始url进行过请求的发送,但是起始url的确是进行了请求的发送,那这是如何实现的呢? - 解答: ...

  7. 13,scrapy框架的日志等级和请求传参

    今日概要 日志等级 请求传参 如何提高scrapy的爬取效率 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy ...

  8. python爬虫---scrapy框架爬取图片,scrapy手动发送请求,发送post请求,提升爬取效率,请求传参(meta),五大核心组件,中间件

    # settings 配置 UA USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, l ...

  9. 12 Scrapy框架的日志等级和请求传参

    一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息. - 日志信息的种类: ERROR : 一般错误 ...

随机推荐

  1. HttpClient 常用方法封装

    简介 在平时写代码中,经常需要对接口进行访问,对于 http 协议 rest 风格的接口请求,大多使用 HttpClient 工具进行编写,想着方便就寻思着把一些常用的方法进行封装,便于平时快速的使用 ...

  2. (四)进行HTTPS请求并进行(或不进行)证书校验(示例)

    原文:https://blog.csdn.net/justry_deng/article/details/81042379 相关方法详情(非完美封装): /** * 根据是否是https请求,获取Ht ...

  3. 其他函数-web_get_int_property

    用于记录http响应的信息.这个函数在调试脚本的常用,但是在实际压力测试中请将这些注释 使用这个函数可以获取到的信息有: 1.HTTP_INFO_RETURN_CODE:返回HTTP响应码 2.HTT ...

  4. 操作-读取excel

    xlrd 该模块主要用来读取excel 注:sheet表示的是excel的表,就是底下的工作栏 (1) 打开excel文件并获取所有sheet import xlrd # 打开Excel文件读取数据 ...

  5. python基础003----标准数据类型

    一.标准数据类型 在python中,只要定义了一个变量,而且它有数据,那么它的类型就已经确定了,不需要开发者主动去说明它的类型,系统会自动识别.可以用type(变量名)来查看变量的类型.常见的变量类型 ...

  6. 基于领域驱动设计(DDD)超轻量级快速开发架构

    smartadmin.core.urf 这个项目是基于asp.net core 3.1(最新)基础上参照领域驱动设计(DDD)的理念,并参考目前最为了流行的abp架构开发的一套轻量级的快速开发web ...

  7. Flutter学习笔记(38)--自定义控件之组合控件

    如需转载,请注明出处:Flutter学习笔记(38)--自定义控件之组合控件 在开始之前想先写点其他的,emm...就是今天在学习到自定义控件的时候,由于自定义控件这块一直是我的短板,无论是Andro ...

  8. 明文暴露___JS前台加密,java后台解密实现

    1.前台JS <script type="text/javascript"> $(function() { $("#btn").click(func ...

  9. linux网络编程-socket(37)

    在编程的时候需要加上对应pthread开头的头文件,gcc编译的时候需要加了-lpthread选项 第三个参数是线程的入口参数,函数的参数是void*,返回值是void*,第四个参数传递给线程函数的参 ...

  10. 计算机网络之tcp/ip协议族

    TCP/IP协议族是一个四层协议系统: 1. 数据链路层   1.1 作用  (1) 实现网卡接口的网络驱动,以处理数据在以太网线等物理媒介上的传输  (2) 网络驱动程序隐藏了不同物理网络的不同电气 ...