2017-05-23 68 views
1

所以這裏是我的Scrapy搜索器代碼。我正在嘗試從網站中提取元數據值。沒有元數據在頁面上多次出現。Scrapy/Python:替換空字符串

class MySpider(BaseSpider): 
    name = "courses" 
    start_urls = ['http://www.example.com/listing'] 
    allowed_domains = ["example.com"] 
    def parse(self, response): 
    hxs = Selector(response) 
    #for courses in response.xpath(response.body): 
    for courses in response.xpath("//meta"): 
    yield { 
       'ScoreA': courses.xpath('//meta[@name="atarbur"]/@content').extract_first(), 
       'ScoreB': courses.xpath('//meta[@name="atywater"]/@content').extract_first(), 
       'ScoreC': courses.xpath('//meta[@name="atarsater"]/@content').extract_first(), 
       'ScoreD': courses.xpath('//meta[@name="clearlywaur"]/@content').extract_first(), 
       } 
    for url in hxs.xpath('//ul[@class="scrapy"]/li/a/@href').extract(): 
     yield Request(response.urljoin(url), callback=self.parse) 

所以我想實現的是,如果任何分數的值是一個空字符串(「」),我想和0(零)repalce它。我不確定如何在'yield'塊中添加條件邏輯。

任何幫助非常感謝。

感謝

回答

4

extract_first()方法有默認值的可選參數,但在你的情況,你可以只使用or表達:

foo = response.xpath('//foo').extract_first('').strip() or 0 

在這種情況下,如果extract_first()返回一個字符串,沒有任何文字它將評估爲「錯誤的」,以便評估最新的評估成員(0)。

將字符串類型轉換爲其他嘗試:

foo = int(response.xpath('//foo').extract_first('').strip() or 0) 
+0

工作就像一個魅力。謝謝。快速提問:我上面的代碼將數值作爲字符串返回,即用引號括起來。你知道我怎麼能不用引號返回值? – Slyper

+0

@Slyper是的,scrapy將總是返回'extract()'和'extract_first()'的字符串或字符串列表。但是,您可以將其轉換爲「float」或「int」類型;看我的編輯。 – Granitosaurus

+0

太好了,再次感謝。 – Slyper