Skip to content

Instantly share code, notes, and snippets.

@onurmatik
Last active October 3, 2017 08:21
Show Gist options
  • Save onurmatik/321cd9b8d828272d3463b8688bd4aa77 to your computer and use it in GitHub Desktop.
Save onurmatik/321cd9b8d828272d3463b8688bd4aa77 to your computer and use it in GitHub Desktop.
A Pyhton script to fetch Twitter user profile images and save them locally
import requests
import urllib
# 'usernames.txt' contains a list of Twitter usernames; one at each line
f = open('usernames.txt')
# Photo addresses are saved into 'images.txt'
out = open('images.txt', 'w')
# for each username, the photo file itself is save under 'photo/' folder
# uncomment the line below for desired image size
# profile_image_url = 'https://twitter.com/%s/profile_image?size=mini'
profile_image_url = 'https://twitter.com/%s/profile_image?size=normal'
# profile_image_url = 'https://twitter.com/%s/profile_image?size=bigger'
# profile_image_url = 'https://twitter.com/%s/profile_image?size=original'
for name in f:
name = name.strip()
r = requests.get(profile_image_url % name)
url = r.url.startswith('https://twitter.com/') and '-' or r.url
out.write(url + '\n')
print url
if url != '-':
extension = r.url.split('.')[-1]
if len(extension) > 6:
urllib.urlretrieve(r.url, 'images/%s' % name)
else:
urllib.urlretrieve(r.url, 'images/%s.%s' % (name, extension))
out.close()
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment