【问题标题】:Error in downloading data using API (python)使用 API (python) 下载数据时出错
【发布时间】: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


【解决方案1】:

编辑:

查看源码发现可以设置current version解决问题

hatebase = HatebaseAPI({"key": key, "version": "4-4"})

原始版本:

我在Hatebase.org注册了API Key,但我没有选择price plan


我取了source code 并在函数authenticate() 中添加了print() 以查看来自服务器的`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',也许它对您有用。


我在 GitHub 上将这个问题以 issues 的身份发送给模块的作者:newer API version - 4.4
也许它会纠正模块(或不纠正)。

【讨论】:

  • 谢谢。这绝对是问题所在。我现在在这里找到了一个直接的解决方案:gist.github.com/noahgh221/68ae96c3f6dc41c9596fed704fdc454d 有效
  • 查看源码后发现可以运行HatebaseAPI({"key": key, 'version': '4-4'})
  • 实际上不是。它仍然显示相同的错误。
  • 它可以显示与令牌相同的错误,但它可能有不同的问题 - 只有 JSON 数据中的文本可以解释它。在我的回答中,我对令牌有同样的错误,但首先是因为我的代码版本错误,后来因为我没有选择price plan。机器人问题给出了相同的错误,但它们需要不同的更改。
猜你喜欢
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
  • 2018-01-25
  • 2020-08-09
  • 1970-01-01
  • 2019-01-18
相关资源
最近更新 更多