【问题标题】:Scraping - wait untill all yielded requests are finished抓取 - 等到所有产生的请求都完成
【发布时间】:2017-06-13 07:18:06
【问题描述】:

嘿,我刚开始使用 Scrapy。我正在为网站popupstore 写一个基本的爬虫。本网站使用 ajax 请求以 json 格式获取与单个产品相关的所有数据。这是我的代码

 `  def parse_item(self, response):
        self.n += 1
        print("inside parse_item => ", self.n)

        popupitem = PopupItem()
        popupitem["url"] = response.url
        item_desc_api = self.get_item_desc_api(response)
        print("url to call =>", item_desc_api)
        # calling api url to get items description
        yield scrapy.Request(item_desc_api, callback=self.parse_item_from_api,
                         meta={"popupitem": popupitem})




    def parse_item_from_api(self, response):
        self.m += 1
        print("inside parse_item_from_api =>",self.m)
        popupitem = response.meta["popupitem"]
        jsonresponse = json.loads(response.body_as_unicode())
        yield popupitem

我使用了两个变量 n 和 m 来显示调用 parse_item (n) 和调用 parse_item_from_api (n) 的次数

问题

当我运行此代码时,它仅显示 n -> 116 和 m-> 37。因此程序在处理所有产生的请求之前退出,并且只有 37 个项目存储在 output.JSON 文件中。 如何确保在程序退出之前处理所有产生的请求

抓取日志

2017-06-13 13:37:40 [scrapy.core.engine] INFO: Closing spider 
(finished)
2017-06-13 13:37:40 [scrapy.extensions.feedexport] INFO: Stored json 
feed (37 items) in: out.json
2017-06-13 13:37:40 [scrapy.statscollectors] INFO: Dumping Scrapy 
stats:
{'downloader/request_bytes': 93446,
'downloader/request_count': 194,
'downloader/request_method_count/GET': 194,
'downloader/response_bytes': 1808706,
'downloader/response_count': 194,
'downloader/response_status_count/200': 193,
'downloader/response_status_count/301': 1,
'dupefilter/filtered': 154,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2017, 6, 13, 8, 37, 40, 576449),
'item_scraped_count': 37,
'log_count/DEBUG': 233,
'log_count/INFO': 8,
'request_depth_max': 3,
'response_received_count': 193,
'scheduler/dequeued': 193,
'scheduler/dequeued/memory': 193,
'scheduler/enqueued': 193,
'scheduler/enqueued/memory': 193,
'start_time': datetime.datetime(2017, 6, 13, 8, 37, 17, 124336)}
2017-06-13 13:37:40 [scrapy.core.engine] INFO: Spider closed ( 
finished)

【问题讨论】:

  • 请任何人帮忙。我陷入了死胡同:(
  • 您的抓取日志是否显示某些请求正在被过滤?您是否收到所有 HTTP-200 或某些请求有误(404、500...)?这应该出现在最后的统计数据中。
  • 您的抓取确实获取了 193 个 HTTP-200 响应,所以在这方面一切都很好。你必须分享你的整个爬虫,或者整个爬取日志显示爬取过早结束。
  • @paultrmbrth 请在他下面的要点gist.github.com/afrazahmad21/29bd47c7120b9c7ae091889db27c468c 中找到我的整个代码
  • 我个人不会运行你的程序,所以我猜你还需要与 LOG_LEVEL='DEBUG' 共享你的爬取日志。如果有要处理的 sill 请求(您在回调中产生的请求),则 Scrapy 不会结束。所以请确保您确实产生了正确的预期数量的请求(可能带有 self.logger.debug('yielding request to %r' % someurl) 语句。

标签: python python-3.x web-scraping scrapy


【解决方案1】:

创建一个您想要提出的所有请求的列表

all_requests = ['https://website.com/1', 'https://website.com/2', 'https://website.com/3']

link = all_requests.pop() # extract the one request to make

# make first request
yield Request(url = link, callback = self.prase_1, meta = {'remaining_links' : all_requests, data = []}

def parse_1(self, response):

    data = []

    data = data.extend( response.meta['data] ) 

    ... GRAB YOUR DATA FROM RESPONSE

    remaining_links = response.meta['remaining_links']


    # if there are more requests to make
    if len(remaining_links) > 0:
        link = remaining_links.pop() # extract one request to make

        yield Request(url = link, callback = self.prase_1, meta = {'remaining_links' : remaining_links, data = data}

    else:

        yield data

【讨论】:

    猜你喜欢
    • 2015-07-17
    • 2020-03-17
    • 2013-11-03
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    相关资源
    最近更新 更多