如何使用python xml.etree.ElementTree解析eBay API响应? [英] how to use python xml.etree.ElementTree to parse eBay API response?

查看:89
本文介绍了如何使用python xml.etree.ElementTree解析eBay API响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用xml.etree.ElementTree来解析来自eBay查找API findItemsByProduct的响应.经过长时间的反复试验,我想到了这段代码,该代码可以打印一些数据:

I am trying to use xml.etree.ElementTree to parse responses from eBay finding API, findItemsByProduct. After lengthy trial and error, I came up with this code which prints some data:

import urllib
from xml.etree import ElementTree as ET

appID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
isbn = '3868731342'

namespace = '{http://www.ebay.com/marketplace/search/v1/services}'

url = 'http://svcs.ebay.com/services/search/FindingService/v1?' \
    + 'OPERATION-NAME=findItemsByProduct' \
    + '&SERVICE-VERSION=1.0.0' \
    + '&GLOBAL-ID=EBAY-DE' \
    + '&SECURITY-APPNAME=' + appID \
    + '&RESPONSE-DATA-FORMAT=XML' \
    + '&REST-PAYLOAD' \
    + '&productId.@type=ISBN&productId=' + isbn

root = ET.parse(urllib.urlopen(url)).getroot()

for parts in root:
    if parts.tag == (namespace + 'searchResult'):
        for item in list(parts):
            for a in list(item):
                if a.tag == (namespace + 'itemId'):
                    print 'itemId: ' + a.text
                if a.tag == (namespace + 'title'):
                    print 'title: ' + a.text

但这似乎不是很优雅,如何在不遍历所有属性并检查是否是我想要的属性的情况下获取"itemId"和"title"呢?我尝试使用 .get(namespace +'itemId') .find(namespace +'itemId') .attrib.get(namespace +'itemId'),但实际上没有任何作用.

But that seems not very elegant, how can I get the 'itemId' and 'title' without looping over all attributes and checking if it is the one I want? I tried using things like .get(namespace + 'itemId') and .find(namespace + 'itemId') and .attrib.get(namespace + 'itemId') but nothing really worked.

有人可以告诉我如何使用此API的某些python包装器来执行此操作吗?我看到了 easyBay pyeBay ,但我没有设法让他们中的任何一个去做我想做的事情.是否有任何值得使用的eBay python API?

Can someone maybe show me how to do this using some python wrapper for this API? I saw easyBay, ebay-sdk-python and pyeBay but I didn't manage to get any of them to do what I want. Is there any eBay python API which is worthwhile to use for this?

推荐答案

您可以使用 ElementTree .如果要获取商品,可以使用findall以及商品的路径,然后遍历商品列表:

You can use ElementTree. If you want to get the items you can use findall and the path to the items, then iterate over the list of items:

items = root.findall(namespace+'searchResult/'+namespace+'item')
for item in items: 
     item.find(namespace+'itemId').text
     item.find(namespace+'title').text

要直接从根目录获取第一个itemId:

To get directly to the first itemId from the root:

root.find(namespace+'searchResult/'+namespace+'item/'+namespace+'itemId')

基本上,find方法使用XPath检索子元素下一层以上的元素.另请参见 Effbot对ElementTree中XPath支持的解释

Basically, the find method uses XPath to retrieve elements more than one level below the subelements. See also Effbot's explanation of XPath support in ElementTree

这篇关于如何使用python xml.etree.ElementTree解析eBay API响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆