已经有ur参数的urllib2打开url

时间:2013-07-01 20:27:57

标签: python urllib2

我想从http://www.site.com/list?a=data&b=data等网页获取数据... 我从site.com的页面检索所有这些URL。当试图打开一个链接时,我得到错误:TypeError:期望的BaseHandler实例,得到了。

我的猜测是网址需要“编码”但是如何?

感谢您的帮助!


编辑: 好的,这是代码,所以所有连接都通过我的代理服务器并尝试打开之前找到的网址,如上所述。

代码:

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 个答案:

答案 0 :(得分: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...

问题解决了!! :)