如何通过Polipo over TOR通过Scrapy连接到https站点?

时间:2013-07-23 20:51:36

标签: python scrapy tor

不完全确定问题在这里。

运行Python 2.7.3和Scrapy 0.16.5

我创建了一个非常简单的Scrapy蜘蛛来测试连接到我的本地Polipo代理,以便我可以通过TOR发送请求。我蜘蛛的基本代码如下:

from scrapy.spider import BaseSpider

class TorSpider(BaseSpider):
    name = "tor"
    allowed_domains = ["check.torproject.org"]
    start_urls = [
        "https://check.torproject.org"
    ]

    def parse(self, response):
        print response.body

对于我的代理中间件,我已定义:

class ProxyMiddleware(object):
    def process_request(self, request, spider):
        request.meta['proxy'] = settings.get('HTTP_PROXY')

我的设置文件中的HTTP_PROXY定义为HTTP_PROXY = 'http://localhost:8123'

现在,如果我将起始网址更改为http://check.torproject.org,一切正常,没问题。

如果我尝试针对https://check.torproject.org运行,每次都会收到400 Bad Request错误(我也尝试过不同的https://网站,并且所有网站都有同样的问题):

2013-07-23 21:36:18+0100 [scrapy] INFO: Scrapy 0.16.5 started (bot: arachnid)
2013-07-23 21:36:18+0100 [scrapy] DEBUG: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2013-07-23 21:36:18+0100 [scrapy] DEBUG: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, RandomUserAgentMiddleware, ProxyMiddleware, RetryMiddleware, DefaultHeadersMiddleware, RedirectMiddleware, CookiesMiddleware, HttpCompressionMiddleware, ChunkedTransferMiddleware, DownloaderStats
2013-07-23 21:36:18+0100 [scrapy] DEBUG: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2013-07-23 21:36:18+0100 [scrapy] DEBUG: Enabled item pipelines: 
2013-07-23 21:36:18+0100 [tor] INFO: Spider opened
2013-07-23 21:36:18+0100 [tor] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2013-07-23 21:36:18+0100 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2013-07-23 21:36:18+0100 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2013-07-23 21:36:18+0100 [tor] DEBUG: Retrying <GET https://check.torproject.org> (failed 1 times): 400 Bad Request
2013-07-23 21:36:18+0100 [tor] DEBUG: Retrying <GET https://check.torproject.org> (failed 2 times): 400 Bad Request
2013-07-23 21:36:18+0100 [tor] DEBUG: Gave up retrying <GET https://check.torproject.org> (failed 3 times): 400 Bad Request
2013-07-23 21:36:18+0100 [tor] DEBUG: Crawled (400) <GET https://check.torproject.org> (referer: None)
2013-07-23 21:36:18+0100 [tor] INFO: Closing spider (finished)

为了仔细检查我的TOR / Polipo设置是不是有问题,我可以在终端中运行以下curl命令,并连接正常:curl --proxy localhost:8123 https://check.torproject.org/

有关这里有什么问题的任何建议吗?

2 个答案:

答案 0 :(得分:1)

尝试

rq.meta['proxy'] = 'http://127.0.0.1:8123'

就我而言,这是有效的

答案 1 :(得分:0)