python的xpath问题

2024-10-01 04:46:58 发布

您现在位置:Python中文网/ 问答频道 /正文

elementtree非常新,所以我尝试为xbmc的tv插件解析xml文件。下面是我遇到问题的代码。我认为我的xpath不正确,placeholder不能处理属性!在

这是我正在处理的xml文件-http://services.tvrage.com/myfeeds/episode_list.php?key=ag6txjP0RH4m0c8sZk2j&sid=2930

    seasonnum = root2.findall("/Show/Episodelist/Season[@no='%s']/episode/seasonnum" % (season))


        import xml.etree.ElementTree as ET
        import urllib            
        tree2 = ET.parse(urllib.urlopen(url))
        root2 = tree2.getroot()
        seasonnum = tree2.findall("./Episodelist/Season[@no='%s']/episode/seasonnum" % '1')
        print seasonnum

需要synrror分隔符[)是什么


Tags: 文件noimportxmlurllibetseasonelementtree
3条回答

我试过你的例子,而且很管用。以下是一个浓缩的完整版本:

import urllib
import xml.etree.ElementTree as ET

url = 'http://services.tvrage.com/myfeeds/episode_list.php?key=ag6txjP0RH4m0c8sZk2j&sid=2930'
tree = ET.parse(urllib.urlopen(url))
seasons = tree.findall("./Episodelist/Season[@no='%s']/episode/seasonnum" % '1')

for s in seasons:
    print s.text

我能想到的唯一问题是,不知何故,您下载了部分XML文档——不太可能,但我不知道其他任何解释。请注意,上面的脚本取自您的问题。我只添加了for循环。在

根据^{} documentation - XPath support

This module provides limited support for XPath expressions for locating elements in a tree. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the module.

您可能需要像^{}这样的第三方库来使用XPath。在

示例:

>>> import lxml.etree
>>>
>>> url = 'http://services.tvrage.com/myfeeds/episode_list.php?key=ag6txjP0RH4m0c8sZk2j&sid=2930'
>>> tree = lxml.etree.parse()
>>> tree.xpath("/Show/Episodelist/Season[@no='%s']/episode/seasonnum/text()" % 1)
['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']

更新

要使用lxml.etree.ElementTree,应该稍微修改xpath:

^{pr2}$

使用ElementTree:

>>> from xml.etree import ElementTree
>>> import urllib2
>>> url = 'http://services.tvrage.com/myfeeds/episode_list.php?key=ag6txjP0RH4m0c8sZk2j&sid=2930'
>>> request = urllib2.Request(url, headers={"Accept" : "application/xml"})
>>> u = urllib2.urlopen(request)
>>> tree = ElementTree.parse(u)
>>> rootElem = tree.getroot()
>>> [s.text for s in rootElem.findall('.//Season[@no="2"]/episode/seasonnum')]
['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', 
 '15', '16', '17', '18', '19', '20', '21', '22']

相关问题 更多 >