Python3的urllib模块基本使用urlencode和quote

2025-05-09 19:31:46

1、打开开发工具IDLE,新建‘urlencode.py’文件,并写代码如下:import urllib.requestcity = '上海'key = 'yourkey'dvar = { 'city':city, 'key':key }incode = urllib.parse.urlencode(dvar)print (incode)print (urllib.parse.quote(dvar))这里用的是某和的天气接口,key仅做演示

Python3的urllib模块基本使用urlencode和quote

3、测试quote函数改写代码如下;import urllib.requestcity = '上海'key = 'yourkey'dvar = { 'city':city, 'key':key }incode = urllib.parse.urlencode(dvar)print (incode)print (urllib.parse.quote(city))

Python3的urllib模块基本使用urlencode和quote

5、测试发送请求,修改代码如下;import urllib.requestcity = '上海'key = 'yourkey'dvar = { 'city':city, 'key':key }incode = urllib.parse.urlencode(dvar)url = 'http://apis.juhe.cn/simpleWeather/query?'+incodes = urllib.request.urlopen(url)print (s.read())

Python3的urllib模块基本使用urlencode和quote

7、使用quote就只能自行拼接字符串请求了,注意解码响应内容,代码如下:import urllib.requestcity = '上海'key = 'yourkey&#泌驾台佐39;dvar = { 'city':city, 'key':key }incode = urllib.parse.urlencode(dvar)url = 'http://apis.juhe.cn/simpleWeather/query?'+'city='+urllib.parse.quote(city)+'&key='yourkey's = urllib.request.urlopen(url)print (s.read().decode('utf8'))

Python3的urllib模块基本使用urlencode和quote
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢