Skip to content

Instantly share code, notes, and snippets.

@liruqi
Last active December 26, 2019 10:04
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save liruqi/7f425bd570fa8a7c73be to your computer and use it in GitHub Desktop.
Save liruqi/7f425bd570fa8a7c73be to your computer and use it in GitHub Desktop.
Get facebook fans email
var run = 0;
var mails = {}
total = 3000; //滚动次数,可以自己根据情况定义
function getEmails (cont) {
var friendbutton=cont.getElementsByClassName("_ohe");
for(var i=0; i<friendbutton.length; i++) {
var link = friendbutton[i].getAttribute("href");
if(link && link.substr(0,25)=="https://www.facebook.com/") {
var parser = document.createElement('a');
parser.href = link;
if (parser.pathname) {
path = parser.pathname.substr(1);
if (path == "profile.php") {
search = parser.search.substr(1);
var args = search.split('&');
email = args[0].split('=')[1] + "@facebook.com\n";
} else {
email = parser.pathname.substr(1) + "@facebook.com\n";
}
if (mails[email] > 0) {
continue;
}
mails[email] = 1;
console.log(email);
}
}
}
}
function moreScroll() {
var text="";
containerID = "BrowseResultsContainer"
if (run > 0) {
containerID = "fbBrowseScrollingPagerContainer" + (run-1);
}
var cont = document.getElementById(containerID);
if (cont) {
run++;
var id = run - 2;
if (id >= 0) {
setTimeout(function() {
containerID = "fbBrowseScrollingPagerContainer" + (id);
var delcont = document.getElementById(containerID);
if (delcont) {
getEmails(delcont);
delcont.parentNode.removeChild(delcont);
}
window.scrollTo(0, document.body.scrollHeight - 10);
}, 1000);
}
} else {
console.log("# " + containerID);
}
if (run < total) {
window.scrollTo(0, document.body.scrollHeight + 10);
}
setTimeout(moreScroll, 2000);
}//1000为间隔时间,也可以根据情况定义
moreScroll();
<?php
//
function fetch_fb_fans($fanpage_name, $no_of_retries = 10, $pause = 100000 /* 500ms */){
$ret = array();
// get page info from graph
$fanpage_data = json_decode(file_get_contents('http://graph.facebook.com/' . $fanpage_name), true);
if(empty($fanpage_data['id'])){
// invalid fanpage name
return $ret;
}
$matches = array();
$url = 'http://www.facebook.com/plugins/fan.php?connections=100&id=' . $fanpage_data['id'];
echo $url . "\n";
for($a = 0; $a < $no_of_retries; $a++){
$randIP = "".mt_rand(0,255).".".mt_rand(0,255).".".mt_rand(0,255).".".mt_rand(0,255);
$context = stream_context_create(array('http' => array('header' => 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0\nX-Forwarded-For:' . $randIP)));
$like_html = file_get_contents($url, false, $context);
preg_match_all('{href="https?://www\.facebook\.com/([a-zA-Z0-9._-]+)" data-jsid="anchor" target="_blank"}', $like_html, $matches);
if(empty($matches[1])){
// failed to fetch any fans - convert returning array, cause it might be not empty
// return array_keys($ret);
$pause = $pause * 2;
echo "# failed: {$pause}";
}else{
$all = ($matches[1]);
foreach ($all as $key => $value) {
if (isset($ret[$value])) {
echo ".\n";
continue;
} else {
$ret[$value] = 1;
echo "{$value}@facebook.com\n";
}
}
// merge profiles as array keys so they will stay unique
// $ret = array_merge($ret, array_flip($matches[1]));
}
// don't get banned as flooder
usleep($pause);
}
return array_keys($ret);
}
print_r(fetch_fb_fans('Lamoda.ru', 10000));
@rasagnareddy
Copy link

Hi liruqi,

This is awesome,,, But it's not listing all members who liked the page.
And also is there a way we can get fans birthdate to publish wishes for them??
I am new to FB app development and learning it out of interest :)
Any help will be appreciated.

Thanks
Rasagna

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment