尝试使用 Tweepy 和 Python 获取推文的 full_text 时出错
Error when trying to get full_text for a Tweet using Tweepy and Python
我正在尝试使用 Tweepy 和流媒体实时跟踪推文。我正在使用以下效果很好:
import tweepy
import configparser
import sys
#read configs
config = configparser.ConfigParser()
config.read('config.ini')
api_key = config['twitter']['api_key']
api_key_secret = config['twitter']['api_key_secret']
access_token = config['twitter']['access_token']
access_token_secret = config['twitter']['access_token_secret']
class StreamCollector(tweepy.Stream):
def on_status(self, status):
if not hasattr(status, 'retweeted_status') and status.in_reply_to_screen_name == None and status.i\
s_quote_status == False:
if status.author.followers_count > 100000:
print('Twitter Handle: @'+status.author.screen_name)
print('Followers:',status.author.followers_count)
print('Tweet:',status.text)
print('\n')
#print(status.user.screen_name.encode('UTF-8'))
stream = StreamCollector(api_key,api_key_secret,access_token, access_token_secret)
stream.filter(track=["table"])
但是,我想生成 未截断的 推文。我尝试用 status.text
代替 status.full_text
但我得到了错误:
AttributeError: 'Status' object has no attribute 'full_text'
我的 Tweepy 版本是 4.5.0,Python 是 3.9.9。
tweepy.API
有一个 compatibility mode
和 extended mode
。 extended mode
应该允许您获取推文的全文。
这是 extended mode
调用的代码。
import sys
import tweepy
import configparser
#read configs
config = configparser.ConfigParser()
config.read('config.ini')
api_key = config['twitter']['api_key']
api_key_secret = config['twitter']['api_key_secret']
access_token = config['twitter']['access_token']
access_token_secret = config['twitter']['access_token_secret']
class StreamCollector(tweepy.Stream):
def on_status(self, status):
if not hasattr(status, 'retweeted_status'):
if status.in_reply_to_screen_name is None and status.is_quote_status is False:
if status.author.followers_count > 100000:
print(f'Twitter Handle: @{status.author.screen_name}')
print(f'Followers: {status.author.followers_count}')
if 'extended_tweet' in status._json:
full_text = status._json['extended_tweet']['full_text']
print(f'Tweet: {full_text}')
elif 'extended_tweet' not in status._json:
print(f'Tweet: {status.text}')
print('\n')
stream = StreamCollector(api_key,api_key_secret,access_token, access_token_secret)
stream.filter(track=["table"])
流媒体包含在 Tweepy's documentation on extended Tweets:
By default, the Status objects from streams may contain an extended_tweet
attribute representing the equivalent field in the raw data/payload for the Tweet. This attribute/field will only exist for extended Tweets, containing a dictionary of sub-fields. The full_text
sub-field/key of this dictionary will contain the full, untruncated text of the Tweet
我正在尝试使用 Tweepy 和流媒体实时跟踪推文。我正在使用以下效果很好:
import tweepy
import configparser
import sys
#read configs
config = configparser.ConfigParser()
config.read('config.ini')
api_key = config['twitter']['api_key']
api_key_secret = config['twitter']['api_key_secret']
access_token = config['twitter']['access_token']
access_token_secret = config['twitter']['access_token_secret']
class StreamCollector(tweepy.Stream):
def on_status(self, status):
if not hasattr(status, 'retweeted_status') and status.in_reply_to_screen_name == None and status.i\
s_quote_status == False:
if status.author.followers_count > 100000:
print('Twitter Handle: @'+status.author.screen_name)
print('Followers:',status.author.followers_count)
print('Tweet:',status.text)
print('\n')
#print(status.user.screen_name.encode('UTF-8'))
stream = StreamCollector(api_key,api_key_secret,access_token, access_token_secret)
stream.filter(track=["table"])
但是,我想生成 未截断的 推文。我尝试用 status.text
代替 status.full_text
但我得到了错误:
AttributeError: 'Status' object has no attribute 'full_text'
我的 Tweepy 版本是 4.5.0,Python 是 3.9.9。
tweepy.API
有一个 compatibility mode
和 extended mode
。 extended mode
应该允许您获取推文的全文。
这是 extended mode
调用的代码。
import sys
import tweepy
import configparser
#read configs
config = configparser.ConfigParser()
config.read('config.ini')
api_key = config['twitter']['api_key']
api_key_secret = config['twitter']['api_key_secret']
access_token = config['twitter']['access_token']
access_token_secret = config['twitter']['access_token_secret']
class StreamCollector(tweepy.Stream):
def on_status(self, status):
if not hasattr(status, 'retweeted_status'):
if status.in_reply_to_screen_name is None and status.is_quote_status is False:
if status.author.followers_count > 100000:
print(f'Twitter Handle: @{status.author.screen_name}')
print(f'Followers: {status.author.followers_count}')
if 'extended_tweet' in status._json:
full_text = status._json['extended_tweet']['full_text']
print(f'Tweet: {full_text}')
elif 'extended_tweet' not in status._json:
print(f'Tweet: {status.text}')
print('\n')
stream = StreamCollector(api_key,api_key_secret,access_token, access_token_secret)
stream.filter(track=["table"])
流媒体包含在 Tweepy's documentation on extended Tweets:
By default, the Status objects from streams may contain an
extended_tweet
attribute representing the equivalent field in the raw data/payload for the Tweet. This attribute/field will only exist for extended Tweets, containing a dictionary of sub-fields. Thefull_text
sub-field/key of this dictionary will contain the full, untruncated text of the Tweet