2017-09-15 46 views
0

我想凑这个网站提供的路径变化:如何从电子商务网站刮价格时scrapy

我的问题是,有2个不同clases和2型价格路径是不同的。

通常我会用

item['price'] = sel.xpath('.//*[@class="price_discount"]/text()').extract_first() 

做,但这种情况下,我还需要刮掉具有价格时,它在它没有提供“atg_store_newPrice”。

我可以使用不同的选择和使用它的价格选择里面,提取里面,像

item['price'] = sel.xpath('.//*[@class="price_discount"]/text()').extract_first(default='sel.xpath('.//*[@class="atg_store_newPrice"]/text()').extract_first()') 

当然,让无效的语法,但也许如果我使用一个不同的选择,我可以做到这一点?像price2

item['price2'] = sel.xpath('.//*[@class="atg_store_newPrice"]/text()').extract_first() 

然后

item['price'] = sel.xpath('.//*[@class="price_discount"]/text()').extract_first(default='price2') 

敢肯定这是不可能的了,所以我想询问一下如何解决这个问题,我有建议。

回答

0

我不知道如果我理解正确的话你的需求,但是你可以使用这个XPath来涵盖这两种选择:

item['price'] = sel.xpath('(.//*[@class="price_discount"]|.//*[@class="atg_store_newPrice"])/text()').extract_first() 
+0

感谢爵士。它完美的作品。 –