import urllib.parse url = ‘http://www.baidu.com/index.html‘; data = { ‘name‘:‘bowen‘, ‘age‘:25, ‘sex‘:‘male‘, } lt = [] for k,v in data.items(): lt.append(k+‘=‘+str(v)) # 这个是在for循环外面的,就是将列表的元素之间用&符号连接起来 query_string = ‘&‘.join(lt) print(lt) print(query_string) url = url + ‘?‘+query_string print(url)
方法二: 其实上面的程序是将字典按照传统方式拼接的,一般不用
import urllib.parse url = ‘http://www.baidu.com/index.html‘; data = { ‘name‘:‘bowen‘, ‘age‘:25, ‘sex‘:‘male‘, } data1 = { ‘name‘:‘博文‘, ‘age‘:25, ‘sex‘:‘male‘, } query_string = urllib.parse.urlencode(data) print(query_string) url1 = url + ‘?‘+query_string print(url1) query_string1 = urllib.parse.urlencode(data1) url2 = url + ‘?‘+query_string1 print(url2)
原文地址:https://www.cnblogs.com/sunnybowen/p/10177063.html
时间: 2024-11-04 00:16:13