python - 用 beautifulSoup 解析不同的 bs4.element.Tag

标签 python html url beautifulsoup

我想解析这个 url 中的表格并将其导出为 csv:

http://www.bde.es/webbde/es/estadis/fi/ifs_es.html

如果我这样做:

sauce = urlopen(url_bank).read()
soup = bs.BeautifulSoup(sauce, 'html.parser')

然后是:

resto = soup.find_all('td')
lista_text = []
for elements in resto:
    lista_text = lista_text + [elements.string]

除最后一列“Códigos Isin”外,我对所有元素都进行了很好的解析 这是因为 html 代码有中断 '.我不知道 该怎么做,我已经尝试过这部分但仍然不起作用:

lista_text = lista_text + [str(elements.string).replace('<br/>','')]

之后,我将列表放入 np.array,然后放入数据框,将其导出为 .csv。那部分已经完成,我只需要解决那个问题。

提前致谢!

最佳答案

只是你需要注意什么 .string 确实 - 如果有多个子元素,它将返回 None - 与 <br> 的情况一样:

If a tag contains more than one thing, then it’s not clear what .string should refer to, so .string is defined to be None

使用 .get_text()相反:

for elements in resto:
    lista_text = lista_text + [elements.get_text(strip=True)]

关于python - 用 beautifulSoup 解析不同的 bs4.element.Tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44071591/

相关文章:

python - 从 numpy 类型获取位数的简单方法?

css - 带边框半径的 HTML5 和 CSS 标题视频

javascript - float 按钮不会在单击时更改背景图像

python - Sklearn AdaBoost 与多项式 SVM 预测全 1

python - 无法在 Azure - web.config 上部署基本 Python 3.6?

python - parse_args 似乎不理解 json 参数

html - 什么时候一起使用 dfn 和 dt?

html - 错误: The requested URL “[no URL]” ,无效

java - JSP 定义值

php - curl 'malformed url'