最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Extracting the source code of a facebook page with JavaScript - Stack Overflow

programmeradmin1浏览0评论

If I write code in the JavaScript console of Chrome, I can retrieve the whole HTML source code by entering:

  var a = document.body.InnerHTML; alert(a); 

For fb_dtsg on Facebook, I can easily extract it by writing:

  var fb_dtsg = document.getElementsByName('fb_dtsg')[0].value;

Now, I am trying to extract the code "h=AfJSxEzzdTSrz-pS" from the Facebook Page. The h value is especially useful for Facebook reporting.

How can I get the h value for reporting? I don't know what the h value is; the h value is totally different when you municate with different users. Without that h correct value, you can not report. Actually, the h value is AfXXXXXXXXXXX (11 character values after 'Af'), that is what I know.

Do you have any ideas for getting the value or any function to generate on Facebook page.

The Facebook Source snippet is below, you can view source on facebook profile, and search h=Af, you will get the value:

  <code class="hidden_elem" id="ukftg4w44">
<!-- <div class="mtm mlm">
  ...
   ....
  <span class="itemLabel fsm">Unfriend...</span></a></li>
  <li class="uiMenuItem" data-label="Report/Block...">
  <a class="itemAnchor" role="menuitem" tabindex="-1" href="/ajax/report/social.php?content_type=0&amp;cid=1352686914&amp;rid=1352686914&amp;ref=http%3A%2F%2Fwww.facebook%2      F%3Fq&amp;h=AfjSxEzzdTSrz-pS&amp;from_gear=timeline" rel="dialog">
  <span class="itemLabel fsm">Report/Block...</span></a></li></ul></div>

  ...
   ....
  </div> -->
  </code>

Please guide me. How can extract the value exactly?

I tried with following code, but the ment block prevent me to extract the code. How can extract the value which is inside ment block?

 var a = document.getElementsByClassName('hidden_elem')[3].innerHTML;alert(a);

If I write code in the JavaScript console of Chrome, I can retrieve the whole HTML source code by entering:

  var a = document.body.InnerHTML; alert(a); 

For fb_dtsg on Facebook, I can easily extract it by writing:

  var fb_dtsg = document.getElementsByName('fb_dtsg')[0].value;

Now, I am trying to extract the code "h=AfJSxEzzdTSrz-pS" from the Facebook Page. The h value is especially useful for Facebook reporting.

How can I get the h value for reporting? I don't know what the h value is; the h value is totally different when you municate with different users. Without that h correct value, you can not report. Actually, the h value is AfXXXXXXXXXXX (11 character values after 'Af'), that is what I know.

Do you have any ideas for getting the value or any function to generate on Facebook page.

The Facebook Source snippet is below, you can view source on facebook profile, and search h=Af, you will get the value:

  <code class="hidden_elem" id="ukftg4w44">
<!-- <div class="mtm mlm">
  ...
   ....
  <span class="itemLabel fsm">Unfriend...</span></a></li>
  <li class="uiMenuItem" data-label="Report/Block...">
  <a class="itemAnchor" role="menuitem" tabindex="-1" href="/ajax/report/social.php?content_type=0&amp;cid=1352686914&amp;rid=1352686914&amp;ref=http%3A%2F%2Fwww.facebook.%2      F%3Fq&amp;h=AfjSxEzzdTSrz-pS&amp;from_gear=timeline" rel="dialog">
  <span class="itemLabel fsm">Report/Block...</span></a></li></ul></div>

  ...
   ....
  </div> -->
  </code>

Please guide me. How can extract the value exactly?

I tried with following code, but the ment block prevent me to extract the code. How can extract the value which is inside ment block?

 var a = document.getElementsByClassName('hidden_elem')[3].innerHTML;alert(a);
Share Improve this question edited Nov 20, 2012 at 22:40 Gurpreet Singh 21.3k5 gold badges46 silver badges61 bronze badges asked Nov 20, 2012 at 22:18 Hafizi VilieHafizi Vilie 371 gold badge1 silver badge7 bronze badges 1
  • 1 Why not just use the actual API? It has to be easier than illegally* scraping the main website (*- may not be illegal in your country, not a lawyer, etc etc) – Igy Commented Nov 20, 2012 at 23:04
Add a ment  | 

1 Answer 1

Reset to default 2

Here's my first attempt, assuming you aren't afraid of a little jQuery:

// http://stackoverflow./a/5158301/74757
function getParameterByName(name, path) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(path);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

var html = $('.hidden_elem')[0].innerHTML.replace('<!--', '').replace('-->', '');
var href = $(html).find('.itemAnchor').attr('href');
var fbId = getParameterByName('h', href); // fbId = AfjSxEzzdTSrz-pS

Working Demo

EDIT: A way without jQuery:

// http://stackoverflow./a/5158301/74757
function getParameterByName(name, path) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(path);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

var hiddenElHtml = document.getElementsByClassName('hidden_elem')[0]
    .innerHTML.replace('<!--', '').replace('-->', '');

var divObj = document.createElement('div');
divObj.innerHTML = hiddenElHtml;

var itemAnchor = divObj.getElementsByClassName('itemAnchor')[0];
var href = itemAnchor.getAttribute('href');

var fbId = getParameterByName('h', href);

Working Demo

I'd really like to offer a different solution for "unmenting" the HTML, but I stink at regex :)

发布评论

评论列表(0)

  1. 暂无评论