2012-07-12 22 views
5

我想從這個頁面解析審查,以解析HTML:http://www.amazon.co.uk/product-reviews/B00143ZBHY無法使用lxml的Xpath的解析器

使用以下方法:

代碼

html # a variable which contains exact html as given at the above page. 
from lxml import etree 
tree = etree.HTML(html) 
r = tree.xpath(".//*[@id='productReviews']/tbody/tr/td[1]/div[9]/text()[4]") 
print len(r) 
print r[0].tag 

輸出

0 
Traceback (most recent call last): 
    File "c.py", line 37, in <module> 
    print r[0].tag 
IndexError: list index out of range 

p,s ,:在Firefox的xpath檢查器插件上使用相同的xpath時,我可以輕鬆地解決它。但這裏沒有結果,請幫忙!

+0

不知道爲什麼,鉻顯示了XPath TBODY :( – codersofthedark 2012-07-12 19:24:24

+0

它自動生成 – fedosov 2012-07-12 21:44:11

回答

7

嘗試從表中刪除/tbody XPath - 在#productReviews中沒有<tbody>

import urllib2 
html = urllib2.urlopen("http://www.amazon.co.uk/product-reviews/B00143ZBHY").read() 
from lxml import etree 
tree = etree.HTML(html) 
r = tree.xpath(".//*[@id='productReviews']/tr/td[1]/div[9]/text()[4]") 
print r[0] 

輸出:

bought this as replacement for the original cover which came with my greenhouse and which ripped in the wind. so far this seems a good replacement although for some reason it seems slightly too small for my greenhouse so that i cant zip both sides of the front at the same time. seems sturdier and thicker than the cover i had before so hoping it lasts a bit longer! 
+0

笑愚蠢的錯誤,它現在的工作,謝謝:) – codersofthedark 2012-07-12 19:16:08

+0

我能接受的答案後,才15分鐘發佈的問題,等待我會做3分鐘 – codersofthedark 2012-07-12 19:20:45

+1

@dragosrsupercool這不是一個愚蠢的錯誤,請閱讀這裏:http://stackoverflow.com/a/5586627/1167879 – 2012-11-21 00:27:50