【问题标题】:How to get media_url from tweets using the Tweepy API如何使用 Tweepy API 从推文中获取 media_url
【发布时间】:2016-08-11 04:29:03
【问题描述】:

我正在使用此代码:

import tweepy
from tweepy.api import API
import urllib
import os

i = 1
consumer_key="xx"
consumer_secret="xx"
access_token="xx"
access_token_secret="xx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.secure = True
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

class MyStreamListener(tweepy.StreamListener):
    def __init__(self, api=None):
        self.api = api or API()
        self.n = 0
        self.m = 10

    def on_status(self, status):
        if 'media' in status.entities:
            for image in  status.entities['media']:
                global i
                #picName = status.user.screen_name
                picName = "pic%s.jpg" % i
                i += 1
                link = image['media_url']
                filename = os.path.join("C:/Users/Charbo/Documents/Python/",picName)
                urllib.urlretrieve(link,filename)
                #use to test
                print(status.user.screen_name)

        else: 
            print("no media_url")

        self.n = self.n+1

        if self.n < self.m: 
            return True
        else:
            print ('tweets = '+str(self.n))
            return False

    def on_error(self, status):
        print (status)

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth, MyStreamListener(),timeout=30)
myStream.filter(track=['#feelthebern'])

我正在尝试访问我字典中“照片”下的 media_url。但我收到以下错误:'dict' 对象没有属性'media'。我将不胜感激帮助导航 JSON。

提前致谢!

【问题讨论】:

  • 您是否尝试查看所有 dir(status) 提供的内容?我无法重现该错误,因为您没有给出最小和完整的步骤来重现它,但是使用了 twitters api,我认为 json 在类似 status._json 的东西中可用?
  • @MohitC 我添加了所有代码。

标签: python twitter tweepy


【解决方案1】:

你应该尝试两件事:

  • 将实体添加到您的请求中

>

tweepy.Cursor(api.search, q="#hashtag", count=5, include_entities=True)
  • 检查媒体是否不为空:

>

if 'media' in tweet.entities:
    for image in  tweet.entities['media']:
        (do smthing with image['media_url'])

希望这会有所帮助

【讨论】:

  • 感谢以上。只是为了澄清我们可以在使用 Stream 类时使用 tweepy.Cursor (所以在流式传输时)?我将您的第一个要点添加到我的代码中。在我原来的问题中查看编辑的代码
  • 我不确定,但我认为是这样,因为光标只处理结果的分页
【解决方案2】:

这个回复可能有点晚,但我相信其他人有一天会发现它有用。实际上,我不想转发任何带有视频的推文。所以我构建了这个函数......并且它运行良好。

def on_status(self, status):
    #Ignores the tweet so long as I am the Author, or it's a reply to a tweet
    if status.in_reply_to_status_id is not None or \
        status.user.id == self.me.id:
        return

    #I only retweet tweets that I haven't yet retweeted. I also don't want to retweet any tweets that are quotes.
    if not status.retweeted and not status.is_quote_status:
        #Checking whether the tweet has no "media" in it.
        if 'media' not in status.entities:
            try:
                print(status.text)
                status.retweet()
                time.sleep(40) #Sleep for 40 seconds to avoid limits
            except Exception as e:
                print("Error on_data %s" % str(e))
                print("Error from retweeting")
        #If tweet has media, I only retweet a tweet with a photo
        elif 'media' in status.entities:
            media_details = status.entities['media']
            media_details_kind = media_details[0]
            #print(vide['type'])
            
            if media_details_kind['type'] == 'photo':
                try:
                    print("It is a photo")
                    status.retweet()
                    time.sleep(40)
                except Exception as e:
                    print("Error on_data %s" % str(e))
                    print("Error from retweeting")
        else: #Anything else is a video or GIF. I do nothing. 
            print("Sorry, this might be a video. Cound't retweet because it is neither a photo nor a text")
            print(status.text)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-06
    • 2020-09-02
    • 2019-04-09
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2020-09-07
    相关资源
    最近更新 更多