我正尝试使用以下方法提取数据:
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
如何设置“?”作为字符串,并且仅获取“?”在网址中而不是%5C?
答案 0 :(得分:2)
您可以使用字符串格式来创建结果网址(string.format或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'
或者您可以使用urllib.parse.urlunsplit库来创建您的网址,例如
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'