简体   繁体   English

如何使用node.js和javascript模仿Facebook的“链接共享”功能

[英]How to mimic Facebook's “link share” functionality using node.js and javascript

so what I want to mimic is the link share feature Facebook provides. 所以我想模仿的是Facebook提供的链接共享功能。 You simply enter in the URL and then FB automatically fetches an image, the title, and a short description from the target website. 您只需输入URL,然后FB就会自动从目标网站获取图像,标题和简短描述。 How would one program this in javascript with node.js and other javascript libraries that may be required? 如何用node.js和其他可能需要的javascript库在javascript中对此进行编程? I found an example using PHP's fopen function, but i'd rather not include PHP in this project. 我找到了一个使用PHP的fopen函数的示例,但我不想在该项目中包括PHP。

Is what I'm asking an example of webscraping? 我要问的是网络抓取的示例吗? Is all I need to do is retrieve the data from inside the meta tags of the target website, and then also get the image tags using CSS selectors? 我需要做的就是从目标网站的meta标签内部检索数据,然后再使用CSS选择器获取图像标签吗?

If someone can point me in the right direction, that'd be greatly appreciated. 如果有人可以指出正确的方向,那将不胜感激。 Thanks! 谢谢!

Look at THIS post. 这个帖子。 It discusses scraping with node.js. 它讨论了如何使用node.js进行抓取。 HERE you have lots of previous info on scraping with javascript and jquery. 在这里,您有很多有关使用javascript和jquery进行抓取的先前信息。

That said, Facebook doesn't actually guess what the title and description and preview are, they (at least most of the time) get that info from meta tags present in the sites that want to be more accessible to fb users. 就是说,Facebook实际上并没有猜测标题,描述和预览是什么,它们(至少在大多数情况下)是从想要让fb用户更容易访问的网站中存在的meta标签获取该信息的。

Maybe you could make use of that existing metadata to pull titles, descriptions and img previews. 也许您可以利用现有的元数据提取标题,描述和img预览。 The docs on the available metadata is HERE . 有关可用元数据的文档在这里

Yes web-scraping is required and that's the easy part. 是的,需要进行网络抓取,这很容易。 The hard part is the generic algo to find headings and relevant texts and images. 困难的部分是用于查找标题以及相关文本和图像的通用算法。

How to scrape 如何刮

You can use jsdom to download and create a DOM structure in your server and scrape that using jquery on your server. 您可以使用jsdom在服务器中下载并创建DOM结构,然后在服务器上使用jquery对其进行抓取。 You can find a good tutorial at blog.nodejitsu.com/jsdom-jquery-in-5-lines-on-nodejs as suggested by @generalhenry above. 如上面@generalhenry所建议,您可以在blog.nodejitsu.com/jsdom-jquery-in-5-lines-on-nodejs上找到一个很好的教程。

What to scrape 刮什么

I guess a good way to find the heading would be:- 我想找到标题的好方法是:

var h;
for(var i=6; i<=1; i++)
 if(h = $('h'+i).first()){
  break;
 }

Now h will have the title or undefined if it fails. 现在,如果h失败,它将具有标题或undefined The alternative for this could be simply get the page's title tag. 替代方法可能只是获取页面的title标签。 :) :)

As for the images. 至于图像。 List all or first few images on that page which are reasonably large, ie so as to filter out sprites used for buttons, arrows, etc. 列出该页面上相当大的所有或前几个图像,例如,以滤除用于按钮,箭头等的精灵。

And while fetching the remote data make sure that ProcessExternalResources flag is off. 在获取远程数据时,请确保已关闭ProcessExternalResources标志。 This will ensure that script tags for ads do not pollute the fetched page. 这样可以确保广告的脚本代码不会污染获取的页面。

And yes the relevant text would be in some tags after h . 是的,相关文本将在h之后的某些标记中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 NODE.JS 上模拟 php crypt() - How to mimic php crypt() on NODE.JS 使用VMWare的share文件夹开发node.js项目时,如何获取node_modules文件? - When you are using VMWare`s share folder to develop node.js project, how to node_modules file? 在javascript / node.js中重现MongoDB的地图/发射功能(没有MongoDB) - Reproducing MongoDB's map/emit functionality in javascript/node.js (without MongoDB) 如何使用 lambda javascript/node.js 将新的 s3 对象同步到其他 s3 帐户 - How to sync new s3 objects to other s3 account using lambda javascript/node.js javascript 子类 node.js 表示添加常用功能的方法? - javascript Subclass node.js express methods to add common functionality? 如何使用javascript(node.js)从Microsoft Dynamics CRM接收Webhooks? - How to receive webhooks from Microsoft's dynamics CRM using javascript(node.js)? Node.js回调功能 - Node.js callback functionality 如何使用Node.js在Facebook上发布? - How to post on Facebook with Node.js? javascript node.js变量在模块之间共享 - javascript node.js variable share among modules 在Javascript / node.js中共享模块之间的变量? - Share variables between modules in Javascript/node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM