我正在做一些有关公园评估的研究,我使用twitter api获取了有关伦敦公园的一些推文。我可以获取文本,用户ID和关注者等,而无法获取twitter_time。错误是“ Tweet”对象没有属性“ created_at”。(created_at实际上存在于返回的Twitter列表中)
我在jupyter笔记本中使用了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”
答案 0 :(得分:1)
在用于包装的API示例笔记本中:
tweet.created_at_datetime
这对我也很好。这是示例的链接: https://github.com/twitterdev/search-tweets-python/blob/master/examples/api_example.ipynb