簡體   English   中英

Python&lxml / xpath:解析XML

[英]Python & lxml / xpath: Parsing XML

我需要從此鏈接獲取FLVPath的值: http//www.testpage.com/v2/videoConfigXmlCode.php? ppg = video_29746_no_0_extsite

from lxml import html

sub_r = requests.get("http://www.testpage.co/v2/videoConfigXmlCode.php?pg=video_%s_no_0_extsite" % list[6])
sub_root = lxml.html.fromstring(sub_r.content)

for sub_data in sub_root.xpath('//PLAYER_SETTINGS[@Name="FLVPath"]/@Value'):
     print sub_data.text

但沒有數據返回

您正在使用lxml.html來解析文檔,這會導致lxml小寫所有元素和屬性名稱(因為這在html中無關緊要),這意味着您必須使用:

sub_root.xpath('//player_settings[@name="FLVPath"]/@value')

或者當您正在解析xml文件時,您可以使用lxml.etree

你可以試試

print sub_data.attrib['Value']
url = "http://www.testpage.com/v2/videoConfigXmlCode.php?pg=video_29746_no_0_extsite"
response = requests.get(url)

# Use `lxml.etree` rathern than `lxml.html`, 
# and unicode `response.text` instead of `response.content`
doc = lxml.etree.fromstring(response.text)

for path in doc.xpath('//PLAYER_SETTINGS[@Name="FLVPath"]/@Value'):
     print path

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM