笔记-scrapy-Request/Response

1.     简介

Scrapy使用Request和Response来爬取网站。

2.     request

class scrapy.http.Request(url [,callback,method ='GET',headers,body,cookies,meta,encoding ='utf-8',priority = 0,dont_filter = False,errback,flags ] )

参数说明:

url (string):the URL of this request

callback (callable):the function that will be called with the response of this request (once its downloaded) as its first parameter. For more information see Passing additional data to callback functions below. If a Request doesn’t specify a callback, the spider’s parse() method will be used. Note that if exceptions are raised during processing, errback is called instead.

回调函数:将这个请求的响应(一旦下载完成)作为第一个参数调用的函数,如果请求没有指定回调,则将使用蜘蛛的parse()方法。请注意,如果在处理期间引发异常,则会调用errback。

method (string):the HTTP method of this request. Defaults to 'GET'.

meta (dict):the initial values for the Request.meta attribute. If given, the dict passed in this parameter will be shallow copied.参数传递用,注意,浅拷贝。

body (str or unicode):the request body. If a unicode is passed, then it’s encoded to str using the encoding passed (which defaults to utf-8). If body is not given, an empty string is stored. Regardless of the type of this argument, the final value stored will be a str (never unicode or None).

headers (dict):http头部。

cookies (dict or list):

可以使用两种形式发送。

字典型:

request_with_cookies = Request(url="http://www.example.com",

cookies={'currency': 'USD', 'country': 'UY'})

字典列表型:

request_with_cookies = Request(url="http://www.example.com",

cookies=[{'name': 'currency',

'value': 'USD',

'domain': 'example.com',

'path': '/currency'}])

The latter form allows for customizing the domain and path attributes of the cookie. This is only useful if the cookies are saved for later requests.

When some site returns cookies (in a response) those are stored in the cookies for that domain and will be sent again in future requests. That’s the typical behaviour of any regular web browser. However, if, for some reason, you want to avoid merging with existing cookies you can instruct Scrapy to do so by setting the dont_merge_cookies key to True in the Request.meta.

不合并cookie示例:

request_with_cookies = Request(url="http://www.example.com",

cookies={'currency': 'USD', 'country': 'UY'},

meta={'dont_merge_cookies': True})

encoding (string):(defaults to 'utf-8').

priority (int):请求优先级,目前没用过;

dont_filter (boolean):表示这个请求不应该被调度器过滤。当您想多次执行相同的请求时使用此选项,以忽略重复过滤器。小心使用它,否则你将进入爬行循环。默认为False。errback (callable): 如果在处理请求时引发任何异常,将会调用该函数

flags (list) – Flags sent to the request, can be used for logging or similar purposes.

该类还包含一些属性:

url 此请求的URL。此属性为转义的URL,可能与构造函数中传递的URL不同。属性是只读的。更改使用URL replace()。

method  表示请求中的HTTP方法,大写。例如:"GET","POST","PUT"

headers  一个包含请求头文件的类似字典的对象。

body        包含请求主体的str。属性是只读的。更改使用body. replace()。

meta        包含此请求的任意元数据的字典。对于新的请求是空的,通常由不同的Scrapy组件(扩展,中间件等)填充。

copy()        返回一个新请求,它是此请求的副本。

2.1.    meta:附加数据传递

使用meta参数传递数据

def parse_page1(self, response):

item = MyItem()

item['main_url'] = response.url

request = scrapy.Request("http://www.example.com/some_page.html",

callback=self.parse_page2)

request.meta['item'] = item

yield request

def parse_page2(self, response):

item = response.meta['item']

item['other_url'] = response.url

yield item

meta有一些官方指定键值用来对请求进行处理:

download_timeout       下载器在超时之前等待的时间(以秒为单位)

2.2.    errbacks:异常处理

import scrapy

from scrapy.spidermiddlewares.httperror import HttpError

from twisted.internet.error import DNSLookupError

from twisted.internet.error import TimeoutError, TCPTimedOutError

class ErrbackSpider(scrapy.Spider):

name = "errback_example"

start_urls = [

"http://www.httpbin.org/",              # HTTP 200 expected

"http://www.httpbin.org/status/404",    # Not found error

"http://www.httpbin.org/status/500",    # server issue

"http://www.httpbin.org:12345/",        # non-responding host, timeout expected

"http://www.httphttpbinbin.org/",       # DNS error expected

]

def start_requests(self):

for u in self.start_urls:

yield scrapy.Request(u, callback=self.parse_httpbin,

errback=self.errback_httpbin,

dont_filter=True)

def parse_httpbin(self, response):

self.logger.info('Got successful response from {}'.format(response.url))

# do something useful here...

def errback_httpbin(self, failure):

# log all failures

self.logger.error(repr(failure))

# in case you want to do something special for some errors,

# you may need the failure's type:

if failure.check(HttpError):

# these exceptions come from HttpError spider middleware

# you can get the non-200 response

response = failure.value.response

self.logger.error('HttpError on %s', response.url)

elif failure.check(DNSLookupError):

# this is the original request

request = failure.request

self.logger.error('DNSLookupError on %s', request.url)

elif failure.check(TimeoutError, TCPTimedOutError):

request = failure.request

self.logger.error('TimeoutError on %s', request.url)

3.     response

classscrapy.http.Response(url [,status = 200,headers = None,body = b'',flags = None,request = None ])

参数:

url(字符串) - 此响应的URL

状态(整数) - 响应的HTTP状态。一般为200。

标题(字典) - 这个响应的标题。字典值可以是字符串(对于单值标题)或列表(对于多值标题)。

body (bytes) - 响应主体。注意解码

标志(列表) - 是包含Response.flags属性初始值的列表 。如果给出,列表将被浅拷贝。

请求(Request对象) - Response.request属性的初始值。这代表了Request产生这个回应的那个。

response还有一些子类,但一般情况下不会使用,不予讨论。

笔记-scrapy-Request/Response的更多相关文章

  1. JavaWeb学习笔记四 request&response

    HttpServletResponse 我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一个为代表请求的request和代表响应res ...

  2. 爬虫框架Scrapy之Request/Response

    Request yield scrapy.Request(url, self.parse) Request 源码: # 部分代码 class Request(object_ref): def __in ...

  3. Scrapy中scrapy.Request和response.follow的区别

    在写scrapy的spider类的parse方法的时候,有些链接需要提取出来继续爬取,这里scrapy提供了一些方法可以方便的实现这个功能,总结如下: 假设我们的目标a标签是target_a 方法1: ...

  4. Scrapy进阶知识点总结(一)——基本命令与基本类(spider,request,response)

    一.常见命令 scrapy全局命令可以在任何地方用,项目命令只能在项目路径下用 全局命令: 项目命令: startproject crawl genspider check settings list ...

  5. 运维开发笔记整理-Request对象与Response对象

    运维开发笔记整理-Request对象与HttpResponse对象 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.request对象 1>.什么是request 首先,我 ...

  6. day06 Request Response

    rw 读写模板的设置 day05 Request Response 1. HttpServletResponse 简介 1.1 Response 的 OutputStream 输出中文的问题 1.2 ...

  7. scrapy-实现下一页请求, scrapy.Request

    # -*- coding: utf-8 -*- import scrapy class HrSpider(scrapy.Spider): name = 'hr' allowed_domains = [ ...

  8. 笔记-scrapy与twisted

    笔记-scrapy与twisted Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是事件驱动的,并且比较适合异步的代码. 在任何情况下,都不要写阻塞的代码.阻塞的代码包括: ...

  9. scrapy.Request使用meta传递数据,以及deepcopy的使用

    scrapy.Request(url[,callback,method="GET",headers,body,cookies,meta,dont_filter=False])   ...

  10. 关于scrapy中scrapy.Request中的属性

    一.源码 def __init__(self, url, callback=None, method='GET', headers=None, body=None, cookies=None, met ...

随机推荐

  1. Android环信即时通讯集成坑爹 注册报错208解决

    第一种情况:    你用的环信APPkey 是授权模式的   只要改成开放模式就可以了 第二种情况:    上述情况在环信官网都有说明  但是这种情况  环信官网就没得说明   只说的是同步方法 注册 ...

  2. appium-python-api中文文档

    来自https://wenku.baidu.com/view/533603ce581b6bd97e19eaa1.html mark,同时提供给需要使用python写脚本的童鞋们

  3. yii2.0安装ElasticSearch及使用

    yii2.0安装ElasticSearch安装及使用教程:https://www.yiichina.com/tutorial/1046 Elasticsearch 权威指南(中文版):https:// ...

  4. c++基础(积少成多)

    1.#include<string> #include<iostream> 这是引用头文件: 2.Using namespace std; 这是引用命名空间,防止同一个命名空间 ...

  5. 怎样学习webpack - 走心分享

    很多朋友可能都知道webpack,也见过webpack的代码,但是不明白里面一坨一坨的东西是什么意思,到底有什么用处!我们每个人学习一个新东西可能都会有这个过程,但是我个人觉得webpack可能是最混 ...

  6. CAD出现向程序发送命令时出现问题提示解决方法分享

    大家有没有遇到在使用cad打开图纸的时候提示向程序发送命令时出现错误的情况呢,如果你在使用cad的时候出现了这个提示,是由于软件的兼容性出现了问题,那么该怎么办呢,下面小编就给大家带来cad打开图纸提 ...

  7. SINAMICS S120/S120 EPOS 基本定位修改方向

    步骤 修改 P1821 (在线修改需要P10=3) 修改完成,copy ram to rom / load to PG

  8. 关于定义顺序和内存分配的关系--记一道不严谨的C语言题

    include<stdio.h> #include<iostream> int main() { char a[] = "123"; char b[] = ...

  9. 【[TJOI2018]碱基序列】

    题目 为什么没人用\(SAM\)啊 我们先把原来的模式串建一遍\(SAM\),之后我们就可以求出\(SAM\)上每一个节点的\(|endpos|\)就可以知道每一个子串出现的次数了,也就是在模式串上的 ...

  10. C/C++语言代码规范

    1.标识符名称: 标识符名称包括函数名.常量名.变量名等.这些名字应该能反映它所代表的实际东西,具有一定的意义,使其能 够见名知义,有助于对程序功能的理解.规则如下: 所有宏定义.枚举常数和const ...