I'm doing a requests.get call with the following url:
https://api.datasource.com/apps/ios/ranking?countries=NL&categories=Overall > Kids > 5 & Under&device=ios&ranks=1000
I get an error message with "categories" has an illegal value "Overall which is due to the & sign in Overall > Kids > 5 & Under
What's the best approach for escaping this character?
Instead of passing the query parameters directly in the URL, requests lets you pass them in a dictionary params and will handle the URL encoding. (In this case, you need to escape the ampersand.)
In [15]: params = dict(countries='NL', categories='Overall > Kids > 5 & Under', device='ios', ranks=1000)
In [16]: requests.get("http://www.example.com", params=params)
Out[16]: <Response [200]>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With