【问题标题】:how to fix ''Tweet' object has no attribute 'created_at'' when using the twitter API?使用 twitter API 时如何修复“Tweet”对象没有属性“created_at”?
【发布时间】:2019-10-12 11:43:59
【问题描述】:

我正在做一些关于公园评估的研究,我使用 twitter api 抓取了一些关于伦敦公园的推文。我可以获取文本、用户 ID 和关注者等,但无法获取 twitter_time。错误是 ''Tweet' 对象没有属性 'created_at''。(created_at 实际上存在于 twitter 返回的列表中)

我在 jupyter notebook 中使用了 python

load_credentials(filename="./search_tweets_creds_example.yaml",
             yaml_key="search_tweets_ent_example",
             env_overwrite=False)
{'username': '<MY_USERNAME>',
'password': '<MY_PASSWORD>',
'endpoint': '<MY_ENDPOINT>'}

premium_search_args = load_credentials("~/.twitter_keys.yaml",
                                   yaml_key="search_tweets_premium",
                                   env_overwrite=False)

rule = gen_rule_payload("#Regentpark", results_per_call=100) # testing with a sandbox account
 print(rule)
 from searchtweets import collect_results
 import numpy as np
 import pandas as pd
 import csv
 df = pd.DataFrame(columns = ['Tweets', 'User', 
                         'fav_count', 'rt_count'])

 def stream(file_name):
i = 0
for tweet in collect_results(rule,
                     max_results=1000,
                     result_stream_args=premium_search_args):
    print(i, end='\r')
    df.loc[i, 'Tweets'] = tweet.text
    df.loc[i, 'User'] = tweet.id
    df.loc[i, 'fav_count'] = tweet.favorite_count
    df.loc[i, 'rt_count'] = tweet.retweet_count
    df.loc[i,'tweet_data']=tweet.created_at
    df.to_excel('{}.xlsx'.format(file_name))
    i+=1
    if i == 1000:
        break
    else:
        pass
    stream(file_name='Regentpark')

AttributeError: 'Tweet' 对象没有属性 'created_at'

【问题讨论】:

    标签: python api twitter tweets


    【解决方案1】:

    在他们使用的包的 API 示例笔记本中:

    tweet.created_at_datetime
    

    这对我来说也很好用。这是示例的链接: https://github.com/twitterdev/search-tweets-python/blob/master/examples/api_example.ipynb

    【讨论】:

      猜你喜欢
      • 2022-06-12
      • 2022-08-06
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      • 2019-12-08
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多