首页 > 解决方案 > 如何在 python 上从用户那里获取特定日期的推文?

问题描述

我正在尝试从路透社 (@reuters) 推特账户下载 2019 年 11 月的推文。

我在 python 上使用 tweepy,这是我的代码:

pip install tweepy
import tweepy as tw

#Keys
consumer_key = "..."
consumer_secret = "..."
access_token = "..."
access_token_secret = "..."

# Login
auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)

#Get user's tweets
tweets = tw.Cursor(api.user_timeline,
                   id="reuters",
                   lang="en",
                   since="2019-11-01",
                   until="2019-11-30").items()

all_tweets = [tweet.text for tweet in tweets]

all_tweets[:100]

“直到”参数似乎不起作用,因为我的代码提取的推文包括最新的推文。

标签: pythontwittertweepy

解决方案


import tweepy
import csv
import pandas as pd
####input your credentials here
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)

# Open/Create a file to append data
csvFile = open('tweets.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)

# tracklist = ["Womens Day", "internationalwomensday", "internationalwomensday2021", "internationalwomensday21","women's day", "international women's day", "IWD", "womensday", "WomensDay", "HappyInternationalWomensDay","Happy Women's Day", "HappyWomensDay", "happywomensday", "happyinternationalwomensday", "Women", "women"]
# tracklist = ''.join(str(e) for e in tracklist)
# import pdb; pdb.set_trace()
count = 0

# for tweet in tweepy.Cursor(api.search,q="Womens Day OR internationalwomensday OR internationalwomensday2021 OR internationalwomensday21 OR women's day OR international women's day OR IWD or womensday OR WomensDay OR HappyInternationalWomensDay OR Happy Women's Day OR HappyWomensDay OR happywomensday OR happyinternationalwomensday OR Women OR women",count=10000,
#                            lang="en",
#                            since="2021-03-06", 
#                            include_rts=False).items():
#     print (tweet.created_at, tweet.text)
#     csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8')])


for tweet in tweepy.Cursor(api.search,q="Womens Day OR internationalwomensday OR internationalwomensday2021 OR internationalwomensday21 OR women's day OR international women's day OR IWD OR HappyInternationalWomensDay OR Happy Women's Day OR HappyWomensDay OR happywomensday OR happyinternationalwomensday",
                           count=100000,
                           include_rts=False,
                           country_code=True,
                           coordinates=True,
                           lang="en",
                           since="2021-03-06",
                           until="2021-03-10"
                           ).items():
    print (tweet.created_at, tweet.text)
    csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8')])

推荐阅读