2016-08-22 47 views
-1
from lxml import html 
import requests 
import time 


#Gets prices 
page = requests.get('https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=hi') 
tree = html.fromstring(page.content) 
price = tree.xpath('//h2[@data-attribute="Hi Guess the Food - What’s the Food Brand in the Picture"]/text()') 

print(price) 

这只能返回[]如何让代码使用xpath返回文本?

当寻找到page.content,它显示了亚马逊的反僵尸的东西。我怎样才能绕过这个?

+0

那么,你有没有看过'page.content'? – vaultah

回答

0

当您尝试从某个网站上删除某些内容时,需要提供一个常规建议。首先看看返回的内容,在这种情况下,尝试任何东西之前page.content。你错误地假设亚马逊允许你很好地获取他们的数据,但当他们没有。

0

我觉得urllib2的是更好的,和XPath可能是:

price = c.xpath('//div[@class="s-item-container"]//h2')[0] 
print price.text 

毕竟,长字符串可能包含奇怪的字符。