2017-12-27 289 views
0

我正在按照教程使用scrapy库从网站上刮掉多个页面。本教程使用yield语句通过css选择器和xpath选择器从页面的html和css结构中获取信息。我决定使用if语句来检查搜索查询是否找到结果,并使用else语句来输出当搜索查询没有遇到结果时要执行的操作。当代码执行提取公司名称的else语句,以及位置和销售字段时,我想要一个传达'未找到'的自定义输出字符串时出现问题。使用Yield语句返回输出,当使用scrapy没有发现搜索查询时python

当我运行该脚本,我得到以下错误:

File "C:\Users\....\hoover-scraper\scraper.py", line 28 

'Location': 'Not Found' 
     ^

我觉得这是不使用yield语句,这就是为什么我收到的SyntaxError消息的正确方法。因此,我想知道在查询遇到空搜索时,是否有任何方法可以为销售和位置字段输出字符串“未找到”。

我的这部分代码:

def parse(self, response): 
    NAME_SELECTOR ="td a::text" 
    LOCATION_SELECTOR ='.//tr/td/text()' #using xpath to grab information for Location and Sales 
    SALES_SELECTOR = './/tr/td/text()' 

if response.css(NAME_SELECTOR).extract_first(): #Checks to see if the company name field has data if not prints 'No results found' 
     yield { 

      'Company Name': response.css(NAME_SELECTOR).extract_first(), 
      'Location' : response.xpath(LOCATION_SELECTOR)[0].extract(), #Location comes first inside the td tags thus the [0] 
      'Sales' : response.xpath(SALES_SELECTOR)[1].extract(), 
     } 

    else: 
     yield { 
      'Company Name': response.css("dd.value.term::text").extract_first() #identifies company name which data was not found 
      'Location': 'Not Found' 
      'Sales': 'Not Found' 
     } 
+0

参见:https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do – MrT

回答

2

yield仅用于发电。你只是想从你的方法中返回这个价值吗?然后在两个地方将yield替换为return

如果您需要稍后在同一个方法中使用该值,请将字典分配给一个变量。像

if response.css(NAME_SELECTOR).extract_first(): #Checks to see if the company name field has data if not prints 'No results found' 
     result = { 

      'Company Name': response.css(NAME_SELECTOR).extract_first(), 
      'Location' : response.xpath(LOCATION_SELECTOR)[0].extract(), #Location comes first inside the td tags thus the [0] 
      'Sales' : response.xpath(SALES_SELECTOR)[1].extract(), 
     } 

    else: 
     result = { 
      'Company Name': response.css("dd.value.term::text").extract_first(), #identifies company name which data was not found 
      'Location': 'Not Found', 
      'Sales': 'Not Found' 
     } 
    # do something with result 
    ... 
    # or just: 
    return result 
+0

是的,我以后需要使用这些值,所以将它们转换为一个字典的作品为了我。我在else语句中的每个字典条目之后添加了昏迷,并且它工作正常。 –

相关问题