Scrapy spider returns 几分钟后响应 200

Scrapy spider returns 200 response after a few mintues

我在尝试删除网站时遇到动态内容问题。我刚刚使用 Docker 使用以下方法将 Splash 添加到我的 Scrapy 中:

https://blog.scrapinghub.com/2015/03/02/handling-javascript-in-scrapy-with-splash/

不幸的是,由于动态内容(可能?),我仍然无法捕获内容。

我的代码运行,捕获内容,然后在抓取大约 4000 页后,它只是 returns 接下来的 6000 页出现这个错误,其中大部分有数据:

[scrapy.core.engine] DEBUG: Crawled (200) <GET http://www...> (referer: None)

这是我的爬虫代码:

import scrapy
from scrapy_splash import SplashRequest

class PeopleSpider(scrapy.Spider):
 name="people"
 start_urls=[
  'http://www.canada411.ca/res/%s/' % page for page in xrange(5192080000,5192090000)   
 ]
 def start_requests(self):
  for url in self.start_urls:
    yield SplashRequest(url, self.parse,
     endpoint='render.html',
     args={'wait': 2},
    )
 def parse(self,response):
  for people in response.css('div#contact'):
   yield{
    'name': people.css('h1.vcard__name::text').extract_first().strip().title(),
    'address': people.css('div.vcard__address::text').extract_first().strip().split(',')[0].strip(),
    'city': people.css('div.vcard__address::text').extract_first().strip().split(',')[1].strip().split(' ')[0].strip(),
    'province': people.css('div.vcard__address::text').extract_first().strip().split(',')[1].strip().split(' ')[1].strip(),
    'postal code': people.css('div.vcard__address::text').extract_first().split(',')[2].strip().replace(' ',''),
    'phone': people.css('span.vcard__label::text').extract_first().replace('(','').replace(')','').replace('-','').replace(' ',''),
   }

当您没有获取数据时,将响应的 HTML 保存在一个文件中,然后在浏览器中打开该 HTML 文件以查看为什么 nameaddress 等该页面上不存在。

我怀疑由于来自同一 IP 的连续请求,他们正在显示验证码。

如果他们显示验证码,您可以使用代理服务来避免验证码,

同时创建一个 DownloadMiddleware and inside process_request 函数,检查是否有验证码,然后使用 dont_filter=True 参数再次抓取 link。

编辑

您可以使用此代码写入文件,顺便说一下,只需 google 并且您会发现使用 Python.

写入文件的多种方法
with open('response.html', '2+') as the_file:
     the_file.write(response.body)