Twitter API: How to get users ID, who favorite specific tweet?

前端 未结 3 1447
轻奢々
轻奢々 2021-02-18 19:17

I\'m trying to get info about users, who added specific tweet to favorites, but I can\'t find it in documentation.

It is unfair that twitter can do that, but doesn\'t g

3条回答
  •  走了就别回头了
    2021-02-18 19:57

    Apparently, the only way to do this is to scrape Twitter's website:

    import urllib2
    from lxml.html import parse
    
    #returns list(retweet users),list(favorite users) for a given screen_name and status_id
    def get_twitter_user_rts_and_favs(screen_name, status_id):
        url = urllib2.urlopen('https://twitter.com/' + screen_name + '/status/' + status_id)
        root = parse(url).getroot()
    
        num_rts = 0
        num_favs = 0
        rt_users = []
        fav_users = []
    
        for ul in root.find_class('stats'):
            for li in ul.cssselect('li'):
    
                cls_name = li.attrib['class']
    
                if cls_name.find('retweet') >= 0:
                    num_rts = int(li.cssselect('a')[0].attrib['data-tweet-stat-count'])
    
                elif cls_name.find('favorit') >= 0:
                    num_favs = int(li.cssselect('a')[0].attrib['data-tweet-stat-count'])
    
                elif cls_name.find('avatar') >= 0 or cls_name.find('face-pile') >= 0:#else face-plant
    
                    for users in li.cssselect('a'):
                        #apparently, favs are listed before retweets, but the retweet summary's listed before the fav summary
                        #if in doubt you can take the difference of returned uids here with retweet uids from the official api
                        if num_favs > 0:#num_rt > 0:
                            #num_rts -= 1
                            num_favs -= 1
                            #rt_users.append(users.attrib['data-user-id'])
                            fav_users.append(users.attrib['data-user-id'])
                        else:                        
                            #fav_users.append(users.attrib['data-user-id'])
                            rt_users.append(users.attrib['data-user-id'])
    
            return rt_users, fav_users
    
    
    #example
    if __name__ == '__main__':
        print get_twitter_user_rts_and_favs('alien_merchant', '674104400013578240')
    

提交回复
热议问题