如果我知道那个
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.