PHP 总结任意 URL

发布于 2024-09-04 18:44:34 字数 151 浏览 4 评论 0原文

在 PHP 中,如何获取任意 URL 的摘要?总而言之,我的意思是类似于 Google 网络搜索结果中的 URL 描述。

这可能吗?是否已经有某种我可以插入的工具,这样我就不必生成自己的摘要?

如果可能的话,我不想使用元数据描述。

-迪伦

How can I, in PHP, get a summary of any URL? By summary, I mean something similar to the URL descriptions in Google web search results.

Is this possible? Is there already some kind of tool I can plug in to so I don't have to generate my own summaries?

I don't want to use metadata descriptions if possible.

-Dylan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

掐死时间 2024-09-11 18:44:34

Google 中显示的(通常)是 META 描述标签。如果您不想使用它,也可以使用页面标题。

What displays in Google is (generally) the META description tag. If you don't want to use that, you could use the page title instead though.

凉月流沐 2024-09-11 18:44:34

如果您不想使用元数据描述(顺便说一句,这正是它们的用途),您需要做大量的研究和工作。本质上,您必须猜测页面的哪一部分是内容,哪一部分只是导航/绒毛。事实上,谷歌正是这样的;但请注意,从无用的废话中提取有价值的信息是他们的第一能力,十年来他们一直在研究和改进这一能力。

当然,您可以进行有根据的猜测(例如“查找具有 ID 或类 maincontent 的元素”并从中获取第一段),也许就可以了。真正的问题是,您希望结果有多好? (Facebook 有类似的网站链接,有时摘要只是坚持广告是主要内容)。

If you don't want to use metadata descriptions (btw, this is exactly what they are for), you have a lot of research and work to do. Essentially, you have to guess which part of the page is content and which is just navigation/fluff. Indeed, Google has exactly that; note however, that extracting valuable information from useless fluff is their #1 competency and they've been researching and improving that for a decade.

You can, of course, make an educated guess (e.g. "look for an element with ID or class maincontent" and get the first paragraph from it) and maybe it will be OK. The real question is, how good do you want the results to be? (Facebook has something similar for linking to websites, sometimes the summary just insists that an ad is the main content).

夜访吸血鬼 2024-09-11 18:44:34

下面将允许您解析页面的 title 标记的内容。注意:必须将 php 配置为允许 file_get_contents 检索 URL。否则,您必须使用 curl 来检索页面 HTML。

$title_open = '<title>';
$title_close = '</title>';

$page = file_get_contents( 'http://www.domain.com' );
$n = stripos( $page, $title_open ) + strlen( $title_open );
$m = stripos( $page, $title_close);

$title = substr( $page, n, m-n );

The following will allow you to to parse the contents of a page's title tag. Note: php must be configured to allow file_get_contents to retrieve URLs. Otherwise you'll have to use curl to retrieve the page HTML.

$title_open = '<title>';
$title_close = '</title>';

$page = file_get_contents( 'http://www.domain.com' );
$n = stripos( $page, $title_open ) + strlen( $title_open );
$m = stripos( $page, $title_close);

$title = substr( $page, n, m-n );
指尖上的星空 2024-09-11 18:44:34

虽然我讨厌推广服务,但我发现了这个:

embed.ly

它有一个 API,可以返回包含您需要的所有数据的 JSON。

但我仍在寻找一个免费/开源库来完成同样的事情。

While i hate promoting a service i have found this:

embed.ly

It has an API, that returns a JSON with all the data you need.

But i am still searching for a free/opensource library to do the same thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文