2016-03-08 186 views
1

我知道這已經被問到過了..但是我的代碼似乎不適用於創建訪問權限的用戶以外的其他用戶 .. ie I am只能訪問一個用戶的個人資料。(使用我所創建的accessToken)使用Instagram API在Instagram中獲取用戶的關注者數量

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" 

錯誤,當我試圖訪問其他的配置文件,我得到的是

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** 
+0

可以嘗試[/users/self](https://www.instagram .com/developer/endpoints/users /#get_users_self)? – walkingRed

+0

@walkingRed:我們不能指定userid當使用自我..如何幫助 – MysticForce

+0

我可能會發現解決方案:[Instagram-python](https://github.com/Instagram/python-instagram) – walkingRed

回答

0

嘗試THI S:

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") 
+0

在我的代碼中,我能夠訪問自我..但我無法訪問其他用戶的數據 – MysticForce

+0

好@MysticForce,但如果你更改其他用戶的網址它的工作,或者它仍然是錯誤的請求錯誤? – walkingRed

+0

它仍然是一個錯誤的請求錯誤 – MysticForce

1

如果你不想打新InstagramAPI你可以使用這樣的解決方法:

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) 
+0

我正在爲此程序獲得一個恆定輸出。它是'2114591' – MysticForce

+0

並且你能告訴你到底是你想要做什麼嗎? – MysticForce

相關問題