Python BS4与SDMX

2024-06-16 12:23:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想检索SDMX文件中给定的数据(比如https://www.bundesbank.de/cae/servlet/StatisticDownload?tsId=BBK01.ST0304&its_fileFormat=sdmx&mode=its)。我试图使用beauthoulsoup,但它似乎看不到标签。在下面的代码中

import urllib2
from bs4 import BeautifulSoup 
url = "https://www.bundesbank.de/cae/servlet/StatisticDownload?tsId=BBK01.ST0304&its_fileFormat=sdmx"
html_source = urllib2.urlopen(url).read()
soup = BeautifulSoup(html_source, 'lxml')
ts_series = soup.findAll("bbk:Series")

给了我一个空对象。在

BS4是错误的工具,还是(更有可能)我做错了什么? 提前谢谢


Tags: httpsimportwwwdeurllib2itsservletsdmx
1条回答
网友
1楼 · 发布于 2024-06-16 12:23:24

soup.findAll("bbk:series")将返回结果。在

事实上,在这种情况下,即使您使用lxml作为解析器,BeautifulSoup仍然将其解析为html,因为html标记是不区分大小写的,beauthoulsoup会对所有标记进行分解,因此soup.findAll("bbk:series")也能工作。见官方文件Other parser problems。在

如果要将其解析为xml,请改用soup = BeautifulSoup(html_source, 'xml')。它还使用lxml,因为lxmlxml解析器BeautifulSoup拥有的。现在您可以使用ts_series = soup.findAll("Series")来获得结果,因为beautifulSoup将剥离名称空间部分bbk。在

相关问题 更多 >