Last active
December 13, 2020 10:43
-
-
Save clins1994/f2a30b1386f94a249fb9e2fa46370704 to your computer and use it in GitHub Desktop.
A script that logs you into Instagram using JavaScript & Google Puppeteer (headless Chrome)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* INSTRUCTIONS ON HOW TO RUN VIA COMMAND LINE | |
* $ node -v | |
* $ 12.14.0 | |
* $ npm -v | |
* $ 6.14.9 | |
* $ cd directory-containing-log-in-to-instagram-script | |
* $ npm init | |
* $ npm install puppeteer | |
* $ node log-in-to-instagram.js | |
*/ | |
const puppeteer = require('puppeteer'); | |
const instagramUrl = 'https://www.instagram.com'; | |
const instagramLoginPath = 'accounts/login'; | |
const user = 'yourUsername'; // change this to your username | |
const pass = 'yourPassword'; // change this to your password | |
const usernameFieldSelector = 'input[type=text][name=username]'; | |
const passwordFieldSelector = 'input[type=password][name=password]'; | |
const loginButtonSelector = 'button[type=submit]'; | |
const statCountSelectorMap = new Map([ | |
['posts', '#react-root > section > main > div > ul > li:nth-child(1) > span > span'], | |
['followers', '#react-root > section > main > div > ul > li:nth-child(2) > a > span'], | |
['following', '#react-root > section > main > div > ul > li:nth-child(3) > a > span'], | |
]); | |
const browserOptions = { | |
headless: true, | |
slowMo: 100, | |
defaultViewport: { width: 350, height: 600 }, | |
isMobile: true, | |
}; | |
(async () => { | |
// initialize the browser | |
const browser = await puppeteer.launch(browserOptions); | |
// initialize page to be scraped | |
const page = await browser.pages().then((pages) => pages[0]); | |
// navigate to the login page | |
await page.goto(`${instagramUrl}/${instagramLoginPath}`); | |
// wait for username field to appear | |
await page.waitForSelector(usernameFieldSelector); | |
// fill in username and password then click login button | |
console.log('filling username ...\n'); | |
await page.click(usernameFieldSelector); | |
await page.type(usernameFieldSelector, user); | |
console.log('filling password ...\n'); | |
await page.click(passwordFieldSelector); | |
await page.type(passwordFieldSelector, pass); | |
console.log('clicking login button ...\n'); | |
await page.waitForSelector(loginButtonSelector); | |
await page.click(loginButtonSelector); | |
await page.waitForTimeout(2000); | |
// navigate to your instagram page | |
console.log('looks like we logged in\n'); | |
console.log('navigating to the instagram page of this user\n'); | |
await page.goto(`${instagramUrl}/${user}`); | |
await page.waitForSelector(statCountSelectorMap.get('posts')); | |
console.log(`here are the stats for ${user}`); | |
statCountSelectorMap.forEach(async (selector, stat) => { | |
const statCountElementHandle = await page.$(selector); | |
const statCountJSHandle = await statCountElementHandle.getProperty('innerText'); | |
const statCount = await statCountJSHandle.jsonValue(); | |
console.log(`\t# of ${stat}: `, statCount); | |
}); | |
console.log('\ndone, closing browser\n'); | |
await browser.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment