Home > database >  How to add values to url on python request
How to add values to url on python request

Time:12-15

I wanna get data from an api and I use this: requests.get("https://www.metaweather.com/api/location/woeid/2013/4/i/"). I want woeid to be a variable and i be an integer.

CodePudding user response:

You can use a f-string to substitute variable names for values in a string.

woeid = "2487956"
i = 12
url = f"https://www.metaweather.com/api/location/{woeid}/2013/4/{i}/"
print(url)
response = requests.get(url)

The url becomes: https://www.metaweather.com/api/location/2487956/2013/4/12/

CodePudding user response:

The longer and more time consuming version of CodeMonkey's answer would be

woeid = "2487956"
i = 12
url = "https://www.metaweather.com/api/location/"   woeid   "/2013/4"   i "/"
print(url)
response = requests.get(url)

Reason why I added this is because, string interpolation isnt supported by all languages.

  • Related