使用 tweepy 检索自月初以来的推文
Retrieving tweets from since the start of the month with tweepy
我正在尝试使用 Tweepy 从当月开始检索推文。这是我定义日期的方式:
now = datetime.now()
monthstart = now.replace (day = 1)
以及我如何尝试检索推文:
#test print
a = [] #raw
b = [] #preprocessed
c = [] #place
d = [] #coord
for tweet in tweepy.Cursor(api.search,q="#lalinbdg", tweet_mode = 'extended',
since=monthstart).items():
a.append(tweet.full_text)
b.append(tweet.full_text)
c.append(tweet.place)
d.append(tweet.coordinates)
b = list(dict.fromkeys(b)) #remove duplicates
for text in b:
text = text.casefold() #lowercase
print(text)
当我尝试使用 monthstart 变量指定日期时,它不会 return 任何输出。
'since' 似乎只接受日期字符串,因为它在我使用像“2020-07-01”这样的字符串时有效,有人对此有解决方案吗?
since
参数需要是一个字符串,并且您传递的是一个日期时间对象。因此,您需要使用 strftime
. Note that datetime.now()
has a time component, so you would be better using date.today()
将其转换为字符串,否则您可能会错过本月 1 日的一些条目,这些条目早于现在的时间。总计:
today = date.today()
monthstart = today.replace(day=1).strftime('%Y-%m-%d')
我正在尝试使用 Tweepy 从当月开始检索推文。这是我定义日期的方式:
now = datetime.now()
monthstart = now.replace (day = 1)
以及我如何尝试检索推文:
#test print
a = [] #raw
b = [] #preprocessed
c = [] #place
d = [] #coord
for tweet in tweepy.Cursor(api.search,q="#lalinbdg", tweet_mode = 'extended',
since=monthstart).items():
a.append(tweet.full_text)
b.append(tweet.full_text)
c.append(tweet.place)
d.append(tweet.coordinates)
b = list(dict.fromkeys(b)) #remove duplicates
for text in b:
text = text.casefold() #lowercase
print(text)
当我尝试使用 monthstart 变量指定日期时,它不会 return 任何输出。 'since' 似乎只接受日期字符串,因为它在我使用像“2020-07-01”这样的字符串时有效,有人对此有解决方案吗?
since
参数需要是一个字符串,并且您传递的是一个日期时间对象。因此,您需要使用 strftime
. Note that datetime.now()
has a time component, so you would be better using date.today()
将其转换为字符串,否则您可能会错过本月 1 日的一些条目,这些条目早于现在的时间。总计:
today = date.today()
monthstart = today.replace(day=1).strftime('%Y-%m-%d')