Python urllib2.urlopen报错,如何成功执行呢?,,【1】url = "ht
Python urllib2.urlopen报错,如何成功执行呢?,,【1】url = "ht
【1】url = "http://www.baidu.com/s?wd=a b c"【2】url = "http://www.baidu.com/s?wd=a%20b%20c"response = urllib2.urlopen(url = url, timeout = timeout)
【1】会报错
【2】可以正常执行
想知道,对于这种url,有没有函数可以对其进行编码,以便成功执行呢?
目前做法:
使用urllib.urlencode()函数解决
如:
url = "http://www.baidu.com/s?"+urlencode({"wd":"a b c"})
不过urllib库新版本中不是不推荐使用了吗?
>>> urllib.quote('http://www.baidu.com/s?wd=a b c', safe=':/?=')'http://www.baidu.com/s?wd=a%20b%20c'>>> print 'http://www.baidu.com/s?wd=%s' % urllib.quote('a b c')'http://www.baidu.com/s?wd=a%20b%20c'>>>>>> import requests>>> requests.get('http://www.baidu.com/s', params={'wd': 'a b c'}).urlu'http://www.baidu.com/s?wd=a+b+c'
编橙之家文章,
相关内容
- 关于digitalocean部署django时遇到runserver抛出问题不会解,
- Python新手数据嵌套问题,python新手嵌套,大家好,我是一
- pypi中没有tkinter吗?难道需要手动安装,pypitkinter安装,
- Python递归函数时间复杂度如何判断,python递归函数判断
- python开发的微信企业付款出现CA证书error,pythonca,我在做
- Python sqlalchemy批量数据插入优化注意事项有哪些?,pyt
- Python程序里用pdb方法进行调试时出现错误,pythonpdb,调试
- Python序列sort与bisect.insort及heapq.heappush哪种效率最高效,
- Python有大数据处理性能优化好方法吗,python性能优化
- python requests发送post Page load erro问题,requestserro,post过去
评论关闭