페이스북 개인 담벼락에 올라가 있는 포스트들의 좋아요 수를 크롤링 하는 예제 코드입니다.
개인적인 연구(?)를 위해 작성해 보았고 관련된 글은 이곳에서 보실 수 있습니다.
이 코드는 해당 글의 2탄을 위해 작성되었습니다. ㅋ
* 직접 해 보신분들은 아시겠지만 facebook graph API는 개인 담벼락의 포스트나 좋아요 수등을 제공해주지 않습니다.
이에, 크롤링을 통해 개인 담벼락에 있는 포스트들의 좋아요 수를 수집하는 코드입니다.
<사용하기>
먼저, Selenium과 webdriver 의 설치가 필수 입니다.
이를 위해 이 링크를 참고해 주세요.
그리고, 아래 코드에서 주석의 설명을 따라 수정해 주시면 됩니다.
수집된 좋아요 수는 out.txt 파일에 페북 개인 아이디와 함께 기록됩니다.
package org.airpage.facebook.scrap;
import java.util.List;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FacebookPostScrapper {
public static String fbID = "FACEBOOK ID";
public static String fbPW = "FACEBOOK PW";
public static int maxCount = 50; //gathering 'Like' count for each ID
public static ArrayList<String> fbIDList = new ArrayList<String>();
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\DEVTOOLS\\chromedriver.exe"); //path for chromedriver
WebDriver _driver = new ChromeDriver();
_driver.get("https://www.facebook.com/");
WebElement loginID = _driver.findElement(By.id("email"));
WebElement loginPassword = _driver.findElement(By.id("pass"));
loginID.sendKeys(fbID);
loginPassword.sendKeys(fbPW);
loginPassword.submit();
//수집하고자 하는 담벼락 id
fbIDList.add("airpage");
for(String fbid : fbIDList) {
startScrap(_driver, fbid);
}
System.out.println("드라이버 종료");
_driver.close();
}
public static void startScrap(WebDriver _driver, String fbid) {
_driver.get("https://www.facebook.com/" + fbid);
JavascriptExecutor jse = (JavascriptExecutor) _driver;
jse.executeScript("scrollBy(0,2500);");
jse.executeScript("scrollBy(0,2500);");
int tryCount = 0;
int allCount = 0;
int retCount = 0;
ArrayList<String> likeIDList = new ArrayList<String>();
while (true) {
retCount = getPosts(_driver, fbid, allCount, likeIDList);
if (retCount == 0) {
tryCount++;
System.out.println("tryCount :" + tryCount);
if (tryCount >= 5) //더이상 새로운 post가 없음
break;
} else {
tryCount = 0;
}
allCount += retCount;
System.out.println("allCount :" + allCount);
if(allCount >= maxCount) break;
jse.executeScript("scrollBy(0,2500);");
jse.executeScript("scrollBy(0,2500);");
}
}
public static void writeLikecount(String fbid, String likecount) {
BufferedWriter out;
try {
out = new BufferedWriter(new FileWriter("out.txt", true));
String s = fbid + ":" + likecount;
out.write(s);
out.newLine();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int getPosts(WebDriver _driver, String fbid, int currentCount, ArrayList<String> likeIDList) {
int count = 0;
List<WebElement> posts = null;
try {
posts = _driver.findElements(By.cssSelector("span[data-tooltip-uri^='/ufi/reaction/tooltip/?ft_ent_identifier']"));
for (WebElement post : posts) {
String likeAttribute = post.getAttribute("data-tooltip-uri");
//if (likeAttribute != null && likeAttribute.contains("/ufi/reaction/tooltip/?ft_ent_identifier=")) {
if (likeIDList.contains(likeAttribute) == false) {
String likeCount = post.getText();
if (likeCount != null) {
System.out.println("==> Like Count: " + likeCount);
count++;
writeLikecount(fbid, likeCount);
if ((currentCount + count) >= maxCount) break;
}
likeIDList.add(likeAttribute);
}
//}
}
}
catch (StaleElementReferenceException e) {
System.out.println("EXCEPTION");
}
return count;
}
}