我正在尝试使用 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/