user1142618
user1142618

Reputation: 221

lxml to parse html:wrong result,why

Here is a strange thing with my code:

import lxml.html
myxml='''
<cooperate>
    <job DecreaseHour="1" table="tpa_radio_sum">  
    </job>

    <job DecreaseHour="2" table="tpa_radio_sum">                                
    </job>


    <job DecreaseHour="3" table="tpa_radio_sum">
    </job>
</cooperate>
'''
root=lxml.html.fromstring(myxml)
nodes1=root.xpath('//job[@DecreaseHour="1"]')
nodes2=root.xpath('//job[@table="tpa_radio_sum"]')    
print "nodes1=",nodes1
print "nodes2=",nodes2

What I get is:
nodes1=[] and

nodes2=[ Element job at 0x1241240,    
 Element job at 0x1362690,     
 Element job at 0x13626c0]

Why nodes1 is []? It's a so strange thing. Why ?

Upvotes: 1

Views: 267

Answers (1)

newtover
newtover

Reputation: 32094

Since you are using html parser all attributes become lower-case:

>>> root.xpath("//job")[0].attrib
{'table': 'tpa_radio_sum', 'decreasehour': '1'}

You can use the real xml-parser:

>>> import lxml.etree
>>> root = lxml.etree.fromstring(myxml)
>>> root.xpath('job[@DecreaseHour="1"]')
[<Element job at 0x293daa8>]

Upvotes: 5

Related Questions