2013-07-01 36 views
0

我想從頁面獲取數據,如http://www.site.com/list?a=data&b=data ... 我從site.com的頁面檢索所有這些網址。當試圖打開鏈接時,我得到錯誤:TypeError:預期的BaseHandler實例,得到。與urllib2已打開的網址已獲取參數

我的猜測是,該網址需要「編碼」,但如何?

感謝您的幫助!


編輯: 好,這裏是代碼,因此,所有的連接由我代理服務器通過,並嘗試打開早期發現類似上述的網址。

代碼:

tileurl = 'http://www.site.com/list?a=data&b=data' 
proxy = SocksiPyHandler(socks.PROXY_TYPE_SOCKS4, '192.168.0.190', 12500) 
opener = urllib2.build_opener(proxy) 
opener.addheaders = [('User-agent', 'Mozilla/5.0')] 
infile = opener.open(tileurl) 
tile_bin = infile.read() 

Traceback (most recent call last): 
File "C:\Users\Jean-michel\Dropbox\Projects\Python Code\Maps Saver\map.py", line 89, in <module> opener = urllib2.build_opener(tileurl) 
File "C:\Python27\lib\urllib2.py", line 490, in build_opener opener.add_handler(h) 
File "C:\Python27\lib\urllib2.py", line 326, in add_handler type(handler)) 
TypeError: expected BaseHandler instance, got type 'str' 
+1

先顯示您的代碼。 – alecxe

+0

您是否可以選擇安裝[請求](http://docs.python-requests.org/en/latest/)並僅使用它?正確使用比標準庫的等價物更容易使用。 –

+0

URL上是否有引號?你能複製並粘貼實際的回溯信息,以便我們看到未經編輯的錯誤信息嗎? –

回答

0
tileurl = tile.replace(t1, "") ## Removing the parameters from the url 
p = urlparse.parse_qs(t1) ## decoding the parameter 
tileparam = urllib.urlencode(p) ## encoding the parameter... 

問題解決了! :)