Scarpy源码分析 13 Requests and Responses Ⅱ

2021SC@SDUSC

书接上回

  • encoding (str) – the encoding of this request (defaults to 'utf-8'). This encoding will be used to percent-encode the URL and to convert the body to bytes (if given as a string).

此请求的编码(默认为 'utf-8')。此编码将用于对 URL 进行百分比编码并将正文转换为字节(如果以字符串形式给出)。

  • priority (int) – the priority of this request (defaults to 0). The priority is used by the scheduler to define the order used to process requests. Requests with a higher priority value will execute earlier. Negative values are allowed in order to indicate relatively low-priority.

此请求的优先级(默认为 0)。调度程序使用优先级来定义用于处理请求的顺序。具有更高优先级值的请求将更早执行。允许负值以指示相对较低的优先级。

  • dont_filter (bool) – indicates that this request should not be filtered by the scheduler. This is used when you want to perform an identical request multiple times, to ignore the duplicates filter. Use it with care, or you will get into crawling loops. Default to False.

表示该请求不应被调度程序过滤。当我们想要多次执行相同的请求时使用它,以忽略重复项过滤器。小心使用否则会陷入爬行循环。默认为假。

errback (collections.abc.Callable) –a function that will be called if any exception was raised while processing the request. This includes pages that failed with 404 HTTP errors and such. It receives a Failure as first parameter. 

如果在处理请求时引发任何异常,将调用该函数。这包括因 404 HTTP 错误等而失败的页面。它接收一个失败作为第一个参数。

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

发送到请求的标志,可用于日志记录或类似目的。

  • cb_kwargs (dict) – A dict with arbitrary data that will be passed as keyword arguments to the Request’s callback.

带有任意数据的 dict,将作为关键字参数传递给请求的回调。

part2:URL:

包含此请求的 URL 的字符串。 请记住,此属性包含转义的 URL,因此它可能与 __init__ 方法中传递的 URL 不同。

该属性是只读的。 要更改请求的 URL,请使用 replace()。

method:

表示请求中的 HTTP 方法的字符串。 必须是大写的。 示例:“GET”、“POST”、“PUT”等

headers:

包含请求标头的类似字典的对象。

body:

请求正文为字节。

meta:

A dict that contains arbitrary metadata for this request. This dict is empty for new Requests, and is usually populated by different Scrapy components (extensions, middlewares, etc). So the data contained in this dict depends on the extensions you have enabled.

包含此请求的任意元数据的字典。对于新的请求,这个 dict 是空的,通常由不同的 Scrapy 组件(扩展、中间件等)填充。因此,此 dict 中包含的数据取决于我们启用的扩展。

cb_kwargs:

包含此请求的任意元数据的字典。它的内容将作为关键字参数传递给请求的回调。对于新请求,它是空的,这意味着默认情况下回调只获取一个 Response 对象作为参数。

当使用 copy() 或 replace() 方法克隆请求时,此 dict 是浅复制的,也可以在我们的爬虫中从 response.cb_kwargs 属性访问。

此外:

In case of a failure to process the request, this dict can be accessed as failure.request.cb_kwargs in the request’s errback. For more information, see Accessing additional data in errback functions.

如果处理请求失败,可以在请求的 errback 中作为 failure.request.cb_kwargs 访问此字典。有关更多信息,请参阅访问 errback 函数中的其他数据。

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

replace([urlmethodheadersbodycookiesmetaflagsencodingprioritydont_filtercallbackerrbackcb_kwargs])

Return a Request object with the same members, except for those members given new values by whichever keyword arguments are specified. The Request.cb_kwargs and Request.meta attributes are shallow copied by default (unless new values are given as arguments). 

返回具有相同成员的 Request 对象,除了那些由指定的关键字参数赋予新值的成员。 Request.cb_kwargs 和 Request.meta 属性默认是浅复制的(除非新值作为参数给出)。

replase的源码我会附在文章末尾。

classmethod      from_curl(curl_commandignore_unknown_options=True**kwargs)

Create a Request object from a string containing a cURL command. It populates the HTTP method, the URL, the headers, the cookies and the body. It accepts the same arguments as the Request class, taking preference and overriding the values of the same arguments contained in the cURL command.
 

从包含 cURL 命令的字符串创建请求对象。填充 HTTP 方法、URL、标题、cookie 和正文。它接受与 Request 类相同的参数,优先考虑并覆盖 cURL 命令中包含的相同参数的值。

(默认情况下会忽略无法识别的选项。要在查找未知选项时引发错误,我们需要通过传递 ignore_unknown_options=False 调用此方法。)

    def replace(self, *args, **kwargs):
        """Create a new Request with the same attributes except for those
        given new values.
        """
        for x in ['url', 'method', 'headers', 'body', 'cookies', 'meta', 'flags',
                  'encoding', 'priority', 'dont_filter', 'callback', 'errback', 'cb_kwargs']:
            kwargs.setdefault(x, getattr(self, x))
        cls = kwargs.pop('cls', self.__class__)
        return cls(*args, **kwargs)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值