url使用的URI无效?在python请求模块中
我尝试使用以下方法提取数据:url使用的URI无效?在python请求模块中,python,string,python-requests,escaping,Python,String,Python Requests,Escaping,我尝试使用以下方法提取数据: 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?' 如果我使用: urlall = url+'/'+i+'.json'+'?'+'page='+str(page) Then I got
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?'
如果我使用:
urlall = url+'/'+i+'.json'+'?'+'page='+str(page)
Then I got another error: can only concatenate str (not "_io.TextIOWrapper") to str
如何将“?”设置为字符串并在url中仅获取“?”而不是%5C?您可以使用字符串格式创建结果url(或) 或者,您可以使用库来创建url,例如
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'
您可以使用字符串格式创建结果url(或) 或者,您可以使用库来创建url,例如
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'
尝试
str(i)
而不是i
一个更安全的选择是字符串格式或python库函数@Shelly检查下面我的答案trystr(i)
而不是i
一个更安全的选择是字符串格式或python库函数@Shelly检查下面我的答案