Twitter API搜索返回截断的推文

时间:2018-04-13 02:59:10

标签: python twitter python-twitter

我正在尝试构建一个python程序来获取基于某个关键字的推文。但是,虽然我可以成功检索推文,但它们会被截断。

如何获取推文的全文?

代码:(代码下面的示例输出) (python-twitter模块)

import twitter

api = twitter.Api(consumer_key=CONSUMER_KEY,
                  consumer_secret=CONSUMER_SECRET,
                  access_token_key=ACCESS_TOKEN,
                  access_token_secret=ACCESS_SECRET)
results = api.GetSearch(term="car", since="2018-04-11", until="2018-04-12", count=5)

for twt in results:
    tempTweet = (str(twt))
tweet = json.loads(tempTweet)
for key in tweet:
    print(str(key) + ": " + str(tweet[key]))

print("#############################################")

SAMPLE OUTPUT:

created_at: Wed Apr 11 20:55:25 +0000 2018
favorite_count: 1573
hashtags: []
id: 984173096566341632
id_str: 984173096566341632
lang: en
retweet_count: 1480
source: <a href="https://about.twitter.com/products/tweetdeck" rel="nofollow">TweetDeck</a>
**text**: Caution: Disturbing video. Car speeds through red light, striking pedestrian during vigil Wednesday for cyclist kil… **SHORTENEDURLHERE**
truncated: True
urls: [{'expanded_url':'https://twitter.com/i/web/status/984173096566341632', 'url':**SHORTENEDURLHERE**}]
user: {'created_at': 'Wed Nov 14 17:43:42 +0000 2007', 'description': 'KTLA has been keeping Southern California informed since 1947. \n\nHave great video, photos or story tips? Share with us using #ktla.', 'favourites_count': 1078, 'followers_count': 717397, 'friends_count': 769, 'geo_enabled': True, 'id': 10252962, 'id_str': '10252962', 'lang': 'en', 'listed_count': 3885, 'location': 'Los Angeles, CA', 'name': 'KTLA', 'profile_background_color': '040718', 'profile_background_image_url': 'http://pbs.twimg.com/profile_background_images/507323957578436608/olqcU4MS.jpeg', 'profile_background_image_url_https': 'https://pbs.twimg.com/profile_background_images/507323957578436608/olqcU4MS.jpeg', 'profile_banner_url': 'https://pbs.twimg.com/profile_banners/10252962/1369959990', 'profile_image_url': 'http://pbs.twimg.com/profile_images/809849913240481792/YQ0aT9hv_normal.jpg', 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/809849913240481792/YQ0aT9hv_normal.jpg', 'profile_link_color': '24009C', 'profile_sidebar_border_color': 'FFFFFF', 'profile_sidebar_fill_color': '95E8EC', 'profile_text_color': '3C3940', 'profile_use_background_image': True, 'screen_name': 'KTLA', 'statuses_count': 144937, 'time_zone': 'Pacific Time (US & Canada)', 'url': '**SHORTENEDURLHERE**', 'utc_offset': -25200, 'verified': True}
user_mentions: []
#############################################

1 个答案:

答案 0 :(得分:2)

您需要在初始化Api时传递tweet_mode='extended'

import twitter
api = twitter.Api(consumer_key=CONSUMER_KEY,
                  consumer_secret=CONSUMER_SECRET,
                  access_token_key=ACCESS_TOKEN,
                  access_token_secret=ACCESS_SECRET,
                  tweet_mode='extended')
results = api.GetSearch(term="car", since="2018-04-11", until="2018-04-12", count=5)

for twt in results:
    tempTweet = (str(twt))
tweet = json.loads(tempTweet)

print(tweet)

这将打印,

{u'lang': u'en', u'full_text': u'Have you ever been in so much trouble that you\u2019ve narrowed your options down to a. Winning the lottery b. Wrapping your car around a telephone pole and c. Giving the creepy neighborhood millionaire the date he keeps pestering for at  Cheescake Factory? \nPffffffttt. Me either. <twitter link>', u'media': [{u'expanded_url': u'https://twitter.com/_jkate/status/984219542061703168/photo/1', u'sizes': {u'large': {u'h': 1280, u'w': 719, u'resize': u'fit'}, u'small': {u'h': 680, u'w': 382, u'resize': u'fit'}, u'medium': {u'h': 1200, u'w': 674, u'resize': u'fit'}, u'thumb': {u'h': 150, u'w': 150, u'resize': u'crop'}}, u'url': u'<twitter link>', u'media_url_https': u'https://pbs.twimg.com/media/Daimc02VQAAulWe.jpg', u'display_url': u'pic.twitter.com/ZfCeeZN4g0', u'type': u'photo', u'id': 984219532733530112, u'media_url': u'http://pbs.twimg.com/media/Daimc02VQAAulWe.jpg'}], u'created_at': u'Wed Apr 11 23:59:59 +0000 2018', u'hashtags': [], u'user_mentions': [], u'source': u'<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>', u'id_str': u'984219542061703168', u'urls': [], u'retweet_count': 2, u'id': 984219542061703168, u'favorite_count': 83, u'user': {u'profile_use_background_image': True, u'id': 492519212, u'profile_image_url_https': u'https://pbs.twimg.com/profile_images/985737787306422273/aJykLNLj_normal.jpg', u'profile_sidebar_fill_color': u'F3F3F3', u'profile_text_color': u'333333', u'followers_count': 13069, u'location': u'United States', u'profile_background_color': u'EBEBEB', u'id_str': u'492519212', u'utc_offset': -21600, u'statuses_count': 1543, u'description': u'Illegitimate love child of digital marketing and \u2615\ufe0f. Instagram: <twitter link>', u'friends_count': 11470, u'profile_link_color': u'990000', u'profile_image_url': u'http://pbs.twimg.com/profile_images/985737787306422273/aJykLNLj_normal.jpg', u'profile_background_image_url_https': u'https://abs.twimg.com/images/themes/theme7/bg.gif', u'profile_banner_url': u'https://pbs.twimg.com/profile_banners/492519212/1509938057', u'profile_background_image_url': u'http://abs.twimg.com/images/themes/theme7/bg.gif', u'screen_name': u'_jkate', u'lang': u'en', u'favourites_count': 8060, u'name': u'\U0001f319J Kate \U0001f4ab', u'created_at': u'Tue Feb 14 20:23:54 +0000 2012', u'time_zone': u'Mountain Time (US & Canada)', u'profile_sidebar_border_color': u'DFDFDF', u'listed_count': 55}}