Well, some time ago as I was sitting at my computer ogling thots on Instagram, I ran into this problem, and I decided to find a way around it. After combing through Instagram’s entire interface for a way to download photos directly and finding none, I opened the page source and started doing a little reverse engineering. I determined the exact location within the file at which the image URL was stored, and I got to work writing the following script which I call Instaget (keep in mind this was nearly two years ago and Instagram may have been revamped since then, so it might not actually work anymore)…
1 #!/usr/bin/env bash 2 # Argument: Instagram URL 3 4 # Get HTML file 5 url=`echo "$1" | sed "s/\?.*$//"` 6 wget "$url" 7 # Search file for image URL 8 imgurl=`grep "jpg" "index.html" | head -1 | sed "s/^.*content=\"//" | sed "s/\".*$//"` 9 # Get image 10 wget "$imgurl" 11 # Cleanup 12 rm "index.html" 13 unset url imgurl
This script is fairly straightforward, with only a couple parts that might warrant explanation. The script downloads the HTML file and operates on it locally, just because it’s easier that way. The
sed command in Line 5 is there to remove the query string from the URL, just in case it causes problems (can’t hurt to make sure). Line 8 finds the image URL by extracting it from the first line in the HTML file that has a JPEG filename. This is the location of the image URL, and its form is
content="filename.jpg". With that in mind, the two remaining
sed commands chop off everything before and after the image URL. After that, we can simply download it directly from Instagram with
The shortcoming of this script is of course that it only works on Instagram, not to mention if Instagram’s code is ever changed significantly, it could break the script. It might be a fun afternoon project to also write versions of the script for Photobucket, Pinterest, and any other image hosting sites with annoying unintuitive interfaces.