Python爬虫:url中带字典列表参数的编码转换方法-创新互联-成都快上网建站

Python爬虫:url中带字典列表参数的编码转换方法-创新互联

平时见到的url参数都是key-value, 一般vlaue都是字符串类型的

梧州ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

如果有幸和我一样遇到字典,列表等参数,那么就幸运了

python2代码

import json
from urllib import urlencode

# 1. 直接将url编码
params = {
  "name": "Tom",
  "hobby": ["ball", "swimming"],
  "books": [{"name": "语文", "pages": 20}]
}

print(urlencode(params))
"""
hobby=%5B%27ball%27%2C+%27swimming%27%5D&
books=%5B%7B%27name%27%3A+%27%5Cxe8%5Cxaf%5Cxad%5Cxe6%5Cx96%5Cx87%27%2C+%27pages%27%3A+20%7D%5D&
name=Tom
"""

# 2. 先用json序列化字典和列表参数
params = {
  "name": "Tom",
  "hobby": json.dumps(["ball", "swimming"]),
  "books": json.dumps([{"name": "语文", "pages": 20}])
}

print(urlencode(params))

"""
hobby=%5B%22ball%22%2C+%22swimming%22%5D&
books=%5B%7B%22name%22%3A+%22%5Cu8bed%5Cu6587%22%2C+%22pages%22%3A+20%7D%5D&
name=Tom
"""

网站题目:Python爬虫:url中带字典列表参数的编码转换方法-创新互联
网页地址:http://kswjz.com/article/ddoccd.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流