如何使用 python xml.etree.ElementTree 解析 eBay API 响应?
我正在尝试使用 xml.etree.ElementTree 解析来自 eBay 查找 API findItemsByProduct 的响应。经过长时间的试验和错误,我想出了这段打印一些数据的代码:
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')
但没有真正起作用。
有人可以告诉我如何使用这个 API 的 python 包装器来做到这一点吗? 我看到了 easyBay、ebay-sdk-python 和 pyeBay< /a> 但我没能让他们做我想做的事。有没有值得使用的 eBay python API?
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
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
ElementTree
。如果您想获取项目,可以使用 findall 和项目的路径,然后迭代项目列表:直接从根获取第一个 itemId:
基本上,find 方法使用 XPath 检索多个元素级别低于子元素。另请参阅 Effbot 对 ElementTree 中 XPath 支持的说明
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:To get directly to the first itemId from the root:
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