自从我编码以来已经有很长时间了。最近回到它,这就是我想要做的。我要从Google电子表格中获取VINS,然后使用请求库将GET请求发送到URL,然后该URL对该VIN进行解码并返回数据(https://vpic.nhtsa.dot.gov/api/)。
到目前为止,这就是我要做的事情,我可以从电子表格中获取VIN并显示它,但是当我尝试发送GET请求时,Python为我返回了很多错误。这是我第一次使用Python进行编码,花了一些时间才将代码移到现在。
def main():
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
# Call the Sheets API
SPREADSHEET_ID = 'The Sheets ID that I use'
RANGE_NAME = 'A:A'
result = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID,
range=RANGE_NAME).execute()
values = result.get('values', [])
if not values:
print('No data found.')
else:
for row in values:
# Print columns A, which correspond to indices 0.
print('%s' % (row[0]))
URL = "https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVin/"
VIN=row[0]
PARAMS={VIN}
r=requests.get(url=URL, params=PARAMS)
data=r.json
r.status_code
text=r.text
print(text)
if __name__ == '__main__':
main()
以下是我得到的错误,但我确实得到了VIN(此处未提及):
Traceback (most recent call last):
File "quickstart.py", line 47, in <module>
main()
File "quickstart.py", line 40, in main
r=requests.get(url=URL, params=PARAMS)
File "/Library/Python/2.7/site-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/Library/Python/2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 498, in request
prep = self.prepare_request(req)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 441, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/Library/Python/2.7/site-packages/requests/models.py", line 309, in prepare
self.prepare_url(url, params)
File "/Library/Python/2.7/site-packages/requests/models.py", line 427, in prepare_url
enc_params = self._encode_params(params)
File "/Library/Python/2.7/site-packages/requests/models.py", line 97, in _encode_params
for k, vs in to_key_val_list(data):
ValueError: too many values to unpack
我如何确定可能是什么问题?我经过很长时间的编码,并且是Python的新手。
答案 0 :(得分:1)
通过查看他们的文档,您就可以
VIN=row[0]
r=requests.get(url=URL + VIN, params={'format': 'json'})
代替