자바스크립트/puppeteer, cheerio
* instagram 자동 로그인 + 좋아요 자동 클릭 (only puppeteer )
큰세상2000
2020. 11. 15. 22:29
반응형
참조 동영상 : https://www.youtube.com/watch?v=jw4exv4qv2E
index.js
const instagram = require('./instagram/index.js');
const instagram_id = '아이디';
const instagram_pass = '비밀번호';
(async () => {
// instagram 초기화
await instagram.initialize();
// 로그인 진행
await instagram.login(instagram_id, instagram_pass);
// 인기게시물에 좋아요 표시
await instagram.like_3_Clicks(['bts']);
// 스크래핑 브라우저 종료
await instagram.close();
})();
instragram.js
/*
Building an Instagram Like Bot with Nodejs & Puppeteer
참조 동영상 : https://www.youtube.com/watch?v=jw4exv4qv2E
첫 페이지는 여기서 로그인 버튼을 눌러서 로그인을 진행한다
https://www.instagram.com/accounts/emailsignup/
로그인 진행 페이지
https://www.instagram.com/accounts/login/?source=auth_switcher
*/
const puppeteer = require('puppeteer');
const clickTag = require('../_inc/clickTag.js');
const BASE_URL = 'https://www.instagram.com/accounts/emailsignup/';
const LOGIN_URL = 'https://www.instagram.com/';
// 'https://www.instagram.com/explore/tags/${tag}' 으로 하면 작동안함
const TAG_URL = (tag) => `https://www.instagram.com/explore/tags/${tag}`;
module.exports = {
browser: null,
page: null,
// 초기화
initialize: async () => {
browser = await puppeteer.launch({ headless: false });
page = await browser.newPage();
},
// 로그인
login: async (username, password) => {
console.log('signup screen start');
try {
/*
회원 가입 화면부터 시작할 경우
https://www.instagram.com/accounts/emailsignup/
<a href="/accounts/login/?source=auth_switcher" tabindex="0">로그인</a>
*/
await page.goto(BASE_URL, { waitUntil: 'networkidle2' });
await clickTag.delay(3000);
console.log('login button click');
await clickTag.clickByText(page, '로그인', 'a');
await page.waitForNavigation({ waitUntil: 'networkidle2' });
await clickTag.delay(1000);
/*
로그인 화면부터 시작할경우
https://www.instagram.com/accounts/login/?source=auth_switcher
https://www.instagram.com/
await page.goto(LOGIN_URL, { waitUntil: "networkidle2" });
await delay(3000);
*/
console.log('id & pass input');
// 아이디와 비빌번호 입력
await page.type('input[name="username"]', username, {
delay: 50,
});
await page.type('input[name="password"]', password, {
delay: 50,
});
/*
로그인 페이지로 넘어가는 버튼 누르기
<div class="Igw0E IwRSH eGOV_ _4EzTm">로그인</div>
*/
console.log('login button click');
await clickTag.clickByText(page, '로그인', 'div');
await clickTag.delay(10000);
} catch (e) {
console.log(e);
}
},
// 인기게시물 사진 3개에 좋아요 클릭하기
// document.querySelectorAll('article > div:nth-child(3) > div > div:nth-child(1) img[decoding="auto"]');
like_3_Clicks: async (tags = []) => {
console.log('photo 3 url loading');
try {
for (let tag of tags) {
await page.goto(TAG_URL(tag), {
waitUntil: 'networkidle2',
});
await clickTag.delay(1000);
let posts = await page.$$(
'article > div.EZdmt > div > div > div:nth-child(1) img[decoding="auto"]',
);
for (let i = 0; i < 3; i++) {
let post = posts[i];
console.log('modal window open');
// 포스트 클릭
await post.click();
await clickTag.delay(3000);
/*
좋아요 버튼
<span class="fr66n"><button class="wpO6b " type="button"><div class="QBdPU ">
<span class=""><svg aria-label="좋아요" class="_8-yf5 " fill="#262626" height="24" viewBox="0 0 48 48" width="24">
<path d="M34.6 6.1c5"></path></svg></span></div></button></span>
모달창 닫기 버튼
<div class="QBdPU "><svg aria-label="닫기" class="_8-yf5 " fill="#ffffff" height="24" viewBox="0 0 48 48"
width="24"><path clip-rule="evenodd" d="M41" fill-rule="evenodd"></path></svg></div>
*/
console.log('like button click');
// 좋아요 클릭
await page.waitForSelector('.fr66n button');
await page.click('.fr66n button');
await clickTag.delay(500);
console.log('modal window close');
// 모달창 닫기
await page.click('.BI4qX button');
}
}
} catch (e) {
console.log(e);
}
},
// 스크래핑 중단
close: async () => {
console.log('browser close');
await browser.close();
},
};
반응형