[英]Beautifulsoup add/insert html tag line in xml document
I want to add a line with HTML tag using beautifulsoup.我想使用beautifulsoup 添加一行带有HTML 标记的行。
Input:输入:
<tr>
<td>Hi </td>
<td>Hello</td>
<td>World</td>
</tr>
Output:I want to add the 4th line which is Good Morning in seperate tag输出:我想在单独的标签中添加第 4 行早上好
<tr>
<td>Hi </td>
<td>Hello</td>
<!--<td>World</br>Good Morning</td> -->
<td></br>Good Morning</td>
</tr>
I made the comment using replacewith function, now i'm stuck with adding new line.我使用 replacewith 函数发表了评论,现在我坚持添加新行。 please help me on this.
请帮我解决这个问题。
First find the element and then next_element
which is text and then replace with your text.首先找到元素,然后是文本的
next_element
,然后用您的文本替换。
from bs4 import BeautifulSoup
html='''<tr>
<td>Hi </td>
<td>Hello</td>
<td>World</td>
</tr>'''
soup=BeautifulSoup(html,'html.parser')
soup.find('td',text='World').next_element.replace_with("</br>Good Morning")
print(soup.prettify(formatter=None))
Output :输出:
<tr>
<td>
Hi
</td>
<td>
Hello
</td>
<td>
</br>Good Morning
</td>
</tr>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.