Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python 2.6中使用XML API findall()时出错_Python_Xml_Api_Alexa - Fatal编程技术网

在python 2.6中使用XML API findall()时出错

在python 2.6中使用XML API findall()时出错,python,xml,api,alexa,Python,Xml,Api,Alexa,我使用下面的代码从Alexa API检索信息,这段代码在Python2.7上运行良好,但我必须使用Python2.6,这给了我一个错误:findall()正好接受2个参数(给定3个) 我假设这个方法在Python2.7中有所改变,但我不知道如何使它在2.6中工作 NS_PREFIXES = { "alexa": "http://alexa.amazonaws.com/doc/2005-10-05/", "awis": "http://awis.amazonaws.com/doc/

我使用下面的代码从Alexa API检索信息,这段代码在Python2.7上运行良好,但我必须使用Python2.6,这给了我一个错误:findall()正好接受2个参数(给定3个)

我假设这个方法在Python2.7中有所改变,但我不知道如何使它在2.6中工作

NS_PREFIXES = {
    "alexa": "http://alexa.amazonaws.com/doc/2005-10-05/",
    "awis": "http://awis.amazonaws.com/doc/2005-07-11",
}

tree = api.sites_linking_in(domain + ".eu", count=10, start=0)
alexa_sites_linkin_in = {}
for element in tree.findall('//awis:SitesLinkingIn/awis:Site',NS_PREFIXES):
    alexa_sites_linkin_in.update({
    element.find('awis:Title', NS_PREFIXES).text: element.find('awis:Url', "awis").text})

感谢您的帮助。

api使用
lxml
(ElementTree作为后端口)解析xml。
lxml
允许附加参数-命名空间,但ElementTree不允许。这就是问题所在。 因此,作为修补程序,我建议安装lxml。

使用Python2.6(及更早版本),您需要手动注册名称空间并将其解析为Clark表示法,然后才能识别
find()

首先,注册名称空间,如下所述:

接下来,您需要自己将名称空间的XPath解析为Clark表示法,
find()
在内部使用。例如,
awis:Title
解析为
{http://awis.amazonaws.com/doc/2005-07-11}标题

def resolved_xpath(xpath, namespace):
    result = xpath
    for short_name, url in namespace.items():
        result = re.sub(r'\b' + short_name + ':', '{' + url + '}', result)
    return result
现在,编写修改后的
find()
findall()
就很容易了,即使在Python 2.6中,它也尊重名称空间:

def find_with_namespace(element, xpath, namespace):
    return element.find(resolved_xpath(xpath, namespace))

def findall_with_namespace(element, xpath, namespace):
    return element.findall(resolved_xpath(xpath, namespace))
您的示例可以实现为:

NS_PREFIXES = {
    "alexa": "http://alexa.amazonaws.com/doc/2005-10-05/",
    "awis": "http://awis.amazonaws.com/doc/2005-07-11",
}

tree = api.sites_linking_in(domain + ".eu", count=10, start=0)
alexa_sites_linkin_in = {}
for element in findall_with_namespace(tree, '//awis:SitesLinkingIn/awis:Site',NS_PREFIXES):
    title = find_with_namespace(element, 'awis:Title', NS_PREFIXES).text
    url = find_with_namespace(element, 'awis:Url', NS_PREFIXES).text
    alexa_sites_linkin_in[title] = url

因此,是的,如果可能,使用
lxml

您是否使用xml.etree.ElementTree中的
findall
函数?如果文档只提到一个参数(不包括self),我不确定这在Python 2.7中如何工作。它尝试导入xml.etree.cElementTree,如果失败,则导入xml.etree.ElementTree。
NS_PREFIXES = {
    "alexa": "http://alexa.amazonaws.com/doc/2005-10-05/",
    "awis": "http://awis.amazonaws.com/doc/2005-07-11",
}

tree = api.sites_linking_in(domain + ".eu", count=10, start=0)
alexa_sites_linkin_in = {}
for element in findall_with_namespace(tree, '//awis:SitesLinkingIn/awis:Site',NS_PREFIXES):
    title = find_with_namespace(element, 'awis:Title', NS_PREFIXES).text
    url = find_with_namespace(element, 'awis:Url', NS_PREFIXES).text
    alexa_sites_linkin_in[title] = url