使用内部函数时Requests.get失败

时间:2018-01-11 08:09:08

标签: python authentication get python-requests

我正在使用python请求库从adzuna api获取数据。

如果我尝试

r = requests.get("http://api.adzuna.com/v1/api/jobs/gb/search/1?app_id=appID\
    &app_key=appKEY&results_per_page=50&\
    what=entry%20level&content-type=application/json")
print r.text

它正在抓取我的数据 但如果我把它包裹在一个功能

def getData ():
    r = requests.get("http://api.adzuna.com/v1/api/jobs/gb/search/1?app_id=appID\
    &app_key=appKey&results_per_page=50&\
    what=entry%20level&content-type=application/json")
    print r.text

getData()

它给了我以下异常

{"display":"Authorisation failed","__CLASS__":"Adzuna::API::Response::Exception","doc":"http://api.adzuna.com/v1/doc","exception":"AUTH_FAIL"}

这个功能有什么问题?

See the Image for more info

2 个答案:

答案 0 :(得分:0)

In the second url you have a sapce between gb/search/ and the 1 after him.

答案 1 :(得分:0)

(edited) Since using a single-line query fixed your problem, I'll recommend that you don't try to build the query yourself but instead let requests do it for you, like so:

query = {'app_id': 'appID',
         'app_key': 'appKey',
         'content-type': 'application/json',
         'results_per_page': '50',
         'what': 'entry level'}
r = requests.get('http://api.adzuna.com/v1/api/jobs/gb/search/1', params=query)

This should be less error-prone and is certainly more convenient.