使用Instagram API获取Instagram中用户的关注者数量 [英] Getting number of followers of a user in instagram using Instagram API

查看:180
本文介绍了使用Instagram API获取Instagram中用户的关注者数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经被问过了..但是我的代码似乎不适用于除创建访问令牌的用户以外的其他用户 ..即我只能访问一个用户的个人资料.(使用我创建访问令牌的用户)

I know this has been asked before .. but my code doesn't seem to work for users other than the one using whom accesstoken is created .. i.e I am able to access the profile of only one user.(using whom I created accesstoken)

from urllib2 import urlopen
from json import load
import json
import json
import sys
import time
import requests

userid="userid_of_some_instagram_user"
access_token="mytoken"
url = "https://api.instagram.com/v1/users/%s/?access_token=%s"%(userid,access_token)
response=urlopen(url)
print(response)
jsonobj=load(response)

print "name:",jsonobj["data"]["username"]  
print "follows ",jsonobj["data"]["counts"]["follows"]," people"
print "followed by ",jsonobj["data"]["counts"]["followed_by"]," people"

我尝试访问其他个人资料时遇到的错误是

Error I am getting when I am trying to access other profiles is

HTTPError:HTTP错误400:错误请求

HTTPError: HTTP Error 400: BAD REQUEST

Edit : **Voila ! I have found the bug . It is the access key. I have tried with a different access key . It worked**

推荐答案

如果您不想使用新的InstagramAPI,则可以使用以下解决方法:

If you don't wanna fight the new InstagramAPI you can use a workaround like this:

import requests
import json
import time
import random

user = 'google'
url = 'https://www.instagram.com/'
url_user = '%s%s%s' % (url, user, '/')
url_login = 'https://www.instagram.com/accounts/login/ajax/'
s = requests.Session()
s.cookies.update ({'sessionid' : '', 'mid' : '', 'ig_pr' : '1',
                     'ig_vw' : '1920', 'csrftoken' : '',
                     's_network' : '', 'ds_user_id' : ''})
login_post = {'username' : 'your_login',
                 'password' : 'your_pw'}
s.headers.update ()
r = s.get(url)
s.headers.update({'X-CSRFToken' : r.cookies['csrftoken']})
time.sleep(5 * random.random())
login = s.post(url_login, data=login_post,
                  allow_redirects=True)
s.headers.update({'X-CSRFToken' : login.cookies['csrftoken']})
if login.status_code == 200:
    r = s.get('https://www.instagram.com/')
    finder = r.text.find('your_login')

r = s.get(url_user)
text = r.text

finder_text_start = ('<script type="text/javascript">'
                     'window._sharedData = ')
finder_text_start_len = len(finder_text_start)-1
finder_text_end = ';</script>'

all_data_start = text.find(finder_text_start)
all_data_end = text.find(finder_text_end, all_data_start + 1)
json_str = text[(all_data_start + finder_text_start_len + 1) \
               : all_data_end]
user_info = json.loads(json_str)
follower_count = user_info['entry_data']['ProfilePage'][0]['user']["followed_by"]['count']

print(follower_count)

这篇关于使用Instagram API获取Instagram中用户的关注者数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆