python - 如何用python格式化url中的字符串?

标签 python string url format

我可以使用 1 个变量格式化链接 (link1),但是当我想使用 2 个变量格式化链接 (link2) 时,它会抛出错误( ValueError: 索引 94 处不支持的格式字符 'A' (0x41)) link2 应该是:https://widget.s24.com/applications/1e082c83/widgets/422/products?searchTerm=Topper&origin=https%3A%2F%2Fwww.real.de%2Fproduct%2F324093002%2F

有什么解决办法吗?

i = 324093002
b = "Topper"
link_product = 'https://www.real.de/product/%s/'
link_keyword = 'https://widget.s24.com/applications/1e082c83/widgets/422/products?searchTerm=*%s*&origin=https%3A%2F%2Fwww.real.de%2Fproduct%2F*%s*%2F'
link1 = link_product %i
link2 = link_keyword % (i, b)

最佳答案

Char % 在字符串格式中具有特殊含义 - 例如 %s%i 等 - 并且您有 % 3A %2F 格式不正确。您必须使用 %% 通知 Python 您需要普通字符 % - %%3A %%2F

i = 324093002
b = "Topper"
link_keyword = 'https://widget.s24.com/applications/1e082c83/widgets/422/products?searchTerm=*%s*&origin=https%%3A%%2F%%2Fwww.real.de%%2Fproduct%%2F*%s*%%2F'

link2 = link_keyword % (i, b)
print(link2)

关于python - 如何用python格式化url中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65199173/

相关文章:

MySQL 前导 0 的日期格式

java - 将字符串两个字符反转两个字符

r - 以编程方式构建公式而无需字符串操作

swift - Foundation 的字符串编码不是网站所期望的

javascript - 将 URL 查询字符串解析为数组

python - 从 h5 文件中读取 n 行

python - 多元线性回归: appending an array on ones to Matrix of features (Python)

python - 如何在Python中转换带有标题的数据框中的摘要输出?

python - 在 Python 中根据请求发送 PIL 图像

python - Django 子 url 不显示其模板