gpt4 book ai didi

python - 如何使用 lxml 抓取 XML 文档的特定部分?

转载 作者:太空宇宙 更新时间:2023-11-04 09:19:34 26 4
gpt4 key购买 nike

我正在使用 Amazon 的 API 接收有关图书的信息。我正在尝试使用 lxml 来提取我的应用程序所需的 XMl 文档的特定部分。不过,我不太确定如何使用 lxml。据我所知:

root = etree.XML(response)

为 XML 文档创建一个 etree 对象。

XML 文档如下所示: http://pastebin.com/GziDkf1a“Items”其实有多个,但我只贴了其中一个给大家举个具体的例子。对于每个项目,我想提取标题和 ISBN。如何使用我拥有的 etree 对象执行此操作?

<ItemSearchResponse><Items><Item><ItemAttributes><Title>I want this info</Title></ItemAttributes></Item></Items></ItemSearchResponse

<ItemSearchResponse><Items><Item><ItemAttributes><ISBN>And I want this info</ISBN></ItemAttributes></Item></Items></ItemSearchResponse

基本上,我不知道如何使用我的 etree 对象遍历树,但我想学习如何操作。

编辑 1:我正在尝试以下代码:

tree = etree.fromstring(response)
for item in tree.iterfind(".//"+AMAZON_NS+"ItemAttributes"):
print(item)
print(item.items()) # Apparently, there is nothing in item.items()
for key, value in item.items():
print(key)
print(value)

但我得到以下输出: http://dpaste.com/287496/

我添加了 print(item.items()),它似乎只是一个空列表。虽然每个项目都是一个元素,但由于某种原因,它们没有项目。

编辑 2:我可以使用下面的代码来获取我想要的信息,但看起来 lxml 必须有更简单的方法......(这种方式似乎不是很有效):

for item in tree.iterfind(".//"+AMAZON_NS+"ItemAttributes"):
title_text = ""
author_text = ""
isbn_text = ""
for isbn in item.iterfind(".//"+AMAZON_NS+"ISBN"):
isbn_text = isbn.text
for title in item.iterfind(".//"+AMAZON_NS+"Title"):
title_text = title.text
for author in item.iterfind(".//"+AMAZON_NS+"Author"):
author_text = author.text
print(title_text + " by " + author_text + " has ISBN: " + isbn_text)

最佳答案

这已经过测试,可以与运行 Python 2.7.1 的 lxml.etree 和 xml.etree.cElementTree 一起使用。

import lxml.etree as ET
# Also works with cElementTree (included in recent standard CPythons).
# Use this import:
# import xml.etree.cElementTree as ET
t = ET.fromstring(xmlstring) # your data -- with 2 missing tags added at the end :-)
AMAZON_NS = "{http://webservices.amazon.com/AWSECommerceService/2009-10-01}"
# Find all ItemAttributes elements.
for ia in t.iter(AMAZON_NS+'ItemAttributes'):
# An ItemAttributes element has *children* named ISBN, Title, Author, etc.
# NOTE WELL: *children* not *attributes*
for tag in ('ISBN', 'Title'):
# Find the first child with that name ...
elem = ia.find(AMAZON_NS+tag)
print "%s: %r" % (tag, elem.text)

输出:

ISBN: '0534950973'
Title: 'Introduction to the Theory of Computation'

如果您想要生成包含 ItemAttributes 节点的所有子节点的字典,只需稍作改动即可:

import lxml.etree as ET
# Also works with cElementTree (included in recent standard CPythons).
# Use this import:
# import xml.etree.cElementTree as ET
from pprint import pprint as pp
t = ET.fromstring(xmlstring)
AMAZON_NS = "{http://webservices.amazon.com/AWSECommerceService/2009-10-01}"
TAGPOS = len(AMAZON_NS)
# Find all ItemAttributes elements.
for ia in t.iter(AMAZON_NS+'ItemAttributes'):
item = {}
# Iterate over all the children of the ItemAttributes node
for elem in ia:
# remove namespace stuff from key, remove extraneous whitepace from value
item[elem.tag[TAGPOS:]] = elem.text.strip()
pp(item)

输出是:

{'Author': 'Michael Sipser',
'Binding': 'Hardcover',
'DeweyDecimalNumber': '511.35',
'EAN': '9780534950972',
'Edition': '2',
'ISBN': '0534950973',
'IsEligibleForTradeIn': '1',
'Label': 'Course Technology',
'Languages': '',
'ListPrice': '',
'Manufacturer': 'Course Technology',
'NumberOfItems': '1',
'NumberOfPages': '400',
'PackageDimensions': '',
'ProductGroup': 'Book',
'ProductTypeName': 'ABIS_BOOK',
'PublicationDate': '2005-02-15',
'Publisher': 'Course Technology',
'Studio': 'Course Technology',
'Title': 'Introduction to the Theory of Computation',
'TradeInValue': ''}

关于python - 如何使用 lxml 抓取 XML 文档的特定部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4456421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com