我想获取从纽约发布的特定主题的所有推文。我有 twitter 凭证,我使用 Twitter API 的 Python 代码如下:
import tweepy
import numpy as np
import pandas as pd
auth = tweepy.OAuthHandler("......", ".......")
auth.set_access_token("........", "........")
api = tweepy.API(auth)
df = pd.DataFrame(columns = ['Tweets', 'Date of Tweet', 'Retweet Count', 'User Location', 'User Registration Date'])
def stream():
i = 0
for tweet in tweepy.Cursor(api.search, q='climatechange', count=100000, lang='en', tweet_mode='extended', since='2020-02-2', until='2020-02-25',geocode='43.17305,-77.62479,100km').items():
print(i, end='\r')
df.loc[i, 'Tweets'] = tweet.full_text
df.loc[i, 'Date of Tweet'] = tweet.created_at
df.loc[i, 'Retweet Count'] = tweet.retweet_count
df.loc[i, 'User Location'] = tweet.user.location
df.loc[i, 'User Registration Date'] = tweet.user.created_at
df.to_csv('GeoTweets1.csv')
i+=1
if i == 10000:
break
else:
pass
stream()
df.info()
问题:1-我想获取元数据。我的意思是尽可能多的推文大数据。我们知道每 15 分钟我们可以请求 180 个关键字,每个请求可以得到 100 条推文,这意味着 18000 条推文。如何迭代它为特定关键字提供 18K 条推文的代码并每 15 分钟自动重复一次?例如,要获取纽约市有关气候变化的推文,我想连续运行此代码 10 小时,即 15 分钟中的 40 小时,这意味着我可以获得 72 万条推文。
2-我也有根据位置获取推文的问题。当我运行上面的代码并为诸如气候变化之类的关键字请求推文时,它给了我 100 条推文,但对于纽约的地理查询给我的推文更少。例如,对于 geocode='43.17305,-77.62479,32km,它给了我 22 条推文,对于 geocode='43.17305,-77.62479,100km,它给了我 12 条推文。为什么地理搜索没有给我 100 条推文 谢谢你的帮助