我正在尝试使用以下代码从以前用于相同目的的公开平台下载数据集。但是,我不确定为什么会出现此错误,也就是说,是否因为代码错误或网站(仇恨库)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
答案 0 :(得分:1)
编辑:
查看源代码后发现可以设置current version来解决问题
hatebase = HatebaseAPI({"key": key, "version": "4-4"})
原始版本:
我在 Hatebase.org 注册以获得 API Key
,但我没有选择 price plan
。
我使用 source code 并在函数 print()
中添加了 authenticate()
以查看`来自服务器的 JSON 数据
print(response.json())
try:
token = response.json()["result"]["token"]
except KeyError as e:
print("Please check your API-Key, Authentication did nod respond with a token.")
并且它在 JSON
'The version of the API is now retired;
please update your queries to resume accessing the API'
源代码有 version = '4-2'
行,但 documentation 显示当前版本是 4.4
,所以我更改为 version = '4-4'
并且它停止显示此错误。
因为我没有选择 price plan
所以现在我得到了
'You must assign a plan to your account to access the API'
我不会选择price plan
,所以我不知道它是否可能需要其他更改。
您可以将带有 class HatebaseAPI
的源代码复制到您的文件中并更改 version = '4-4'
,也许它对您有用。
我将此问题作为 issues
在 GitHub 上发送给模块的作者:newer API version - 4.4。
也许它会更正模块(或不会)。