from bs4 import BeautifulSoup
html="<html><body></body></html>"
soup = BeautifulSoup(html)
soup.body.append("<test")
print soup
"
输出结果:
"<html><body><test</body></html>
怎样才能使尖括号不被转换成html实体呢?
如:<html><body><test</body></html>
PHP中文网2017-04-17 14:26:45
http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#beautifulsoup-new-string-new-tag
If you want to add a tag, please use new_tag()
It is not a good practice to write a tag directly in append()
Also, angle brackets will be escaped in web pages, otherwise it may cause label exceptions or something
黄舟2017-04-17 14:26:45
The author checked whether it has something to do with the python version, bs version or file encoding. I tested it locally and there was no problem, as shown in the picture
To add: the native python is 2.7.5, the bs version is beautifulsoup4-4.3.2, and the encoding is utf8 without BOM