小编Kon*_*rad的帖子

将标记字符串附加到BeautifulSoup中的标记

是否可以将标记设置为标记内容(类似于innerHtmlJavaScript中的设置)?

举个例子,假设我想在a中添加10个<a>元素<div>,但用逗号分隔:

soup = BeautifulSoup(<<some document here>>)

a_tags = ["<a>1</a>", "<a>2</a>", ...] # list of strings
div = soup.new_tag("div")
a_str = ",".join(a_tags)
Run Code Online (Sandbox Code Playgroud)

使用div.append(a_str)逃逸<>&lt;&gt;,所以我结束了

<div> &lt;a1&gt; 1 &lt;/a&gt; ... </div>
Run Code Online (Sandbox Code Playgroud)

BeautifulSoup(a_str)把它包裹起来<html>,我看到把树当作一个不雅的黑客.

该怎么办?

python string markup beautifulsoup html-parsing

4
推荐指数
1
解决办法
5178
查看次数

标签 统计

beautifulsoup ×1

html-parsing ×1

markup ×1

python ×1

string ×1