问题
I have a list of thousands of instagram user-ids. How do i get their Instagram usernames/handles?
Thanks
回答1:
You have to use this Instagram API:
https://api.instagram.com/v1/users/{user-id}/?access_token=ACCESS-TOKEN
The response will have username, full name, bio, follower counts and other info.
But you have to get you app approved by Instagram before you can access API.
You can try https://www.picodash.com, it allows you to search for id and get user results, but it will be a manual process to search one-by-one and get the info
回答2:
You can access it without needing API via internal /user/
endpoint instagram uses for AJAX requests:
https://i.instagram.com/api/v1/users/{user_id}/info/
where {user_id}
is numeric user id like 6817966272
.
Returned response example (see user['username']
key for username):
GET https://i.instagram.com/api/v1/users/6817966272/info/
{
"user": {
"pk": 6817966272,
"username": "myriaamaa",
"full_name": "\u2661",
"is_private": false,
"profile_pic_url": "https://instagram.fcnx2-1.fna.fbcdn.net/vp/66486d198fc02046d04d7bc11e51e54a/5D913015/t51.2885-19/s150x150/61226760_298998544311382_2345929352031502336_n.jpg?_nc_ht=instagram.fcnx2-1.fna.fbcdn.net",
"profile_pic_id": "2056076981860037983_6817966272",
"is_verified": false,
"has_anonymous_profile_picture": false,
"media_count": 216,
"follower_count": 4926,
"following_count": 83,
"following_tag_count": 0,
"biography": "YOU. ARE. HOLY \ud83c\udf19",
"external_url": "",
"total_igtv_videos": 0,
"total_ar_effects": 0,
"usertags_count": 6,
"is_favorite": false,
"is_interest_account": true,
"hd_profile_pic_versions": [
{
"width": 320,
"height": 320,
"url": "https://instagram.fcnx2-1.fna.fbcdn.net/vp/fafecdc76c82de85580c9c03d14b1aaa/5D9BD2E5/t51.2885-19/s320x320/61226760_298998544311382_2345929352031502336_n.jpg?_nc_ht=instagram.fcnx2-1.fna.fbcdn.net"
},
{
"width": 640,
"height": 640,
"url": "https://instagram.fcnx2-1.fna.fbcdn.net/vp/0ec5339e3958c9c41414e5378fa2443c/5D7DD28A/t51.2885-19/s640x640/61226760_298998544311382_2345929352031502336_n.jpg?_nc_ht=instagram.fcnx2-1.fna.fbcdn.net"
}
],
"hd_profile_pic_url_info": {
"url": "https://instagram.fcnx2-1.fna.fbcdn.net/vp/8b3859950f0bb8e1a4a8f65566992b78/5D9132EF/t51.2885-19/61226760_298998544311382_2345929352031502336_n.jpg?_nc_ht=instagram.fcnx2-1.fna.fbcdn.net",
"width": 774,
"height": 774
},
"mutual_followers_count": 0,
"has_highlight_reels": true,
"can_be_reported_as_fraud": false,
"is_business": false,
"account_type": 1,
"is_call_to_action_enabled": null,
"include_direct_blacklist_status": true,
"is_potential_business": true,
"is_bestie": false,
"has_unseen_besties_media": false,
"show_account_transparency_details": false,
"auto_expand_chaining": false,
"highlight_reshare_disabled": false
},
"status": "ok"
}
EDIT 2019-10:
This endpoint only serves user-agents that identify themselves as instagram app.
I got it working with iphone's instagram user agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 Instagram 12.0.0.16.90 (iPhone9,4; iOS 10_3_3; en_US; en-US; scale=2.61; gamut=wide; 1080x1920)
For more check out this online user agent database: https://developers.whatismybrowser.com/useragents/explore/software_name/instagram/1
# top 5:
Mozilla/5.0 (Linux; Android 8.1.0; motorola one Build/OPKS28.63-18-3; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/70.0.3538.80 Mobile Safari/537.36 Instagram 72.0.0.21.98 Android (27/8.1.0; 320dpi; 720x1362; motorola; motorola one; deen_sprout; qcom; pt_BR; 132081645)
Mozilla/5.0 (Linux; Android 7.0; Lenovo K33b36 Build/NRD90N; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/65.0.3325.109 Mobile Safari/537.36 Instagram 41.0.0.13.92 Android (24/7.0; 480dpi; 1080x1920; LENOVO/Lenovo; Lenovo K33b36; K33b36; qcom; pt_BR; 103516666)
Mozilla/5.0 (Linux; Android 5.0.1; LG-H342 Build/LRX21Y; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/65.0.3325.109 Mobile Safari/537.36 Instagram 40.0.0.14.95 Android (21/5.0.1; 240dpi; 480x786; LGE/lge; LG-H342; c50ds; c50ds; pt_BR; 102221277)
Mozilla/5.0 (Linux; Android 6.0.1; SM-G610M Build/MMB29K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/65.0.3325.109 Mobile Safari/537.36 Instagram 41.0.0.13.92 Android (23/6.0.1; 480dpi; 1080x1920; samsung; SM-G610M; on7xelte; samsungexynos7870; pt_BR; 103516666)
Mozilla/5.0 (Linux; Android 8.0.0; SM-A520F Build/R16NW; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.158 Mobile Safari/537.36 Instagram 46.0.0.15.96 Android (26/8.0.0; 480dpi; 1080x1920; samsung; SM-A520F; a5y17lte; samsungexynos7880; pt_BR; 109556226)
Edit 2020-05: Currently this endpoint returns less information but username is still present:
{
"user": {
"username": "meram.ba",
"pk": 6817966272,
"profile_pic_url": "https://instagram.fbkk5-5.fna.fbcdn.net/v/t51.2885-19/s150x150/93795245_228684278392964_3931284463400517632_n.jpg?_nc_ht=instagram.fbkk5-5.fna.fbcdn.net&_nc_ohc=O6-_BTmMHUIAX9Qy8up&oh=8303e6fc8444b920c0c0edc603c917b5&oe=5EE91CF3"
},
"status": "ok"
}
回答3:
If you don't have approved instagram application I suggest to use this php library: https://github.com/postaddictme/instagram-php-scraper
$instagram = Instagram::withCredentials('username', 'password', 'path/to/cache/');
$account = $instagram->getAccountById('193886659');
echo $account->getUsername();
Or direct access:
https://www.instagram.com/query/?q=ig_user(3){id,username,external_url,full_name,profile_pic_url,biography,followed_by{count},follows{count},media{count},is_private,is_verified}
Update: This url doesn't work anymore. You need to use POST. See repo to know how
回答4:
Because of the Instagram limitation on its API (i mean the SandBox), It's not easy to use instagram official API for such cases so i coded a snippet in Python which converts Instagram user ID to username & vice versa.
It uses GraphQL API to get information without any limitation from Instagram.
[+] Code Updated due to Instagram API changes (30/5/2019)
import json
import requests
import re
import hashlib
def usernameToUserId(user):
r1 = requests.get('https://www.instagram.com/web/search/topsearch/?query=' + user, headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0'}).text
if json.loads(r1)['users'][0]['user']['username'] == user:
return json.loads(r1)['users'][0]['user']['pk']
def useridToUsername(id):
if str(id).isnumeric():
r1 = requests.get('https://instagram.com/instagram/', headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0', }).text
rhx_gis = json.loads(re.compile('window._sharedData = ({.*?});', re.DOTALL).search(r1).group(1))['nonce']
ppc = re.search(r'ProfilePageContainer.js/(.*?).js', r1).group(1)
r2 = requests.get('https://www.instagram.com/static/bundles/es6/ProfilePageContainer.js/' + ppc + '.js').text
query_hash = re.findall(r'{value:!0}\);const o=\"(.*?)\"', r2)[0]
query_variable = '{"user_id":"' + str(id) + '","include_reel":true}'
t = rhx_gis + ':' + query_variable
x_instagram_gis = hashlib.md5(t.encode("utf-8")).hexdigest()
header = {'X-Instagram-GIS': x_instagram_gis,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0',
'X-Requested-With': 'XMLHttpRequest'}
r3 = requests.get(
'https://www.instagram.com/graphql/query/?query_hash=' + query_hash + '&variables=' + query_variable,
headers=header).text
username = json.loads(r3)['data']['user']['reel']['user']['username']
return username
#print(useridToUsername("1234567890"))
#print(usernameToUserId("TheUserName"))
Github Link For more Updates: https://github.com/Snbig/InstaTrack
回答5:
Now that the Instagram API referenced in the approved answer is no longer available, you'll have to resort to scraping described in the other answer. Having said that, I ended up at this SO because I was trying to figure out what a user changed their username to.
Here's what I figured out: if you've got a user ID, you've probably been scraping or use something like 4K Stogram. If that's the case, you may very well have a media page's direct URL that looks something like: https://www.instagram.com/p/Bjh4rbdHcCU/ Easiest way to get the new username is just to go directly to that URL which will have been updated with the new username. If you're automating it, it's pretty easy to pull the new username from the page's <title>
element.
I know the original poster's question was a bit different than this, but hopefully this is a solution for some of the people that end up on this page.
回答6:
Grab screen name from a post by the user (e.g. https://www.instagram.com/p/Bqfhjk_AMTq/) in python:
import requests, re, json
from bs4 import BeautifulSoup
r = requests.get('https://www.instagram.com/p/Bqfhjk_AMTq/')
soup = BeautifulSoup(r.content, "lxml")
scripts = soup.find_all('script', type="text/javascript", text=re.compile('window._sharedData'))
stringified_json = scripts[0].get_text().replace('window._sharedData = ', '')[:-1]
print json.loads(stringified_json)['entry_data']['PostPage'][0]['graphql']['shortcode_media']['owner']['username']
回答7:
https://github.com/Snbig/InstaTrack works excellent for this, can process a .txt file with list of ID and get the users as a plaint text output also.
来源:https://stackoverflow.com/questions/38356283/instagram-given-a-user-id-how-do-i-find-the-username