【问题标题】:How to use scrapy with an internet connection through a proxy with authentication如何通过具有身份验证的代理将scrapy与互联网连接一起使用
【发布时间】:2013-08-11 21:39:08
【问题描述】:

我的互联网连接是通过具有身份验证的代理,当我尝试运行 scraoy 库以制作更简单的示例时,例如:

scrapy shell http://stackoverflow.com

一切正常,直到您使用 XPath 选择器请求某些内容,响应是下一个:

>>> hxs.select('//title')
[<HtmlXPathSelector xpath='//title' data=u'<title>ERROR: Cache Access Denied</title'>]

或者,如果您尝试运行在项目中创建的任何蜘蛛,则会出现以下错误:

C:\Users\Victor\Desktop\test\test>scrapy crawl test
2012-08-11 17:38:02-0400 [scrapy] INFO: Scrapy 0.16.5 started (bot: test)
2012-08-11 17:38:02-0400 [scrapy] DEBUG: Enabled extensions: LogStats, TelnetCon
sole, CloseSpider, WebService, CoreStats, SpiderState
2012-08-11 17:38:02-0400 [scrapy] DEBUG: Enabled downloader middlewares: HttpAut
hMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, De
faultHeadersMiddleware, RedirectMiddleware, CookiesMiddleware, HttpProxyMiddlewa
re, HttpCompressionMiddleware, ChunkedTransferMiddleware, DownloaderStats
2012-08-11 17:38:02-0400 [scrapy] DEBUG: Enabled spider middlewares: HttpErrorMi
ddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddle
ware
2012-08-11 17:38:02-0400 [scrapy] DEBUG: Enabled item pipelines:
2012-08-11 17:38:02-0400 [test] INFO: Spider opened
2012-08-11 17:38:02-0400 [test] INFO: Crawled 0 pages (at 0 pages/min), scraped
0 items (at 0 items/min)
2012-08-11 17:38:02-0400 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:602
4
2012-08-11 17:38:02-0400 [scrapy] DEBUG: Web service listening on 0.0.0.0:6081
2012-08-11 17:38:47-0400 [test] DEBUG: Retrying <GET http://automation.whatismyi
p.com/n09230945.asp> (failed 1 times): TCP connection timed out: 10060: Se produ
jo un error durante el intento de conexi¾n ya que la parte conectada no respondi
¾ adecuadamente tras un periodo de tiempo, o bien se produjo un error en la cone
xi¾n establecida ya que el host conectado no ha podido responder..
2012-08-11 17:39:02-0400 [test] INFO: Crawled 0 pages (at 0 pages/min), scraped
0 items (at 0 items/min)
...
2012-08-11 17:39:29-0400 [test] INFO: Closing spider (finished)
2012-08-11 17:39:29-0400 [test] INFO: Dumping Scrapy stats:
{'downloader/exception_count': 3,
  'downloader/exception_type_count/twisted.internet.error.TCPTimedOutError': 3,
     'downloader/request_bytes': 732,
     'downloader/request_count': 3,
     'downloader/request_method_count/GET': 3,
     'finish_reason': 'finished',
     'finish_time': datetime.datetime(2012, 8, 11, 21, 39, 29, 908000),
     'log_count/DEBUG': 9,
     'log_count/ERROR': 1,
     'log_count/INFO': 5,
     'scheduler/dequeued': 3,
     'scheduler/dequeued/memory': 3,
     'scheduler/enqueued': 3,
     'scheduler/enqueued/memory': 3,
     'start_time': datetime.datetime(2012, 8, 11, 21, 38, 2, 876000)}
2012-08-11 17:39:29-0400 [test] INFO: Spider closed (finished)

看来我的代理是问题所在。如果有人知道通过身份验证代理使用 scrapy 的方法,请告诉我。

【问题讨论】:

    标签: python proxy web-scraping scrapy


    【解决方案1】:

    Scrapy 通过使用HttpProxyMiddleware 来支持代理:

    此中间件设置 HTTP 代理以用于请求,通过设置 Request 对象的代理元值。像 Python 标准一样 库模块 urllib 和 urllib2,它遵循以下环境 变量:

    • http_proxy
    • https_proxy
    • no_proxy

    另见:

    【讨论】:

    • 问题是第一个链接对我不起作用,我不知道为什么,第二个已经读过,我不知道如何设置 http_proxy 变量,如果你帮助我跨度>
    • 您设置了http_proxyhttps_proxy 环境变量吗?
    • 是的,问题已解决,谢谢。链接mahmoud.abdel-fattah.net/2012/04/07/using-scrapy-with-proxies 非常有用。
    • @alecxe 我可以为每个请求设置 https_proxy 吗?
    • 如果有人在寻找第一个链接 wbwm 有它:https://web.archive.org/web/*/mahmoud.abdel-fattah.net/2012/04/07/using-scrapy-with-proxies/
    【解决方案2】:

    重复 Mahmoud M. Abdel-Fattah 的回答,因为该页面现在不可用。归功于他,但是,我做了一些细微的修改。

    如果middlewares.py已经存在,添加如下代码。

    class ProxyMiddleware(object):
        # overwrite process request
        def process_request(self, request, spider):
            # Set the location of the proxy
            request.meta['proxy'] = "http://YOUR_PROXY_IP:PORT"
    
            # Use the following lines if your proxy requires authentication
            proxy_user_pass = "USERNAME:PASSWORD"
            # setup basic authentication for the proxy
            encoded_user_pass = base64.encodestring(proxy_user_pass.encode())
            #encoded_user_pass = base64.encodestring(proxy_user_pass)
            request.headers['Proxy-Authorization'] = 'Basic ' + \
                str(encoded_user_pass)
    

    在settings.py文件中,添加如下代码

        DOWNLOADER_MIDDLEWARES = {
        'project_name.middlewares.ProxyMiddleware': 100,
    }
    

    这应该通过设置http_proxy 来工作。但是,就我而言,我正在尝试使用 HTTPS 协议访问 URL,需要设置 https_proxy 我仍在调查。这方面的任何线索都会有很大帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多