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. oracle 锁表解决方式

    /*查看被锁住的存储过程*/ SELECT * FROM V$DB_OBJECT_CACHE WHERE OWNER = 'APPADMIN' AND LOCKS != '0'; SELECT * F ...

  2. 【MySQL】究竟什么是MVCC呢?

    MVCC是什么呢? MVCC其实就是一个多版本并发控制,即多个不同版本的数据实现并发控制的技术,其基本思想是为每次事务生成一个新版本的数据, 在读数据时选择不同版本的数据即可以实现对事务结果的完整性读 ...

  3. [noi.ac省选模拟赛]第12场题解集合

    题目 比赛界面. T1 数据范围明示直接\(O(n^2)\)计算,问题就在如何快速计算. 树上路径统计通常会用到差分方法.这里有两棵树,因此我们可以做"差分套差分",在 A 树上对 ...

  4. swift - TextView和TextField之return隐藏回收键盘

    一.点击界面空白处即可收起键盘,空白处不能有其他控件的响应事件. //点击空白处关闭键盘 override func touchesEnded(_ touches: Set<UITouch> ...

  5. (一)HttpClient Get请求

    原文链接:https://blog.csdn.net/justry_deng/article/details/81042379 HttpClient的主要功能: 实现了所有 HTTP 的方法(GET. ...

  6. mysql内连接

    inner join(等值连接) 只返回两个表中联结字段相等的行 select * from role_action ra INNER JOIN action a on ra.action_id = ...

  7. 从字符串到常量池,一文看懂String类设计

    从一道面试题开始 看到这个标题,你肯定以为我又要讲这道面试题了 // 这行代码创建了几个对象? String s3 = new String("1"); 是的,没错,我确实要从这里 ...

  8. input属性设置type="number"之后, 仍可输入e;input限制只输入数字

    只需在行内输入   onKeyUp="this.value=this.value.replace(/[^\.\d]/g,'');"     就解决了   <input typ ...

  9. 红米手机 android4.4.4 root之路

    第一步: 进入360root官网下载apk安装包: http://root.360.cn/index.html 说明:不是所有的机型都能root,  一般android5.0 以下的系统root的成功 ...

  10. Thunk函数的使用

    Thunk函数的使用 编译器的求值策略通常分为传值调用以及传名调用,Thunk函数是应用于编译器的传名调用实现,往往是将参数放到一个临时函数之中,再将这个临时函数传入函数体,这个临时函数就叫做Thun ...