我可以使用 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/