我使用tweepy
流应用程序接口来获取包含特定标签的推文。我面临的问题是,我无法从流API中提取推文的全文。只有140个字符可用,在此之后它会被截断。
代码如下:
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
def analyze_status(text):
if 'RT' in text[0:3]:
return True
else:
return False
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
if not analyze_status(status.text):
with open('fetched_tweets.txt', 'a') as tf:
tf.write(status.text.encode('utf-8') + '\n\n')
print(status.text)
def on_error(self, status):
print("Error Code : " + status)
def test_rate_limit(api, wait=True, buffer=.1):
"""
Tests whether the rate limit of the last request has been reached.
:param api: The `tweepy` api instance.
:param wait: A flag indicating whether to wait for the rate limit reset
if the rate limit has been reached.
:param buffer: A buffer time in seconds that is added on to the waiting
time as an extra safety margin.
:return: True if it is ok to proceed with the next request. False otherwise.
"""
# Get the number of remaining requests
remaining = int(api.last_response.getheader('x-rate-limit-remaining'))
# Check if we have reached the limit
if remaining == 0:
limit = int(api.last_response.getheader('x-rate-limit-limit'))
reset = int(api.last_response.getheader('x-rate-limit-reset'))
# Parse the UTC time
reset = datetime.fromtimestamp(reset)
# Let the user know we have reached the rate limit
print "0 of {} requests remaining until {}.".format(limit, reset)
if wait:
# Determine the delay and sleep
delay = (reset - datetime.now()).total_seconds() + buffer
print "Sleeping for {}s...".format(delay)
sleep(delay)
# We have waited for the rate limit reset. OK to proceed.
return True
else:
# We have reached the rate limit. The user needs to handle the rate limit manually.
return False
# We have not reached the rate limit
return True
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener,
tweet_mode='extended')
myStream.filter(track=['#bitcoin'], async=True)
有人有解决方案吗?
发布于 2018-01-19 01:18:14
tweet_mode=extended
在此代码中将不起作用,因为流API不支持该参数。如果Tweet包含更长的文本,那么它将在JSON响应中包含一个名为extended_tweet
的额外对象,该对象又将包含一个名为full_text
的字段。
在这种情况下,您需要像print(status.extended_tweet.full_text)
这样的工具来提取较长的文本。
发布于 2019-11-15 17:55:01
在Twitter流中有可用的布尔值。当消息包含的字符超过140个时,'status.truncated‘为True。只有这样,'extended_tweet‘对象才可用:
if not status.truncated:
text = status.text
else:
text = status.extended_tweet['full_text']
只有当你在发送tweet时,这才能起作用。当您使用API方法收集旧的tweet时,您可以使用类似以下内容:
tweets = api.user_timeline(screen_name='whoever', count=5, tweet_mode='extended')
for tweet in tweets:
print(tweet.full_text)
此full_text字段包含所有tweet的文本,无论是否截断。
发布于 2018-09-07 14:24:14
您必须启用扩展推文模式,如下所示:
s = tweepy.Stream(auth, l, tweet_mode='extended')
然后,您可以打印扩展的tweet,但请记住,由于Twitter API,您必须确保扩展的tweet存在,否则将抛出错误
l = listener()
class listener(StreamListener):
def on_status(self, status):
try:
print(status.extended_tweet['full_text'])
except Exception as e:
raise
else:
print(status.text)
return True
def on_error(self, status_code):
if status_code == 420:
return False
对我很管用。
https://stackoverflow.com/questions/48319243
复制相似问题