如何解决将beautifulsoup与带有编码头的xml文件一起使用的问题

2024-09-24 02:19:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我在编码版本的xml文件中使用beautifulsoup时遇到了这个问题

我有这个档案

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd“>

<modelVersion>4.0.0</modelVersion>
<artifactId>project</artifactId>
<packaging>pom</packaging>.....</project>

还有python代码

for file in files:

print(dir + file)

infile = open( dir + file,"r")
contents = infile.read()
soup = BeautifulSoup(contents, features ="xml")
print(soup.prettify())

打印的结果是

<?xml version="1.0" encoding="utf-8"?>

项目标记被忽略。它只发生在编码在第一行的文件中


Tags: 文件project编码versiondirxmlpackaginginfile
1条回答
网友
1楼 · 发布于 2024-09-24 02:19:32
import requests
from bs4 import BeautifulSoup

r = requests.get("http://maven.apache.org/xsd/maven-4.0.0.xsd")

soup = BeautifulSoup(r.text, 'xml')


print(soup)

相关问题 更多 >