0

django-dynamic-scraper在我的一个 django 项目中使用。我正在做的事情很简单。我正在继承类以在其方法dynamic_scraper.spiders.DjangoSpider中具有一些自定义功能。parse以下是我所做的:

from dynamic_scraper.spiders import DjangoSpider

class CustomSpider(DjangoSpider):
    def __init__(self, *args, **kwargs):
        # some custom stuff here
        super(CustomSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        # Modify response based on some custom
        # set of rules

        super(CustomSpider, self).parse(response)

现在这里方法中的super调用parse没有被触发。我确保我继承了正确的类并且它确实有一个 parse 方法。

我尝试在DjangoSpider的 parse 方法中打印调试语句,但在 stdout 中看不到任何内容。

如果我在超级调用之后尝试打印相同的调试语句,我会在标准输出中看到这些语句。

有任何想法吗 ?

4

1 回答 1

2

parse是生成器,而不是常规方法。

所以你必须这样做:

def parse(self, response):
    # Modify response based on some custom
    # set of rules

    for x in super(CustomSpider, self).parse(response)
        yield x

Because a generator is returned, parse is iterable. You need to yield each item from the super call that was passed your modified response.

Example

class Base(object):
    def gen(self, data):
        for i in data:
            yield i

class Something(Base):
    def gen(self, data):
        data += "world"
        for i in super(Something, self).gen(data):
            yield i

s = Something()
for i in s.gen("hello"):
    print i

Output

h
e
l
l
o
w
o
r
l
d
于 2014-10-30T15:22:12.447 回答