9

我想使用scrapy shell和测试需要基本身份验证凭据的 url 的响应数据。我试图检查scrapy shell 文档,但在那里找不到。

我试过了,scrapy shell 'http://user:pwd@abc.com'但没有用。有人知道我怎么能做到吗?

4

2 回答 2

21

如果您只想使用外壳,您可以执行以下操作:

$ scrapy shell

并在外壳内:

>> from w3lib.http import basic_auth_header
>> from scrapy import Request
>> auth = basic_auth_header(your_user, your_password)
>> req = Request(url="http://example.com", headers={'Authorization': auth})
>> fetch(req)

asfetch使用当前请求来更新 shell 会话。

于 2017-03-16T02:57:34.110 回答
6

是的,使用httpauth 中间件

确保在设置中启用了 HTTPAuthMiddleware,然后定义:

class MySpider(CrawSpider):
    http_user = 'username'
    http_pass = 'password'
    ...

作为蜘蛛中的类变量。

此外,如果在设置中启用了中间件,则无需在 url 中指定登录凭据。

于 2017-03-16T02:46:38.877 回答