所以我编码已经很久了。最近重新回到这里,这就是我想要做的事情。我从Google电子表格中获取VINS,然后使用Requests库向URL发送GET请求,然后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完全陌生。
从他们的文档来看,你想要的
VIN=row[0]
r=requests.get(url=URL + VIN, params={'format': 'json'})
代替