【发布时间】:2020-12-01 04:14:58
【问题描述】:
我已经为此苦苦挣扎了一段时间。
给定以下 XML 文件
<?xml version='1.0' encoding='UTF-8'?>
<html>
<body>
<feed xml:base="https:newrecipes.org"
xmlns="http://www.w3.org/2005/Atom"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<id>https://recipes.com</id>
<title>Cuisine</title>
<updated>2020-08-10T08:48:56.800Z</updated>
<link href="Cuisine" rel="self" title="Cuisine"/>
<entry>
<id>https://www.cuisine.org(53198770598313985)</id>
<category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="DefaultNamespace.Cuisine"></category>
<title></title>
<updated>1970-01-01T00:00:00.000Z</updated>
<content type="application/xml">
<m:properties>
<d:id m:type="Edm.Int64">53198770598313985</d:id>
<d:name m:type="Edm.String">American</d:name>
</m:properties>
</content>
</entry>
<entry>
<id>https://www.cuisine.org(53198770598313986)</id>
<category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="DefaultNamespace.Cuisine"></category>
<title></title>
<updated>1970-01-01T00:00:00.000Z</updated>
<content type="application/xml">
<m:properties>
<d:id m:type="Edm.Int64">53198770598313986</d:id>
<d:name m:type="Edm.String">Asian</d:name>
</m:properties>
</content>
</entry>
</feed>
</body>
</html>
使用 BeautifulSoup 我想出了以下解决方案,以便使用子组合器从条目标签中获取 id。
from bs4 import BeautifulSoup
import re
# Make a BS object to parse the xml string.
xml_soup = BeautifulSoup(xml_string, features="lxml")
# Use the child combinator to select the ids that are direct descendants of entry
cuisine_ids_unparsed = xml_soup.select("entry > content")
# Get the ids from the Tag value using regex.
# Then return the first occurrence of the regex found.
cuisine_ids = [re.findall(r"\((.*)\)", cuisine_id.text)[0] for cuisine_id in cuisine_ids_unparsed]
这将返回文件中 <id> 标记括号中的所有美食 ID。但我也想访问每个entry 中的properties。因为这些包含菜品的 id 和名称,无需任何解析。
不幸的是,使用 css 中的 Child 组合器(>)我无法更深入,我想知道是否有更好的方法,而不是迭代元素以提取值。比如:
cuisine_ids_unparsed = xml_soup.select("entry > content > properties > id")
检索所有 id 和
cuisine_names_unparsed = xml_soup.select("entry > content > properties > name")
检索所有名称。
【问题讨论】:
标签: python css xml beautifulsoup