【发布时间】:2021-07-16 00:44:33
【问题描述】:
我正在尝试使用以下代码从以前用于相同目的的公开平台下载数据集。但是,我不确定为什么会收到此错误,也就是说,是因为代码中的错误还是网站(hatebase)API 的某些更改。任何建议都会非常有帮助。谢谢。
import json
import requests
import pandas as pd
from hatebase import HatebaseAPI
key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA" # this is the key
filepath = "/dictionary.csv"
hatebase = HatebaseAPI({"key": key})
filters = {"language": "eng"}
format = "json"
# initialize list for all vocabulary entry dictionaries
eng_vocab = []
response = hatebase.getVocabulary(filters=filters, format=format)
pages = response["number_of_pages"]
# fill the vocabulary list with all entries of all pages
# this might take some time...
for page in range(1, pages+1):
filters["page"] = str(page)
response = hatebase.getVocabulary(filters=filters, format=format)
eng_vocab.append(response["result"])
# create empty pandas df for all vocabulary entries
df_eng_vocab = pd.DataFrame()
# fill df
for elem in eng_vocab:
df_eng_vocab = df_eng_vocab.append(elem)
# reset the df index
df_eng_vocab.reset_index(drop=True, inplace=True)
# saving the file to csv
df_eng_vocab.to_csv(filepath)
我得到的错误如下
Please check your API-Key, Authentication did nod respond with a token.
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-60-de9970a761e5> in <module>
7 #filepath = "/dictionary.csv"
8
----> 9 hatebase = HatebaseAPI({"key": key})
10 filters = {"language": "eng"}
11 format = "json"
~\Anaconda3\lib\site-packages\hatebase\__init__.py in __init__(self, settings)
37 self.debug = settings["debug"]
38
---> 39 self.authenticate()
40
41 def authenticate(self):
~\Anaconda3\lib\site-packages\hatebase\__init__.py in authenticate(self)
57 print("Please check your API-Key, Authentication did nod respond with a token.")
58
---> 59 if token is not None:
60 self.token = response.json()["result"]["token"]
61 else:
UnboundLocalError: local variable 'token' referenced before assignment
【问题讨论】:
-
似乎模块有错误 - 作者忘记使用默认值
None创建变量token。或者它可能无法从服务器获取令牌并且它没有设置默认值token = None。可能你必须在源代码中挖掘来检查token。或者如果这个模块有 GitHub,那么在 GitHub 上检查它是否有一些修复。
标签: python python-3.x pandas api python-requests