TypeError:“发布”对象不可下标

时间:2019-08-30 13:22:06

标签: python python-3.x

当前试图弄清楚为什么我不能从返回的字典中提取特定的键/值。查找此问题后,我发现this earlier问题基本上说明该对象必须为json格式才能被访问。

def Dumpster_Fire_Parser():

    import moesearch
    import pandas as pd

    trash = moesearch.search(archiver_url="https://archive.4plebs.org",
                             board="pol",
                             filter="image",
                             deleted="not-deleted",
                             )
    # trash = dict(trash)

    time_dumpster_dict = {}
    country_dumpster_dict = {}

    for i, j in enumerate(trash):

        trash_dict = j
        time_stamp = trash_dict['timestamp']
        comment = trash_dict['comment']
        country = trash_dict['poster_country_name']
        time_dumpster_dict[time_stamp] = comment
        country_dumpster_dict[time_stamp] = country

    export_frame = pd.DataFrame([time_dumpster_dict, country_dumpster_dict]).T
    export_frame.columns = ['d{}'.format(i) for i, col in enumerate(export_frame, 1)]

    print(export_frame)

    return export_frame

运行此代码将返回错误:

Traceback (most recent call last):
  File "<input>", line 17, in <module>
TypeError: 'Post' object is not subscriptable

我浏览了moesearch.search()的源代码,它已经在那里转换为json对象。

req = requests.get(url, stream=False, verify=True, params=kwargs)
  res = req.json() # How its written in source

通过trash = dict(trash)完成请求后,我尝试将其显式转换为dict,但是会返回另一个错误:

TypeError: cannot convert dictionary update sequence element #0 to a sequence 
# Is thrown when trash = dict(trash) isn't commented out

有人遇到过吗?该代码是可执行的,请记住Search API限制为每分钟5个请求。其他端点没有限制。

1 个答案:

答案 0 :(得分:1)

您正确的将moesearch的源代码转换为Line 40中的JSON,但是在接下来的几行中,您可以看到函数search()返回了{{1 }}对象(line 44Post语句):

return

因此,在您的代码中,def search(archiver_url, board, **kwargs): ... req = requests.get(url, stream=False, verify=True, params=kwargs) res = req.json() if ArchiveException.is_error(res): raise ArchiveException(res) res = res['0'] return [Post(post_obj) for post_obj in res["posts"]] 是一个列表,trash是一个j类型的对象;您可以像这样检查:

Post