【问题标题】:using tor with scrapy framework在scrapy框架中使用tor
【发布时间】:2011-12-26 10:22:45
【问题描述】:

我正在尝试抓取网站,该网站足够复杂,可以阻止机器人,我的意思是它只允许几个请求,之后 Scrapy 挂起。

问题 1:有没有办法,如果 Scrapy 挂起,我可以从同一点重新开始我的爬行过程。 为了摆脱这个问题,我这样写了我的设置文件

BOT_NAME = 'MOZILLA'
BOT_VERSION = '7.0'

SPIDER_MODULES = ['yp.spiders']
NEWSPIDER_MODULE = 'yp.spiders'
DEFAULT_ITEM_CLASS = 'yp.items.YpItem'
USER_AGENT = '%s/%s' % (BOT_NAME, BOT_VERSION)

DOWNLOAD_DELAY = 0.25
DUPEFILTER=True
COOKIES_ENABLED=False
RANDOMIZE_DOWNLOAD_DELAY=True
SCHEDULER_ORDER='BFO'

这是我的程序:

class ypSpider(CrawlSpider):

   name = "yp"


   start_urls = [
       SOME URL

   ]
   rules=(
      #These are some rules
   )
   def parse_item(self, response):
   ####################################################################
   #cleaning the html page by removing scripts html tags    
   #######################################################
   hxs=HtmlXPathSelector(response)

问题是我可以在哪里编写 http 代理,我是否必须导入任何与 tor 相关的类,我是 Scrapy 的新手,因为这个小组我学到了很多,现在我正在尝试学习“如何使用 ip 轮换”或 tor'

正如我们的一位成员建议的那样,我启动了 tor 并将 HTTP_PROXY 设置为

set http_proxy=http://localhost:8118

但它会抛出一些错误,

failure with no frames>: class 'twisted.internet.error.ConnectionRefusedError'   Connection was refused by other side 10061: No connection could be made because the target machine actively refused it.

所以我把 http_proxy 改成了

set http_proxy=http://localhost:9051

现在错误是

failure with no frames>: class 'twisted.internet.error.ConnectionDone' connection was closed cleanly.

我检查了 firefox 网络设置,在那里我看不到任何 http 代理,但不是它使用 SOCKSV5,而是显示 127.0.0.1:9051。 (在 TOR 之前它没有代理)请帮助我,我仍然不明白如何通过 Scrapy 使用 TOR。 我应该使用哪个 TOR 捆绑包以及如何使用? 我希望我的两个问题都能得到解决

  1. 如果一个 scrapy 爬虫由于某种原因挂起(连接失败),我想从那里恢复服务本身
  2. 如何在 Scrapy 中使用轮换 IP

【问题讨论】:

  • 您是否尝试过将用户代理字符串设置为比MOZILLA/7.0 更有说服力的字符串?尝试使用您自己浏览器的完整用户代理字符串:httpbin.org/user-agent

标签: python scrapy tor


【解决方案1】:

TOR 本身不是 http 代理,端口 8118 和连接被拒绝错误表明您没有正常运行 privoxy[1]。尝试正确设置 privoxy,然后使用环境变量 http_proxy=http://localhost:8118 重试。

我已经使用 privoxy 和 scrapy 成功完成了 TOR 的爬取。

[1]http://www.privoxy.org/

【讨论】:

  • 嗨 Rho 你能在这里分享你的经验和源代码吗?这对我们来说可能非常有趣。提前谢谢你
  • @imx51 没有太多关于使用 TOR 作为代理的分享。您只需要设置环境变量即可。
猜你喜欢
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
  • 1970-01-01
  • 2020-12-04
相关资源
最近更新 更多