Ewig
V2EX  ›  问与答

Python 中的一个逻辑写法

  •  
  •   Ewig · Dec 3, 2018 · 1870 views
    This topic created in 2743 days ago, the information mentioned may be changed or developed.
    if response.xpath('//link[contains(@rel,"apple-touch-icon-precomposed")]/@href') :
    apple_touch_url = response.xpath('//link[contains(@rel,"apple-touch-icon-precomposed")]/@href').extract()
    apple_touch_url=[link for link in apple_touch_url if 'https' in link or 'http' in link ]
    item['logo'] = apple_touch_url
    if not apple_touch_url:
    ‘这里没有这个列表我想走 elif 的逻辑’但是好像没有这样的语法,求人指教,谢谢

    elif response.xpath('//link[contains(@rel,"icon")]/@href'):
    rel_url=response.xpath('//link[contains(@rel,"icon")]/@href').extract()
    item['logo']=rel_url
    print(rel_url)
    Supplement 1  ·  Dec 3, 2018
    if response.xpath('//link[contains(@rel,"apple-touch-icon-precomposed")]/@href') :
    apple_touch_url = response.xpath('//link[contains(@rel,"apple-touch-icon-precomposed")]/@href').extract()
    apple_touch_url=[link for link in apple_touch_url if 'https' in link or 'http' in link ]
    item['logo'] = apple_touch_url
    if not apple_touch_url:
    ‘这里没有这个列表我想走 elif 的逻辑’但是好像没有这样的语法,求人指教,谢谢

    elif response.xpath('//link[contains(@rel,"icon")]/@href'):
    rel_url=response.xpath('//link[contains(@rel,"icon")]/@href').extract()
    item['logo']=rel_url
    print(rel_url)
    Supplement 2  ·  Dec 3, 2018
    if response.xpath('//link[contains(@rel,"apple-touch-icon-precomposed")]/@href') :
    apple_touch_url = response.xpath('//link[contains(@rel,"apple-touch-icon-precomposed")]/@href').extract()
    apple_touch_url=[link for link in apple_touch_url if 'https' in link or 'http' in link ]
    item['logo'] = apple_touch_url
    if not apple_touch_url:
    ‘这里没有这个列表我想走 elif 的逻辑’但是好像没有这样的语法,求人指教,谢谢

    elif response.xpath('//link[contains(@rel,"icon")]/@href'):
    rel_url=response.xpath('//link[contains(@rel,"icon")]/@href').extract()
    item['logo']=rel_url
    print(rel_url)
    8 replies    2018-12-03 16:48:30 +08:00
    kimchan
        1
    kimchan  
       Dec 3, 2018
    这... 没法看啊..
    whoami9894
        2
    whoami9894  
       Dec 3, 2018 via Android
    楼下有人能理解楼主想表达啥吗
    holajamc
        3
    holajamc  
       Dec 3, 2018
    if not apple_touch_url and response.xpath('//link[contains(@rel,"icon")]/@href'):
    猜的)
    kimchan
        4
    kimchan  
       Dec 3, 2018
    我猜是他需要在最上边加一句: apple_touch_url = None
    ant2017
        5
    ant2017  
       Dec 3, 2018
    我猜是判断变量是否定义?
    'apple_touch_url' in dir()
    Ewig
        6
    Ewig  
    OP
       Dec 3, 2018
    @kimchan 我不知道怎么排版
    Trim21
        7
    Trim21  
       Dec 3, 2018
    用 markdown 语法
    hahastudio
        8
    hahastudio  
       Dec 3, 2018
    @Ewig 贴 gist link 到评论里
    https://gist.github.com/
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   834 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 20:36 · PVG 04:36 · LAX 13:36 · JFK 16:36
    ♥ Do have faith in what you're doing.