python - 在 tweepy 上使用用户对象时出现属性错误

标签 python twitter tweepy

我正在尝试编写一个程序,使用 Stream API 和 Tweepy 从 Twitter 传输推文。这是我的代码的相关部分:

def on_data(self, data):
    if data.user.id == "25073877" or data.in_reply_to_user_id == "25073877":
        self.filename = trump.csv

    elif data.user.id == "30354991" or data.in_reply_to_user_id == "30354991":
        self.filename = harris.csv

    if not 'RT @' in data.text:
        csvFile = open(self.filename, 'a')
        csvWriter = csv.write(csvFile)

        print(data.text)
        try:
            csvWriter.writerow([data.text, data.created_at, data.user.id, data.user.screen_name,  data.in_reply_to_status_id])

        except:
            pass

def on_error(self, status_code):
    if status_code == 420:
        return False

代码应该做的是流式传输推文并写入推文文本、创建日期、推文的用户 ID、他们的屏幕名称以及他们正在回复的状态的回复 ID(如果推文是回复。但是,我收到以下错误:

File "test.py", line 13, in on_data

 if data.user.id == "25073877" or data.in_reply_to_user_id == "25073877":

AttributeError: 'unicode' object has no attribute 'user'

有人可以帮我吗?谢谢!

编辑:读入“数据”的内容示例

{"created_at":"Fri Feb 15 20:50:46 +0000 2019","id":1096512164347760651,"id_str":"1096512164347760651","text":"@realDonaldTrump \nhttps:\/\/t.co\/NPwSuJ6V2M","source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":25073877,"in_reply_to_user_id_str":"25073877","in_reply_to_screen_name":"realDonaldTrump","user":{"id":1050189031743598592,"id_str":"1050189031743598592","name":"Lauren","screen_name":"switcherooskido","location":"United States","url":null,"description":"Concerned citizen of the USA who would like to see Integrity restored in the US Government. Anti-marxist!\nSigma, INTP\/J\nREJECT PC and Identity Politics #WWG1WGA","translator_type":"none","protected":false,"verified":false,"followers_count":1459,"friends_count":1906,"listed_count":0,"favourites_count":5311,"statuses_count":8946,"created_at":"Thu Oct 11 00:59:11 +0000 2018","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"000000","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"FF691F","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"000000","profile_text_color":"000000","profile_use_background_image":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1068591478329495558\/ng_tNAXx_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1068591478329495558\/ng_tNAXx_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1050189031743598592\/1541441602","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/NPwSuJ6V2M","expanded_url":"https:\/\/www.conservativereview.com\/news\/5-insane-provisions-amnesty-omnibus-bill\/","display_url":"conservativereview.com\/news\/5-insane-\u2026","indices":[18,41]}],"user_mentions":[{"screen_name":"realDonaldTrump","name":"Donald J. Trump","id":25073877,"id_str":"25073877","indices":[0,16]}],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"und","timestamp_ms":"1550263846848"}

所以我认为修改后的问题是如何告诉程序仅将此 JSON 输出的部分内容写入 CSV 文件?我一直在使用 Twitter 的流 API 为“数据”属性提供的引用。

最佳答案

正如您的评论中所述,推文数据采用“JSON 格式”。我相信你的意思是它是 JSON 格式的字符串(unicode),而不是解析的 JSON 对象。为了在代码中访问您想要的字段,您需要使用 json 解析数据字符串。

例如

import json

json_data_object = json.loads(data)

然后您可以像访问字典一样访问字段,例如

json_data_object['some_key']['some_other_key']

关于python - 在 tweepy 上使用用户对象时出现属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54715381/

相关文章:

python - Writing multiple JSON to CSV in Python - 字典到 CSV

python - 在用户提交表单时使用 Tweepy OAuth 对用户进行身份验证

python - 避免转义字符串中的双引号

python - 如何在 scikit 学习管道中实现 RandomUnderSampler?

json - Twitter json 提要?

python - 使用 Tweepy API Search 搜索列表中的任何元素

python - 如何在交互模式下跳过 `if __name__ == "__main_ _"`?

python - Popen 与 PIPE 通信无法捕获进程的所有输出

powershell - 如何使用 Powershell 连接到 Twitter API 并遵循参数

javascript - Angular4 - 如何检查 Twitter 媒体 key 是否存在或未定义