Skip to content

Instantly share code, notes, and snippets.

@diragb
Last active August 1, 2023 17:06
Show Gist options
  • Save diragb/5a5cc95360d364ecfbacb03978fce1dd to your computer and use it in GitHub Desktop.
Save diragb/5a5cc95360d364ecfbacb03978fce1dd to your computer and use it in GitHub Desktop.
Quick scraper for Instagram profiles.
const getInstagramHeaders = async () => {
const options = {
method: 'GET',
credentials: 'same-origin',
}
const response = await fetch('https://www.instagram.com/a.thousand.apologies/', options)
const parser = new DOMParser()
const html = await response.text()
const document = parser.parseFromString(html, 'text/html')
return {
...Object.fromEntries(response.headers.entries()),
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) 20100101 Firefox/103.0',
'Accept': '*/*',
'Accept-Language': 'en,en-US;q=0.3',
'X-Csrftoken': document.body.innerHTML.split('csrf_token')[1].split('\\"')[2],
'X-IG-App-ID': document.body.innerHTML.split('X-IG-App-ID')[1].split(',')[0].replaceAll('"', '').replace(':', ''),
'X-ASBD-ID': '198337',
'X-IG-WWW-Claim': '0',
'Origin': 'https://www.instagram.com',
'DNT': '1',
'Alt-Used': 'i.instagram.com',
'Connection': 'keep-alive',
'Referer': 'https://www.instagram.com/',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'Sec-GPC': '1',
}
}
//
const getInstagramProfile = async (username) => {
const fetchedHeaders = await getInstagramHeaders()
const headers = new Headers(fetchedHeaders);
const response = await fetch(`https://www.instagram.com/api/v1/users/web_profile_info/?username=${username}`, {
method: 'GET',
headers
})
return await response.json()
}
const getInstagramPhotoIDs = async (username) => {
const profile = await getInstagramProfile(username)
const edges = profile.data.user.edge_owner_to_timeline_media.edges
const shortCodes = edges.map(edge => edge.node.shortcode)
return shortCodes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment