2010-03-24 38 views
1

我需要下载一个CSV文件,使用浏览器工作正常:python urllib2.openurl不能使用特定的URL(重定向)?

http://www.ftse.com/objects/csv_to_csv.jsp?infoCode=100a&theseFilters=&csvAll=&theseColumns=Mw==&theseTitles=&tableTitle=FTSE%20100%20Index%20Constituents&dl=&p_encoded=1&e=.csv

下面的代码适用于任何其他文件(URL)(用完全合法的路径),但上述网址是下载800字节的乱码。

def getFile(self,URL): 

    proxy_support = urllib2.ProxyHandler({'http': 'http://proxy.REMOVED.com:8080/'}) 
    opener = urllib2.build_opener(proxy_support) 
    urllib2.install_opener(opener) 
    response = urllib2.urlopen(URL) 
    print response.geturl() 
    newfile = response.read() 
    output = open("testFile.csv",'wb') 
    output.write(newfile) 
    output.close() 
+0

适合我,没有代理服务器。也许代理(缓存/访问控制)麻烦。 – gimel 2010-03-24 17:34:49

回答

1

urllib2在引擎盖下使用httplib,因此诊断这个问题的最佳方法是打开http连接调试。在访问网址之前添加此代码,您应该得到一个完整的HTTP流量正在生成的摘要:

import httplib 
httplib.HTTPConnection.debuglevel = 1