有没有办法让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/