我是一名python/scrapy新手。我想刮一個網站的做法,基本上當數據-SRC存在,那麼我想從IMG :: ATTR(數據-SRC)使用值,python如何做else語句
def parse(self, response):
for item in items:
listing['img'] = item.css('div img::attr(data-src)').extract()
否則如果數據 - SRC不存在,則使用IMG :: ATTR(SRC)
listing['img'] = item.css('div img::attr(src)').extract()
但問題是如何做到這一點的if語句?
listing['img'] = if item.css('div img::attr(data-src)'): item.css('div img::attr(data-src)').extract() else: item.css('div img::attr(src)').extract()
當然,這並不工作,它有一個錯誤:
SyntaxError: invalid syntax
感謝您的幫助和投入!
的可能的複製[函數和如果 - 其他在python中。 Codeacademy](http://stackoverflow.com/questions/15149667/functions-and-if-else-in-python-codeacademy) –
@ BoltClock♦你是對的,它確實應該是關於使用多個CSS選擇器的最佳方式。 –