gpt4 book ai didi

xml - 使用Python3和BeautifulSoup解析xml文件

转载 作者:行者123 更新时间:2023-12-02 03:28:40 29 4
gpt4 key购买 nike

我知道有关使用 Python 3 进行 xml 解析的问题有多个答案,但我找不到其中两个的答案。我正在尝试从 BoardGameGeek xml 文件中解析和提取信息,如下所示(对于我来说,粘贴到这里太长了):

https://www.boardgamegeek.com/xmlapi/boardgame/10

1) 我无法从这两行中提取主要游戏名称:

<name sortindex="1" primary="true">Elfenland</name>
<name sortindex="1">Elfenland (Волшебное Путешествие)</name>

2) 我在提取数据列表时也遇到了麻烦,例如在这个 xml 中:

<poll title="User Suggested Number of Players" totalvotes="96"  name="suggested_numplayers">
<results numplayers="1">
<result numvotes="0" value="Best"/>
<result numvotes="0" value="Recommended"/>
<result numvotes="58" value="Not Recommended"/>
</results>
<results numplayers="2">
<result numvotes="2" value="Best"/>
<result numvotes="21" value="Recommended"/>
<result numvotes="53" value="Not Recommended"/>
</results>
<results numplayers="3">
<result numvotes="10" value="Best"/>
<result numvotes="46" value="Recommended"/>
<result numvotes="17" value="Not Recommended"/>
</results>
<results numplayers="4">
<result numvotes="47" value="Best"/>
<result numvotes="36" value="Recommended"/>
<result numvotes="1" value="Not Recommended"/>
</results>
<results numplayers="5">
<result numvotes="35" value="Best"/>
<result numvotes="44" value="Recommended"/>
<result numvotes="2" value="Not Recommended"/>
</results>
<results numplayers="6">
<result numvotes="23" value="Best"/>
<result numvotes="48" value="Recommended"/>
<result numvotes="11" value="Not Recommended"/>
</results>
<results numplayers="6+">
<result numvotes="0" value="Best"/>
<result numvotes="1" value="Recommended"/>
<result numvotes="46" value="Not Recommended"/>
</results>
</poll>

目前,我的代码非常简单,如下所示。它仅提取简单的单值 xml 行。任何有关如何提取更复杂信息的帮助都会很棒。谢谢。

url = 'https://www.boardgamegeek.com/xmlapi/boardgame/10'
response = urllib.request.urlopen(url)
data = response.read() # a `bytes` object
text = data.decode('utf-8') # a `str`;
soup = BeautifulSoup(text,'xml')
yearpublished = soup.find_all('yearpublished')

最佳答案

对于第一部分,尝试搜索存在属性“primary”的元素“name”,如下所示:

from bs4 import BeautifulSoup
import urllib

url = 'https://www.boardgamegeek.com/xmlapi/boardgame/10'
response = urllib.request.urlopen(url)
data = response.read() # a `bytes` object
text = data.decode('utf-8') # a `str`;
soup = BeautifulSoup(text,'xml')
name = soup.find('name', primary = True)

print (name.get_text())

输出:

Elfenland

对于“结果”元素的第二个循环并提取所需的数据:

text = """
<poll title="User Suggested Number of Players" totalvotes="96" name="suggested_numplayers">
<results numplayers="1">
<result numvotes="0" value="Best"/>
...
<result numvotes="46" value="Not Recommended"/>
</results>
</poll>
"""
soup = BeautifulSoup(text,'xml')

for result in soup.find_all('results'):
numplayers = result['numplayers']
best = result.find('result', {'value': 'Best'})['numvotes']
recommended = result.find('result', {'value': 'Recommended'})['numvotes']
not_recommended = result.find('result', {'value': 'Not Recommended'})['numvotes']
print (numplayers, best, recommended, not_recommended)

输出:

1 0 0 58
2 2 21 53
3 10 46 17
4 47 36 1
5 35 44 2
6 23 48 11
6+ 0 1 46

或者,如果您想更优雅地找到所有每个属性并压缩它们:

soup = BeautifulSoup(text,'xml')
numplayers = [tag['numplayers'] for tag in soup.find_all('results')]
best = [tag['numvotes'] for tag in soup.find_all('result', {'value': 'Best'})]
recommended = [tag['numvotes'] for tag in soup.find_all('result', {'value': 'Recommended'})]
not_recommended = [tag['numvotes'] for tag in soup.find_all('result', {'value': 'Not Recommended'})]
print(list(zip(numplayers, best, recommended, not_recommended)))

输出:

[('1', '0', '0', '58'), ('2', '2', '21', '53'), ('3', '10', ' 46', '17'), ('4', '47', '36', '1'), ('5', '35', '44', '2'), ('6', ' 23', '48', '11'), ('6+', '0', '1', '46')]

关于xml - 使用Python3和BeautifulSoup解析xml文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52470518/

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