如果我知道该元素或类的id,如何在一些HTML元素中使用Beautiful Soup设置值?

前端之家收集整理的这篇文章主要介绍了如果我知道该元素或类的id,如何在一些HTML元素中使用Beautiful Soup设置值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我知道那个 HTML元素或类的id,如何在某个元素中使用Beautiful Soup设置值?
比如我有

< td id =“test”>< / td>

我想设置文本RESTORE …喜欢

< td id =“test”> RESTORE …< / td>.

解决方法

使用find()搜索id = test查找要修改标记.然后:

BeautifulSoup Documentation – “Modifying the tree”

Modifying .string

If you set a tag’s .string attribute,the tag’s contents are replaced with the string you give:

markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)

tag = soup.a
tag.string = "New link text."
tag
# <a href="http://example.com/">New link text.</a>

Be careful: if the tag contained other tags,they and all their contents will be destroyed.

原文链接:https://www.f2er.com/html/224186.html

猜你在找的HTML相关文章