3

我需要从此链接获取 FLVPath 的值:http ://www.testpage.com/v2/videoConfigXmlCode.php?pg=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

但是没有返回数据

4

3 回答 3

4

lxml.html用于解析文档,这会导致 lxml 将所有元素和属性名称小写(因为这在 html 中无关紧要),这意味着您必须使用:

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

或者,当您无论如何都在解析 xml 文件时,您可以使用lxml.etree.

于 2012-12-09T20:52:03.330 回答
2

你可以试试

print sub_data.attrib['Value']
于 2012-12-09T20:49:16.643 回答
0
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
于 2012-12-09T20:56:24.353 回答