gpt4 book ai didi

python - 在 scrapy 框架中使用 tor

转载 作者:太空狗 更新时间:2023-10-30 00:08:16 26 4
gpt4 key购买 nike

我正在尝试抓取网站,该网站足够复杂以阻止机器人程序,我的意思是它只允许几个请求,然后 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 并将 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

最佳答案

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

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

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

关于python - 在 scrapy 框架中使用 tor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8084423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com