python - BeautifulSoup XML 解析不起作用

标签 python xml beautifulsoup urllib2 lxml

我正在尝试使用 BeautifulSoup 解析 XML 页面,但由于某种原因,它无法找到 XML 解析器。我不认为这是路径问题,因为我过去使用 lxml 来解析页面,而不是 XML。代码如下:

from bs4 import *
import urllib2
import lxml
from lxml import *


BASE_URL = "http://auctionresults.fcc.gov/Auction_66/Results/xml/round/66_115_database_round.xml"

proxy = urllib2.ProxyHandler({'http':'http://myProxy.com})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
page = urllib2.urlopen(BASE_URL)

soup = BeautifulSoup(page,"xml") 

print soup

我可能错过了一些简单的东西,但是我在这里找到的所有带有 BS 问题的 XML 解析都是围绕 bs3 的,而我正在使用 bs4,它使用不同的方法来解析 XML。谢谢。

最佳答案

如果您安装了 lxml,只需将其称为 BeautifulSoup 的解析器即可,如下所示。

代码:

from bs4 import BeautifulSoup as bsoup
import requests as rq

url = "http://auctionresults.fcc.gov/Auction_66/Results/xml/round/66_115_database_round.xml"
r = rq.get(url)

soup = bsoup(r.content, "lxml")
print soup

结果:

<html><body><dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:nonamespaceschemalocation="66_database.xsd"><all_bids>
<auction_id>66</auction_id>
<auction_description>Advanced Wireless Services</auction_description>
... really long list follows...
[Finished in 34.9s]

请告诉我们这是否有帮助。

关于python - BeautifulSoup XML 解析不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23223352/

相关文章:

python - 如何在 python 中重新训练机器学习模型,直到获得期望的结果

python - 出错时退出多处理程序

python - 在一个脚本中更新列表并从另一个脚本访问更新的列表

xml - 如何指定 Nuget 架构?

python - BeautifulSoup 4 : Remove comment tag and its content

python - 索引错误: list index out of range While Trying to Scrape Specific Columns on a Table?

python - BeautifulSoup : Remove Tags that only contain href

python - 字典理解中的嵌套循环

xml - JSON 架构与 XML 架构

java - 如何使用 JPA (Java EE) 将 XML 数据结构映射到数据库