Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
url使用的URI无效?在python请求模块中_Python_String_Python Requests_Escaping - Fatal编程技术网

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检查下面我的答案try
str(i)
而不是
i
一个更安全的选择是字符串格式或python库函数@Shelly检查下面我的答案