在python3中,將urllib和urllib2合併了,因此在使用urllib2的地方改爲urllib.request便可。
示例以下python
- import urllib2.request
- url = 'http://apis.baidu.com/heweather/weather/free?city=beijing'
- req = urllib.request.Request(url)
- req.add_header("apikey","33481140591d83456d6486e55fb00686")
- resp = urllib.request.urlopen(req)
- content = resp.read()
- if(content):
- print(content)
-
-
改例是在經過百度提供的api獲取城市的天氣。web