1
解析XML對於該XML與etree的Python
<locations>
<location>
<locationid>1</locationid>
<homeID>281</homeID>
<buildingType>Added</buildingType>
<address>A</address>
<address2>This is address2</address2>
<city>This is city/city>
<state>State here</state>
<zip>1234</zip>
</location>
<location>
<locationid>2</locationid>
<homeID>81</homeID>
<buildingType>Added</buildingType>
<address>B</address>
<address2>This is address2</address2>
<city>This is city/city>
<state>State here</state>
<zip>1234</zip>
</location>
.
.
.
.
<location>
<locationid>10</locationid>
<homeID>21</homeID>
<buildingType>Added</buildingType>
<address>Z</address>
<address2>This is address2</address2>
<city>This is city/city>
<state>State here</state>
<zip>1234</zip>
</location>
</locations>
我怎樣才能locationID
的地址A
,使用etree
。
這裏是我的代碼,
import urllib2
import lxml.etree as ET
url="url for the xml"
xmldata = urllib2.urlopen(url).read()
# print xmldata
root = ET.fromstring(xmldata)
for target in root.xpath('.//location/address[text()="A"]'):
print target.find('LocationID')
獲取輸出None
,哪些錯誤我在做什麼嗎?
試試這個'.//location/[normalize-space(address)="A「]' – Naren
@Naren謝謝,試過這個但是不行。 – fledgling