2016-08-22 46 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 

畢竟,長字符串可能包含奇怪的字符。