Skip to content

Instantly share code, notes, and snippets.

@VincentSit
Created December 9, 2015 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VincentSit/39406d95514c72816975 to your computer and use it in GitHub Desktop.
Save VincentSit/39406d95514c72816975 to your computer and use it in GitHub Desktop.
download instagram image for specific user
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from urllib import request
import json
import os
uid = 260732811
access_token = '277504095.59c578a.6aed864be0794247a1c0318221b03d7b'
base_url = r'https://api.instagram.com/v1/users/%s/media/recent/?access_token=%s' % (uid, access_token)
def download(url):
with request.urlopen(url) as f:
json_data = f.read().decode('utf-8')
dic = json.loads(json_data)
dataArray = dic['data']
for d in dataArray:
ty = d['type']
if ty == 'image':
s_url = d['images']['standard_resolution']['url']
else:
s_url = d['videos']['standard_resolution']['url']
print(s_url)
dire = 'instagram'
if not os.path.exists(dire):
os.makedirs(dire)
filename = os.path.join(dire, os.path.basename(s_url))
output = open(filename, 'wb')
output.write(request.urlopen(s_url).read())
output.close()
next_url = dic['pagination']['next_url']
if next_url:
download(next_url)
else:
print('下载完成')
download(base_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment