如果字符串不是None

时间:2018-10-27 18:20:56

标签: python scrapy

我正在使用scrapy python库,并且在某些迭代中获得了xpath响应的返回值,但是,其他人却得到了返回: "(None,)"值。 我只想产生不是"(None,)"的值。

我尝试了以下操作:

if option1 not None:

,但是它仍然打印"(None,)"来输出。

任何方向将不胜感激。

更新,这是我的完整代码:

import scrapy

class StreakSpider(scrapy.Spider):
    name = 'streak_spider'
    start_urls = ['http://streak.espn.com/en/?date=20181026']
def parse(self, response):
    GAME_SELECTOR = '.matchup-container'

    for game in response.css(GAME_SELECTOR):
        QUESTION_SELECTOR = '.gamequestion strong ::text'
        TIME_SELECTOR = '.startTime ::text'
        SPORT_SELECTOR = '.sport-description ::text'
        WINNER1_SELECTOR = '.winner ::text'
        OPTION1_SELECTOR = './/span/strong/a/text()'
        question = game.css(QUESTION_SELECTOR).extract_first(),
        time = game.css(TIME_SELECTOR).extract_first(),
        sport = game.css(SPORT_SELECTOR).extract_first(),
        winner1 = game.css(WINNER1_SELECTOR).extract_first(),
        option1 = game.xpath(OPTION1_SELECTOR).extract_first(),

        #if option1 != '"(None,)"':
        #if option1 is not None:
        if option1:
            yield {
                    'option1': option1,
            }

1 个答案:

答案 0 :(得分:0)

(None,)tuple

您必须使用.extract(),所以我建议使用.extract_first()仅获得第一个结果