search

Home  >  Q&A  >  body text

python - BeautifulSoup append包含尖括号会被转换成实体

from bs4 import BeautifulSoup
    html="<html><body></body></html>"
    soup = BeautifulSoup(html)
    soup.body.append("<test")
    print soup
    "


   输出结果:
"<html><body>&lt;test</body></html>

怎样才能使尖括号不被转换成html实体呢?
如:
<html><body><test</body></html>

伊谢尔伦伊谢尔伦2892 days ago768

reply all(2)I'll reply

  • PHP中文网

    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

    reply
    0
  • 黄舟

    黄舟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

    reply
    0
  • Cancelreply