0

I was trying to extract data using:

urlall = url+'/'+i+'.json'+'\\?'+'page='+str(page)

r = requests.get(urlall)

I got an error 400 Client Error: Invalid URI for url: the '\?' turned out to be '%5C?' 

if I use:

urlall = url+'/'+i+'.json'+'?'+'page='+str(page)

Then I got another error: can only concatenate str (not "_io.TextIOWrapper") to str

How can I set '?' as a string and get only the '?' in the url instead of %5C?

2
  • try str(i) instead of i Commented Jun 17, 2019 at 6:16
  • A safer alternative is string formatting or a python library function @Shelly check my answer below Commented Jun 17, 2019 at 7:29

1 Answer 1

2

You can use string formatting to create your resultant url (string.format or f-strings)

In [4]: url = 'http.example.com'                                                                                                          
In [5]: i = 1                                                                                                                             
In [8]: page = 1                                                                                                                          
#f-strings for python>=3.6
In [10]: f'{url}/{i}.json?page={page}'                                                                                                    
Out[10]: 'http.example.com/1.json?page=1'
#String formatting
In [11]: '{}/{}.json?page={}'.format(url, i, page)                                                                                        
Out[11]: 'http.example.com/1.json?page=1'

Or you can use urllib.parse.urlunsplit library to create your url, for example

In [1]: from urllib.parse import urlunsplit                                                                                               

In [2]: urlunsplit(['http','example.com','1.json','page=1',''])                                                                           
Out[2]: 'http://example.com/1.json?page=1'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.