繁体   English   中英

scrapy.Request()阻止我进入函数

[英]scrapy.Request() prevents me from stepping into my function

大家好〜我是Scrapy的新手,遇到了一个非常奇怪的问题。 简而言之,我发现scrapy.Request()阻止我进入函数。
这是我的代码:

# -*- coding: utf-8 -*-
import scrapy
from tutor_job_spy.items import TutorJobSpyItem

class Spyspider(scrapy.Spider):
    name = 'spy'
    #for privacy reasons I delete the url information :)
    allowed_domains = ['']
    url_0 = ''
    start_urls = [url_0, ]
    base_url = ''
    list_previous = []
    list_present = []

    def parse(self, response):
        numbers = response.xpath(  '//tr[@bgcolor="#d7ecff" or @bgcolor="#eef7ff"]/td[@width="8%" and @height="40"]/span/text()').extract()
        self.list_previous = numbers
        self.list_present = numbers
        yield scrapy.Request(self.url_0, self.keep_spying)

    def keep_spying(self, response):
        numbers = response.xpath('//tr[@bgcolor="#d7ecff" or @bgcolor="#eef7ff"]/td[@width="8%" and @height="40"]/span/text()').extract()
        self.list_previous = self.list_present
        self.list_present = numbers
        # judge if anything new
        if (self.list_present != self.list_previous):  
            self.goto_new_demand(response)
        #time.sleep(60)  #from cache
        yield scrapy.Request(self.url_0, self.keep_spying, dont_filter=True)

    def goto_new_demand(self, response):
        new_demand_links = []
        detail_links = response.xpath('//div[@class="ShowDetail"]/a/@href').extract()
        for i in range(len(self.list_present)):
            if (self.list_present[ i] not in self.list_previous):  
                new_demand_links.append(self.base_url + detail_links[i])
        if (new_demand_links != []):
            for new_demand_link in new_demand_links:
                yield scrapy.Request(new_demand_link, self.get_new_demand)

    def get_new_demand(self, response):
        new_demand = TutorJobSpyItem()
        new_demand['url'] = response.url
        requirments = response.xpath('//tr[@#bgcolor="#eef7ff"]/td[@colspan="2"]/div/text()').extract()[0]
        new_demand['gender'] = self.get_gender(requirments)
        new_demand['region'] = response.xpath('//tr[@bgcolor="#d7ecff"]/td[@align="left"]/text()').extract()[5]
        new_demand['grade'] = response.xpath('//tr[@bgcolor="#d7ecff"]/td[@align="left"]/text()').extract()[7]
        new_demand['subject'] = response.xpath('//tr[@bgcolor="#eef7ff"]/td[@align="left"]/text()').extract()[2]
        return new_demand

    def get_gender(self, requirments):
        if ('女老师' in requirments):
            return 'F'
        elif ('男老师' in requirments):
            return 'M'
        else:
            return 'Both okay'

问题是,当我调试时,发现无法进入goto_new_demand

if (self.list_present != self.list_previous):  
    self.goto_new_demand(response)

我每次运行该脚本或调试它,它只是跳过goto_new_demand,但经过我的评论yield scrapy.Request(new_demand_link, self.get_new_demand)goto_new_demand,然后我可以进入它。 我已经尝试了很多次,发现只有当其中没有yyield scrapy.Request(new_demand_link, self.get_new_demand)时,我才能进入goto_new_demand 为什么会这样?
在此先感谢任何可以提供建议的人:)
PS:
Scrapy的:1.5.0
lxml:4.1.1.0
libxml2:2.9.5
cssselect:1.0.3
解析度:1.3.1
w3lib:1.18.0
扭曲:17.9.0
Python:3.6.3(v3.6.3:2c5fed8,2017年10月3日,18:11:49)[MSC v.1900 64位(AMD64)]
pyOpenSSL:17.5.0(OpenSSL 1.1.0g 2017年11月2日)
密码学:2.1.4
平台:Windows-7-6.1.7601-SP1

问题解决了!
我将生成器 goto_new_demand修改为函数 goto_new_demand 因此问题完全是由于我对发电机 产量的一点理解。
这是修改后的代码:

if (self.list_present != self.list_previous):
    # yield self.goto_new_demand(response)
    new_demand_links = self.goto_new_demand(response)
    if (new_demand_links != []):
        for new_demand_link in new_demand_links:
            yield scrapy.Request(new_demand_link, self.get_new_demand)

def goto_new_demand(self, response):
    new_demand_links = []
    detail_links = response.xpath('//div[@class="ShowDetail"]/a/@href').extract()
    for i in range(len(self.list_present)):
        if (self.list_present[ i] not in self.list_previous):
            new_demand_links.append(self.base_url + detail_links[i])
    return new_demand_links

原因在于巴拉克的答案。

文档中介绍了调试Scrapy Spider的正确方法。 尤其有用的技术是使用Scrapy Shell检查响应。

我认为您可能需要更改此声明

if (self.list_present != self.list_previous):  
    self.goto_new_demand(response)

至:

if (self.list_present != self.list_previous):  
    yield self.goto_new_demand(response)

因为self.goto_new_demand()只是一个生成器(该函数在函数中具有yield语句),所以仅使用self.goto_new_demand(response)不会使任何运行。

生成器的一个简单示例可以使您对此更加清楚:

def a():
    print("hello")

# invoke a will print out hello
a()

但对于生成器,只需调用此函数将仅返回生成器:

def a():
    yield
    print("hello")

# invoke a will not print out hello, instead it will return a generator object
a()

因此,要抓紧时间,您应该使用yield self.goto_new_demand(response)使goto_new_demand(response)实际运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM