2012-07-12 11 views
5

sto cercando di analizzare recensione da questa pagina: http://www.amazon.co.uk/product-reviews/B00143ZBHYNon in grado di analizzare HTML usando lxml Xpath parser

Utilizzando seguente approccio:

Codice

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 

uscita

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 ,: Mentre si utilizza lo stesso xpath su xpath checker addon di firefox, sono in grado di farlo facilmente. Ma nessun risultato qui, per favore aiuto!

+0

non so perché Chrome ha mostrato tbody in XPath :( – codersofthedark

+0

E 'generati automaticamente – fedosov

risposta

7

Provare a rimuovere /tbody modulo XPath - non c'è <tbody> in .

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] 

uscita:.

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

lol stupido errore, il suo funzionamento ora, grazie :) – codersofthedark

+0

posso accettare la risposta solo dopo il 15 minuti dal postare la domanda, aspetta che lo farei in 3 minuti – codersofthedark

+1

@dragosrsupercool Non è un errore stupido, leggi qui: http://stackoverflow.com/a/5586627/1167879 –

Problemi correlati