首页 > 解决方案 > Scrapy 产生深度为 2 的请求

问题描述

因此,我无法从一个从起始parse方法向下请求 2 个请求的函数进行日志记录。这是代码:

from datetime import datetime
import scrapy
import requests
import re
import os


class ScrapyTest(scrapy.Spider):
    """
    Generic crawler
    """
    name = "test"
    start_urls = [
            'http://www.reddit.com',
    ]

    def __init__(self, *args, **kwargs):
        super(ScrapyTest, self).__init__(*args, **kwargs)

    def parse(self, response):
        """
        Entry point for the crawler
        """
        self.logger.debug('starting off in the parse function')
        yield scrapy.Request(self.start_urls[0], callback=self.parse_hw_post)

    def parse_hw_images(self, image_links):
        self.logger.debug("inside parse_hw_images about to scrapy request parse_hw_image")
        yield scrapy.Request(self.start_urls[0], callback=self.parse_hw_image)

    def parse_hw_image(self, response):
        self.logger.debug('inside ________internal________ parse hw image')
        yield 'test string to yield in to'

    def parse_hw_post(self, response):
        # Save the images to a tmp directory for now
        self.logger.debug('in parse_hw_post')
        self.parse_hw_images('whatever')

现在唯一显示的日志是Starting off in the parse function然后inside parse_hw_images about to scrapy request parse_hw_image

预期的行为将是:

  1. 解析

  2. parse_hw_post

  3. parse_hw_images

  4. parse_hw_image

谁能看到我在做什么?

标签: pythonweb-scrapingscrapy

解决方案


yield scrapy.Request(self.start_urls[0], callback=self.parse)意味着您正在parse使用相同的 URL 调用相同的方法,因此 scrapy 将其过滤为重复的 URL。

设置DUPEFILTER_DEBUG=True为查看重复的 URL。


推荐阅读