gpt4 book ai didi

twitter - tweepy 流媒体 API : full text

转载 作者:行者123 更新时间:2023-12-02 21:09:39 30 4
gpt4 key购买 nike

我正在使用 tweepy 流 API 来获取包含特定主题标签的推文。我面临的问题是我无法从 Streaming 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)

有人有解决办法吗?

最佳答案

tweet_mode=extended 在此代码中无效,因为 Streaming API 不支持该参数。如果推文包含较长的文本,它将在 JSON 响应中包含一个名为 extended_tweet 的附加对象,该对象又包含一个名为 full_text 的字段。

在这种情况下,您将需要类似 print(status.extended_tweet.full_text) 的内容来提取较长的文本。

关于twitter - tweepy 流媒体 API : full text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48319243/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com