Jay*_*uso 24 python beautifulsoup
试图用BS来解决html构建问题.
我正在尝试插入新标签:
self.new_soup.body.insert(3, """<div id="file_history"></div>""")
Run Code Online (Sandbox Code Playgroud)
当我检查结果时,我得到:
<div id="file_histor"y></div>
Run Code Online (Sandbox Code Playgroud)
所以我正在插入一个为websafe html进行清理的字符串..
我期望看到的是:
<div id="file_history"></div>
Run Code Online (Sandbox Code Playgroud)
如何div
在ID为3的位置插入新标签file_history
?
Guy*_*ely 25
请参阅有关如何附加标记的文档:
soup = BeautifulSoup("<b></b>")
original_tag = soup.b
new_tag = soup.new_tag("a", href="http://www.example.com")
original_tag.append(new_tag)
original_tag
# <b><a href="http://www.example.com"></a></b>
new_tag.string = "Link text."
original_tag
# <b><a href="http://www.example.com">Link text.</a></b>
Run Code Online (Sandbox Code Playgroud)
Bir*_*rei 12
使用工厂方法创建新元素:
new_tag = self.new_soup.new_tag('div', id='file_history')
Run Code Online (Sandbox Code Playgroud)
并插入它:
self.new_soup.body.insert(3, new_tag)
Run Code Online (Sandbox Code Playgroud)
其他答案直接来自文档.这是捷径:
from bs4 import BeautifulSoup
temp_soup = BeautifulSoup('<div id="file_history"></div>')
# BeautifulSoup automatically add <html> and <body> tags
# There is only one 'div' tag, so it's the only member in the 'contents' list
div_tag = temp_soup.html.body.contents[0]
# Or more simply
div_tag = temp_soup.html.body.div
your_new_soup.body.insert(3, div_tag)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28884 次 |
最近记录: |