python - 属性错误 : 'dict' object has no attribute 'read' while using Tweepy

标签 python

我是 python 的新手。我无法运行以下代码,因为它会引发属性错误。有人可以帮忙吗?

import tweepy
import urllib

import json


api_key = "VdG3NjsNKg49NbNb7GMHiX"
api_secret = "yBGKwe2K3QYk5lDny1eIKiyEQawVLQKX1HbRCTRfA9hK9"
access_token_key = "110456973-H8CAAET5CBoEa6FS4CKmk98XOADnJOsxK45"
access_token_secret = "wPUlfaxs1TFrTlXs2VqJIE5ffAfclhJCWmMlLPncb"


auth = tweepy.auth.OAuthHandler(api_key,api_secret)
auth.set_access_token(access_token_key,access_token_secret)
api=tweepy.API(auth,parser=tweepy.parsers.JSONParser())

results=api.search(q="microsoft",count=100)

print type(results)
print json.load(results)

最佳答案

results=api.search(q="microsoft",count=100)

print type(results)
print json.load(results)

results 这里已经是一个dict

不需要反序列化为JSON。

参见:tweepy.api.API.search()内容如下:

API.search(q[, lang][, locale][, rpp][, page][, since_id][, geocode][, show_user])

Returns tweets that match a specified query.

Parameters: 
    q – the search query string
    lang – Restricts tweets to the given language, given by an ISO 639-1 code.
    locale – Specify the language of the query you are sending. This is intended for language-specific clients and the default should work in the majority of cases.
    rpp – The number of tweets to return per page, up to a max of 100.
    page – The page number (starting at 1) to return, up to a max of roughly 1500 results (based on rpp * page.
    geocode – Returns tweets by users located within a given radius of the given latitude/longitude. The location is preferentially taking from the Geotagging API, but will fall back to their Twitter profile. The parameter value is specified by “latitide,longitude,radius”, where radius units must be specified as either “mi” (miles) or “km” (kilometers). Note that you cannot use the near operator via the API to geocode arbitrary locations; however you can use this geocode parameter to search near geocodes directly.
    show_user – When true, prepends “<user>:” to the beginning of the tweet. This is useful for readers that do not display Atom’s author field. The     default is false.

Return type:    
    list of SearchResult objects

注意:“返回类型:”

关于python - 属性错误 : 'dict' object has no attribute 'read' while using Tweepy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30229310/

相关文章:

python - 在 Python3.8 的 Multiprocess SharedMemory 中共享一个 bytes 对象

python - 使用异常链的原因是什么

python - 根据数据框中的列对数据进行切片

python - 我如何训练一个系统来识别 Python 中的模式?

python - Python 中 'empty' else 语句的必要性

python - x长度为2个字符的排列

python - 从特定 channel 抓取 YouTube 视频并进行搜索?

python - 从字符串中获取有效的 python 列表(javascript 数组)

python - Slack API - 用于检索斜杠命令的队列

python - 选择索引标签位于两个列表之一中的 Pandas Dataframe 行