Crawl all images on a user’s instagram with Python requests.

Tram Ho


This article guides you who are new to python syntax and can use python’s requests library to simply download an Instagram account image.


install requests library: pip install requests


First, we access the page of the user needing to crawl the F12 -> network image, easily find the api to get instagram photos

The api has the form:

first: the number of images will start from after. With after = “” we get the first 12 images (after = end_cursor of requests before it)

So our crawl will be the first api request -> Image crawl, end_cursor, check if the page is behind? -> send api again with end_cursor in previous api call if available.

Image crawl process: Using results from api requests -> switch to json -> check whether the post is a photo or video-> Check to see if there are other images -> get image urls

Check if there is a next page and get end_cursor:

Finally, create a new folder and upload the photos:

Full Code:

You can download the source code, replace the id with the id found in the api to start downloading the image.

p / s: code is written in the most rudimentary way, encouraging revision to be cleaner.

Share the news now

Source : Viblo