简体   繁体   中英

Params in requests (python)

I have the following url:

url = 'http://api.worldweatheronline.com/premium/v1/weather.ashx?key=KEY_VALUE&q=48.85,2.35&num_of_days=2&tp=3&format=json'

which I want to access using requests Python library. If I insert this as the url, it does return the expected data ( requests.get(url) ). My problem arises when trying to connect using the params parameter. My approach was the following one:

urlPrefix = 'http://api.worldweatheronline.com/premium/v1/weather.ashx'
parameters = {'key': key, 'q': {48.85,2.35}, 'num_of_days':2, 'tp': 3, 'format': 'json'}

print(requests.get(urlPrefix, params=parameters).url)
>>> http://api.worldweatheronline.com/premium/v1/weather.ashx?key=KEY_VALYE&tp=3&q=48.85&q=2.35&format=json&num_of_days=2

As you can see, the url will vary due to the representation of {48.85,2.35} . Therefore, my question is: how should q value represented in order to have the same url request as in the first request?

For the q representation it would be best to group the entire thing as a string. ie 'q':{'48.85,2.35'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM