【发布时间】:2020-05-21 23:04:30
【问题描述】:
我有以下html:
<body><h3>Full Results for race 376338</h3>"Category","Position","Name","Time","Team"<br>"A","1","James","20:20:00","5743"<br><br>"A","2","Matt","20:15:00"<br>
它像<br> # some text <br> 一样持续数百行。
我想在每个
创建一个新行,所以它是这样的 CSV 格式:
<body><h3>Full Results for race 376338</h3>"Category","Position","Name","Time","Team"
<br>"A","1","James","20:20:00","5743"<br>
<br>"A","2","Matt","20:15:00"<br>
我有这个代码:
soup = BeautifulSoup(html_string, features="html.parser")
for br in soup.find_all('br'):
soup.replace_with("\n")
这样我得到了错误:ValueError: Cannot replace one element with another when the element to be replaced is not part of a tree.
我需要改变什么?
【问题讨论】:
-
html_string('br')应该做什么?我想你的意思是soup.find_all('br')。 -
html_string是什么,检查一下类型。可能是字节串。 -
@JohnGordon 正确,我做到了,但是无论我有
body还是br,都会返回ValueError: Cannot replace one element with another when the element to be replaced is not part of a tree.。 -
@PythonIsBae,你能发布更新的代码吗?
-
@PythonIsBae,尝试在
br上应用 replace_with 而不是汤。
标签: python html beautifulsoup