提前澄清一下,我没有 Facebook 帐户,也无意创建一个。此外,我正在努力实现的目标在我的国家和美国完全合法。
我不想使用 Facebook API 来获取 Facebook 页面的最新时间线帖子,而是想直接向页面 URL(例如this page)发送获取请求并从 HTML 源代码中提取帖子。
(我想获取帖子的文本和创建时间。)
当我在 Web 控制台中运行它时:
document.getElementsByClassName('userContent')
我得到一个包含最新帖子文本的元素列表。
但我想从 nodejs 脚本中提取该信息。我可能可以使用无头浏览器puppeteer
之类的无头浏览器很容易地做到这一点,但这会产生大量不必要的开销。我真的很想要一个简单的方法,比如下载 HTML 代码,将它传递给cheerio 并使用cheeriio 的类似 jQuery 的 API 来提取帖子。
这是我尝试的尝试:
// npm i request cheerio request-promise-native
const rp = require('request-promise-native'); // requires installation of `request`
const cheerio = require('cheerio');
rp.get('https://www.facebook.com/pg/officialstackoverflow/posts/').then( postsHtml => {
const $ = cheerio.load(postsHtml);
const timeLinePostEls = $('.userContent');
console.log(timeLinePostEls.html()); // should NOT be null
const newestPostEl = timeLinePostEls.get(0);
console.log(newestPostEl.html()); // should NOT be null
const newestPostText = newestPostEl.text();
console.log(newestPostText);
//const newestPostTime = newestPostEl.parent(??).child('.livetimestamp').title;
//console.log(newestPostTime);
}).catch(console.error);
不幸$('.userContent')
的是不起作用。但是,我能够验证我要查找的数据是否嵌入在该 HTML 代码中的某个位置。
但我真的无法想出一个好的正则表达式方法或类似的方法来提取该数据。
根据帖子内容,帖子中 HTML 标记的数量差异很大。
这是一个包含一个链接的帖子的简单示例:
<div class="_5pbx userContent _3576" data-ft="{"tn":"K"}"><p>We're proud to be named one of Built In NYC's Best Places to Work in 2019, ranking in the top 10 for Best Midsize Places to Work and top 3 (!) for Best Perks and Benefits. See what it took to make the list and check out our profile to see some of our job openings. <a href="https://l.facebook.com/l.php?u=https%3A%2F%2Fbit.ly%2F2H3Kbr2&h=AT29h2HyDsEk0rHRWqJA-Fa4M1qi3nJT1NBi95othaR3qeFuFAMNiVS2Dgtv5KR5m0xqjw6kfwZdhZt0_D3UQT1Oel2UhxRql-KwkA1xqWvrql4u1jDhzrkGVT_XxoUd8_w8_fzLZzzhz23a8yPCK6IPfWKB76_CEFjG3b78y4dFJvY9Z08AYlR01dmi5_FvWVEVytkN-123u6alYE8pqL6Jb6dtIQUTWGXYJPaNMrtxkCUZniEVXEcILkwHGSuHqCTAarboyMP55F1vhYO3OAiVMkvjbN274fVq92YvbK3bi90bU9T-5ADWHDUJ-CwcofSBTW47chstQeY0n_UluD_rBIPLsfXVSnCtpRkR2kXi9zzHLnNeIYeNssv3i7UKS_f5Z2pnVT6xe3zJbNpB68doH1Z__I9nsTCNIyFyKx2VxabecoL03DIawbRrzBoxLAwzNPLACBjTkpEQhdVn4_wdAIjXRL4cLQDcZkLEoG_sspBgRePH23TFbNufQOBly-FNtLHnkUDO2Ca-FYvAGXpcu6J4B1aH3XFPB803lsz-GRdACyOFOgXDXJfwr4WtWzUHxfiOPULWiI43yI5L4aU6wYRhPjxua3RuRZ8oj9fXa1w4Jrht94Ue2wfKtz8" target="_blank" data-ft="{"tn":"-U"}" rel="noopener nofollow" data-lynx-mode="async">http://*******/2H3Kbr2</a></p></div>
以更易读的形式格式化,它看起来有点像这样:
<div class="_5pbx userContent _3576" data-ft="{"tn":"K"}">
<p>
We're proud to be named one of Built In NYC's Best Places to Work in
2019, ranking in the top 10 for Best Midsize Places to Work and top 3 (!) for
Best Perks and Benefits. See what it took to make the list and check out our
profile to see some of our job openings.
<a href="VERY_LONG_URL.........." target="_blank" data-ft="{"tn":"-U"}" rel="noopener nofollow" data-lynx-mode="async">SHORT_LINK.....</a>
</p>
</div>
这个正则表达式似乎可以正常工作,但我认为它不是很可靠:
/<div class="[^"]+ userContent [^"]+" data-ft="[^"]+">(.+?)<\/div>/g
例如,如果帖子包含另一个 div 元素,那么它将无法正常工作。除此之外,我无法知道使用这种方法创建帖子的时间/日期?
有什么想法可以相对可靠地提取最近的 2-3 个帖子,包括创建日期/时间?