-6ren"> -from bs4 import BeautifulSoup xmlcontent = "some text with " bs = BeautifulSoup(xmlcontent, "xml") p-6ren">
gpt4 book ai didi

python-2.7 - 在 Beautiful Soup 中使用 ="1.0"时如何删除

转载 作者:行者123 更新时间:2023-12-03 23:21:10 26 4
gpt4 key购买 nike

from bs4 import BeautifulSoup

xmlcontent = "some text with <tags>"

bs = BeautifulSoup(xmlcontent, "xml")

print bs

输出:
<?xml version="1.0" encoding="utf-8"?>
some text with <tags>

是否可以不输出:
<?xml version="1.0" encoding="utf-8"?>

我知道是否使用 lxml , 删除添加的 <body>我可以做的标签:
bs = BeautifulSoup(xmlcontent, "lxml")

print bs.body.next

是否有与 xml 相同的用法?以便不包括 xml 版本和编码?

我选择使用 xmllxml因为被解析的内容通常都是 xml 格式 - 这是最好的选择还是我可以只使用 lxml对于 xml 内容?

最佳答案

这似乎有效:

from bs4 import BeautifulSoup

xmlcontent = "some text with <tags>"

bs = BeautifulSoup(xmlcontent, "xml")

bs = bs.encode_contents()

print type(bs) # it's a string

print bs

# some text with <tags>

关于python-2.7 - 在 Beautiful Soup 中使用 ="1.0"时如何删除 <?xml version ="utf-8"encoding "xml"?>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19217792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com