python 在 URL 中增加数字

python increment number in the URL

有什么方法可以使我拥有的 URL 中的两个不同数字递增?其中一个数字将以 -1 开头。第二个数字将从 0 开始。

url = "https://mywebsite.com/amp"
for count in range(-1, 5):
    print(url + str(count)) 

这给了我

https://mywebsite.com/amp-1
https://mywebsite.com/amp0
https://mywebsite.com/amp1
https://mywebsite.com/amp2
https://mywebsite.com/amp3
https://mywebsite.com/amp4

我正在努力实现这个

https://mywebsite.com/amp=-1&id=0
https://mywebsite.com/amp=0&id=1
https://mywebsite.com/amp=1&id=2
https://mywebsite.com/amp=2&id=3
https://mywebsite.com/amp=3&id=4

试试这个:

url = "https://mywebsite.com/amp"
for count in range(-1, 5):
    print(f"{url}/amp={count}&id={count+1}")

输出:

https://mywebsite.com/amp/amp=-1&id=0
https://mywebsite.com/amp/amp=0&id=1
https://mywebsite.com/amp/amp=1&id=2
https://mywebsite.com/amp/amp=2&id=3
https://mywebsite.com/amp/amp=3&id=4
https://mywebsite.com/amp/amp=4&id=5

请记住,如果您希望查询字符串将值作为适当的参数传递,您需要在第一个 /amp 之后添加一个 ?。像这样:

https://mywebsite.com/amp?amp=-1&id=0

使用str.format():

for n in range(-1, 4):
    print('{}?amp={}&id={}'.format(url, n, n+1))