我知道关于使用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')
答案 0 :(得分:1)
在第一部分中,尝试搜索元素“名称”,其中存在属性“主要”,如下所示:
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
对于“ results”元素的第二个循环,并提取所需的数据:
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')]