URL 正在替换为特殊字符 python

问题描述 投票:0回答:1

这是我的网址:https://10.10.10.20/v1/device[]=1_2234

当我在 Python 脚本中发送 URL 时,它会将 [] 替换为 %5B 和 %5D。

import requests

url = 'https://10.10.10.20/v1/device[]=1_2234'
response = requests.get(api_key,  url)
print(response)

错误:

requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://10.10.10.20/v1/device%5B%5D=1_2234
python-3.x
1个回答
0
投票

尝试在每个括号前使用反斜杠转义括号

url = 'https://10.10.10.20/v1/device\[\]=1_2234'

您还可以通过在字符串之前放置 r 来使用原始字符串

url = r'https://10.10.10.20/v1/device[]=1_2234'

如何在Python中编写字符串文字而不必转义它们?.

© www.soinside.com 2019 - 2024. All rights reserved.