您当前的位置: 首页 >  scrapy

彭世瑜

暂无认证

  • 2浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Scrapy添加代理验证

彭世瑜 发布时间:2020-05-30 11:27:29 ,浏览量:2

middlewares.py

from w3lib.http import basic_auth_header

class CustomProxyMiddleware(object):
    def process_request(self, request, spider):
        request.meta['proxy'] = "https://:"
        request.headers['Proxy-Authorization'] = basic_auth_header(
            '', '')

settings.py

DOWNLOADER_MIDDLEWARES = {
    '.middlewares.CustomProxyMiddleware': 350,
    'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware': 400,
}
问题

1、如果代理验证设置不对,状态码会返回407 407 Proxy Authentication Required

刚开始采用以下格式配置,发现部分请求可以发送,不过会有一个重试,部分请求直接报错

request.meta['proxy'] = "https://:@:"

正确的设置是在请求头中设置 Proxy-Authorization

参考

  1. Using a custom proxy in a Scrapy spider
  2. Proxy-Authorization
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.1493s