如何在python请求URL中使用可变数量

时间:2018-04-10 08:11:16

标签: python-3.x python-requests

可以在请求中使用可变数量而不是' 201804101602  (python 3)?

import requests
url = "http://127.0.0.1:8080/school/class201804101456"
headers = {'name': 'tom201804101456'}
response = requests.request("GET", url, headers=headers)
print(response.text)

1 个答案:

答案 0 :(得分:0)

因为它们只是字符串,为什么不设置变量然后将它附加到上面的示例中的类和tom?

例如:

import requests
token = "201804101456"
url = "http://127.0.0.1:8080/school/class" + token
nameHeader = "tom" + token
headers = {'name': nameHeader}
response = requests.request("GET", url, headers=headers)
print(response.text)