简体   繁体   中英

Getting number of followers of a user in instagram using Instagram API

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 .. ie 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 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**

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)

Try this:

import json
import requests

access_token = "mytoken"
url = "https://api.instagram.com/v1/users/self/?access_token={0}".format(access_token)
response = requests.get(url)
j = json.loads(response.text)
if 'data' in j:
    print("handle correct response")
else:
    print("handle incorrect response")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM