The InstaClient class

InstaTweet.instaclient.USER_AGENT

Hardcoded user agent proven to work with the get_user() endpoint

Version Added

v2.0.0b13

InstaClient.DOWNLOAD_DIR

[Optional] – Directory to temporarily download media to

class InstaTweet.instaclient.InstaClient(session_id, user_agent=USER_AGENT, proxies=None)[source]View on GitHub

Bases: object

Minimalistic class for scraping/downloading Instagram user/media data

__init__(session_id, user_agent=USER_AGENT, proxies=None)[source]View on GitHub

Initialize an InstaClient with an Instagram sessionid cookie (at minimum)

Note

As of v2.0.0b13, the endpoint used by get_user() seems to require a specific USER_AGENT You can override the hardcoded one if you’d like, but you’ll likely get a "useragent mismatch" response

Parameters
  • session_id (str) – valid Instagram sessionid cookie from a browser

  • user_agent (str) – user agent to use in requests made by the class

  • proxies (Optional[Dict]) – proxies to use in requests made by the class

request(url)[source]View on GitHub

Sends a request using the cookies, headers, and proxies

Parameters

url (str) – the Instagram URL to send the request to

Return type

Response

scrape(page)[source]View on GitHub

Scrapes an Instagram page and wraps the response data

Parameters

page (str) – an Instagram hashtag (prefixed with #) or username

Returns

an InstaUser or Hashtag

Return type

InstaPage

get_hashtag(tag, max_id='')[source]View on GitHub

Scrapes an Instagram hashtag and wraps the response with Hashtag

Parameters
  • tag (str) – the hashtag to scrape (with or without a #)

  • max_id (str) – the end cursor

Return type

Hashtag

get_user(username)[source]View on GitHub

Scrapes an Instagram user’s profile and wraps the response with InstaUser

Parameters

username (str) – the username of the IG user to scrape

Return type

InstaUser

get_post(shortcode)[source]View on GitHub

Scrapes an Instagram post by shortcode or URL

Parameters

shortcode (str) – the shortcode or URL of the post

Return type

Optional[InstaPost]

get_username(user_id)[source]View on GitHub

Retrieves the Instagram username for the user with the provided user_id

Tip

Use this with get_user() to scrape by user_id:

>> user_id = 51276430399
>> username = insta.get_username(user_id)
>> user = insta.get_user(username)
>> print(user.posts[0])

Post 2981866202934977614 by @dailykittenig on 2022-11-29 01:44:37

Parameters

user_id (Union[int, str]) – the id of the Instagram user to retrieve the username of

Return type

str

download_post(post, filepath=None)[source]View on GitHub

Downloads the media from an Instagram post

Parameters
Return type

bool

property headers: Dict

Headers to use in request()

property cookies: Dict

Cookies to use in request()