我需要帮助在 Ubuntu 中设置 Tor 并在 scrapy 框架中使用它。
我做了一些研究并找到了这个指南:
class RetryChangeProxyMiddleware(RetryMiddleware):
def _retry(self, request, reason, spider):
log.msg('Changing proxy')
tn = telnetlib.Telnet('127.0.0.1', 9051)
tn.read_until("Escape character is '^]'.", 2)
tn.write('AUTHENTICATE "267765"\r\n')
tn.read_until("250 OK", 2)
tn.write("signal NEWNYM\r\n")
tn.read_until("250 OK", 2)
tn.write("quit\r\n")
tn.close()
time.sleep(3)
log.msg('Proxy changed')
return RetryMiddleware._retry(self, request, reason, spider)
然后在settings.py中使用它:
DOWNLOADER_MIDDLEWARE = {
'spider.middlewares.RetryChangeProxyMiddleware': 600,
}
然后你只想通过本地 Tor 代理(polipo)发送请求,可以通过以下方式完成:
tsocks scrapy crawl spirder
有谁可以确认这种方法有效并且您获得不同的IP吗?
我正在使用这个片段:http://snipplr.com/view/66992/use-a-random-user-agent-for-each-request/ http://snipplr.com/view/66992/use-a-random-user-agent-for-each-request/
更新:已修复损坏的链接
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)