Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requests.get not working with & character

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?

like image 678
user3642173 Avatar asked Mar 19 '26 20:03

user3642173


1 Answers

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]>
like image 68
Katriel Avatar answered Mar 21 '26 09:03

Katriel