【发布时间】:2022-12-20 21:05:23
【问题描述】:
我决定使用 BeautifulSoup 从 Pandas 列中提取字符串整数。 BeautifulSoup 适用于一个简单的示例,但不适用于 Pandas 中的列表列。我找不到任何错误。你能帮我吗?
输入:
df = pd.DataFrame({
"col1":[["<span style='color: red;'>9</span>", "abcd"], ["a", "b, d"], ["a, b, z, x, y"], ["a, y","y, z, b"]],
"col2":[0, 1, 0, 1],
})
for list in df["col1"]:
for item in list:
if "span" in item:
soup = BeautifulSoup(item, features = "lxml")
item = soup.get_text()
else:
None
print(df)
期望的输出:
df = pd.DataFrame({
"col1":[["9", "abcd"], ["a", "b, d"], ["a, b, z, x, y"], ["a, y","y, z, b"]],
"col2":[0, 1, 0, 1],
})
【问题讨论】:
标签: python html pandas beautifulsoup xml-parsing