我使用的是最新的scrapy版本,v1.3
我通过按照分页中的网址逐页抓取网页。在某些页面中,网站检测到我使用机器人并在 html 中显示错误。由于它是一个成功的请求,它缓存了页面,当我再次运行它时,我得到了同样的错误。
我需要的是如何防止该页面进入缓存?或者,如果我不能这样做,我需要在意识到 parse 方法中的错误后将其从缓存中删除。然后我可以重试并得到正确的。
我有一个部分解决方案,我在元中使用“dont_cache”:False 参数生成所有请求,因此我确保它们使用缓存。在我检测到错误并重试请求的地方,我将 dont_filter=True 和 "dont_cache":True 放在一起,以确保我获得了错误 url 的新副本。
def parse(self, response):
page = response.meta["page"] + 1
html = Selector(response)
counttext = html.css('h2#s-result-count::text').extract_first()
if counttext is None:
page = page - 1
yield Request(url=response.url, callback=self.parse, meta={"page":page, "dont_cache":True}, dont_filter=True)
Run Code Online (Sandbox Code Playgroud)
我还尝试了一个自定义重试中间件,在缓存之前我设法让它工作,但我无法成功读取 response.body。我怀疑它以某种方式被压缩,因为它是二进制数据。
class CustomRetryMiddleware(RetryMiddleware):
def process_response(self, request, response, spider):
with open('debug.txt', 'wb') as outfile:
outfile.write(response.body)
html = Selector(text=response.body)
url = response.url
counttext = html.css('h2#s-result-count::text').extract_first()
if counttext is None:
log.msg("Automated process error: %s" %url, level=log.INFO)
reason = …Run Code Online (Sandbox Code Playgroud) 我在这方面搜索了很多,它可能有一个我想念的简单解决方案.
我在本地计算机和服务器上都设置了scrapy + scrapyd.当我尝试"scrapyd"时,它们都可以正常工作.
我可以毫无问题地部署到本地,我也可以从浏览器访问localhost:6800,我可以在本地运行蜘蛛.
在远程运行scrapyd后,我尝试部署到http:// remoteip:6800 /,就像我在本地部署一样,
我明白了
Packing version 1500333306
Deploying to project "projectX" in http://remoteip:6800/addversion.json
Deploy failed: <urlopen error [Errno 111] Connection refused>
Run Code Online (Sandbox Code Playgroud)
我也无法从我的本地PC 访问http:// remoteip:6800 /但我可以从远程PC上的ssh访问(有卷曲)
我在远程服务器上打开了入站和出站连接,我还缺少什么?
谢谢