首页 > 解决方案 > scrapy - 重新启动 scrapoxy 实例?

问题描述

我已经使用scrapoxy设置了代理 我尝试设置非常保守的设置,不那么频繁地达到目标,按照这里的提示虽然我不确定我理解它的意思

从浏览器中的知名用户代理池中轮换您的用户代理(谷歌四处获取它们的列表)

这是我当前的 settings.py

# -*- coding: utf-8 -*-

BOT_NAME = 'mybot'

SPIDER_MODULES = ['ffscraper.spiders']
NEWSPIDER_MODULE = 'ffscraper.spiders'


# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'myUnivProject'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
CONCURRENT_REQUESTS = 5

# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
DOWNLOAD_DELAY = 20.0

# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 1
CONCURRENT_REQUESTS_PER_IP = 1
DOWNLOAD_TIMEOUT = 30
RETRY_TIMES = 0

# PROXY
PROXY = 'http://xxx.xxx.xxx.x:8888/?noconnect'

# SCRAPOXY
API_SCRAPOXY = 'http://xxx.xxx.xxx.x:8889/api'
API_SCRAPOXY_PASSWORD = 'xxxx'

DOWNLOADER_MIDDLEWARES = {
    'scrapoxy.downloadmiddlewares.proxy.ProxyMiddleware': 100,
    'scrapoxy.downloadmiddlewares.wait.WaitMiddleware': 101,
    'scrapoxy.downloadmiddlewares.scale.ScaleMiddleware': 102,
    'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware': None,
}


# Disable cookies (enabled by default)
COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
#}

# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    'ffscraper.middlewares.FfscraperSpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    'ffscraper.middlewares.FfscraperDownloaderMiddleware': 543,
#}

# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
#ITEM_PIPELINES = {
#    'ffscraper.pipelines.FfscraperPipeline': 300,
#}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
AUTOTHROTTLE_ENABLED = True
# The initial download delay
AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

我仍然,只有在大约 50 个请求之后,才开始被重定向到带有验证码的页面。

2019-01-04 00:17:09 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (meta refresh) to <GET https://www.asite.com/distil_r_captcha.html?requestId=xxx-xxx-xxx-xxx-xxx&httpReferrer=xxxx> from <GET https://www.asite.com/xxx/xxx>

在这里阻止重定向不是一种选择。我需要能够恢复活动。目前我唯一能做的就是停止爬虫,杀死 scrapoxy 上的虚拟机,然后重新启动爬虫。

有趣的是,如果我阻止重定向

yield scrapy.Request(
                    url="https://www.whatasite.com" + url.split("?")[0],
                    meta={"name": name, 'dont_redirect': True,},
                    callback=self.parse
                    )

然后我得到200一个我无法抓取的页面(因此得到空值),而不是捕获我可能可以处理的错误代码here

标签: pythonweb-scrapingscrapy

解决方案


推荐阅读