2014-04-22 116 views
1

我试图用BeautifulSoup解析XML页面,由于某种原因,它不能够找到XML解析器。我不认为这是一个路径问题,因为我以前使用lxml解析页面,而不是XML。下面的代码:BeautifulSoup 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和我使用它使用了XML解析不同的方法BS4。谢谢。

回答

1

如果您已经安装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] 

让我们知道这会有所帮助。

+0

它必须是路径问题,因为它在iPython中工作,但不在Eclipse PyDev中。我会整理一下。谢谢你的提示。 – TomR

+0

这有点奇怪。很高兴知道你找到了问题的根源,但。在任何情况下,上述答案都可以作为解决方法。 :) – Manhattan