python - 如何让 Beautifulsoup 不添加 <html> 或 <?xml ?>

标签 python html xml beautifulsoup

有没有办法让beautifulsoup不添加<?xml version="1.0" encoding="utf-8"?>在 xml 文件的开头或 <html> </html>标签?

我读过 bs4 doc并尝试了 xml、html 和 lxml 解析器,但结果相似。 我还测试了 soup.find('?xml') ,这不会返回任何内容。

$ python
Python 2.7.5 (default, Aug  2 2016, 04:20:16) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup
>>> xml='<tag>value</tag>'
>>> soup=BeautifulSoup(xml,'xml')
>>> soup
<?xml version="1.0" encoding="utf-8"?>  <=============== 
<tag>value</tag>
>>> soup.find('?xml')
>>> soup=BeautifulSoup(xml,'html')
>>> soup
<html><body><tag>value</tag></body></html>  <===========
>>> soup=BeautifulSoup(xml,'lxml')
>>> soup
<html><body><tag>value</tag></body></html>  <===========
>>> 

最佳答案

#You can directly print the tag (or body). Hope this helps.
from bs4 import BeautifulSoup
xml='<tag>value</tag>'
soup=BeautifulSoup(xml,'lxml')
soup.tag  #or soup.body

#Output:
<tag>value</tag>

关于python - 如何让 Beautifulsoup 不添加 <html> 或 <?xml ?>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43191946/

相关文章:

php - 为什么标题小部件不显示在 Google Chrome 上?

python - 操作错误 : no such table: entries while "init_db()" already inserted in flaskr. py

javascript - 将图像文件放在 node.js 中的什么位置?

javascript - 将我的 Web 横幅图片链接到 URL

xml - 强类型 xml 属性组合的提示(构建器)

iPhone - 存储与文件相关的字符串数据的位置

python - 如何将求和或替换等操作分配给变量

python - 如何对 Python 列表进行切片,以便将列移动为单独的元素列?

python - Celery 和 Django - 没有名为 'django' 的模块

xml - This OR That 或 This AND That 的最有效模式