2010-03-24 34 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