python3中urllib的简单用法
因为python3 较python2 在urllib库有一些合并改变,所以备注一下以后用到的话可直接粘贴。
- GET方式
url = 'http://www.baidu.com'
response = urllib.request.urlopen(url)
result = response.read().decode('utf-8')
print(result)
- POST方式
url = 'https://shuju.wdzj.com/plat-info-59.html'
data = urllib.parse.urlencode({'type':1,'target1':2,'target2':0,'wdzjPlatId':59}).encode(encoding='UTF8')
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor())
request = urllib.request.Request(url)
response = opener.open(request, data)
result = response.read().decode('utf-8')
print (result)
注意:
在python3中使用urlopen打开url时不可有中文否则会报错(类似这样:UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 38-39: ordinal not in range(128))。
#错误写法示例
url = 'https://movie.douban.com/j/search_subjects?type=movie&tag='热门'&sort=recommend&page_limit=20&page_start=0'
此时可以这样写:
url = 'https://movie.douban.com/j/search_subjects?type=movie&tag='+ urllib.parse.quote('热门') +'&sort=recommend&page_limit=20&page_start=0'