我想检索SDMX文件(如https://www.bundesbank.de/cae/servlet/StatisticDownload?tsId=BBK01.ST0304&its_fileFormat=sdmx&mode=its)中给定的数据。我尝试使用BeautifulSoup,但它似乎看不到标签。在下面的代码中
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是错误的工具吗,或者(更有可能的)我做错了什么?提前感谢
发布于 2016-09-16 21:58:48
soup.findAll("bbk:series")将返回结果。
事实上,在这种情况下,即使您使用lxml作为解析器,BeautifulSoup仍然会将其解析为html,因为html标签是大小写敏感的,BeautifulSoup会将所有标签小写,因此soup.findAll("bbk:series")可以工作。请参阅官方文档中的Other parser problems。
如果您希望将其解析为xml,请改用soup = BeautifulSoup(html_source, 'xml')。它还使用lxml,因为lxml是BeautifulSoup唯一的xml解析器。现在,您可以使用ts_series = soup.findAll("Series")获得结果,因为beautifulSoup将剥离名称空间部分bbk。
https://stackoverflow.com/questions/39532776
复制相似问题