首页 > 解决方案 > 在 PHP 中解析和处理 HTML:使用刮刀获取 Wordpress-plugin-Metadata

问题描述

如何在 PHP 中解析和处理 HTML - 一个简单的爬虫

我目前正在开发一个解析器,以便从用户在 PHP 中给出的 URL 对页面进行小预览。

我想只检索页面的标题和一小部分信息(一些文本)

该项目:获取流行的 wordpress 插件的元数据列表(参见https://de.wordpress.org/plugins/browse/popular/并收集前 50 个 URL - 这是 50 个感兴趣的插件!挑战是:我想获取所有现有插件的元数据。我随后想要在获取后过滤掉的是 - 那些具有最新时间戳的插件 - 最近更新(最近)。这一切都是...

https://wordpress.org/plugins/wp-job-manager
https://wordpress.org/plugins/ninja-forms
https://wordpress.org/plugins/participants-database ....and so on and so forth.

使用元文本信息查看一页的信息

所以要考虑一页 - 获取一个 Wordpress 插件的元数据:使用 simple_html_dom (http://simplehtmldom.sourceforge.net/)我想有一种适当的方式和方法可以在没有任何其他外部的情况下做到这一点库/类。到目前为止,我还尝试使用一般(DOM)-DOCDocument 类http://docs.php.net/manual/en/domdocument.loadhtml.php),加载 HTML 并将其显示在屏幕上,现在我思考正确的方法来做到这一点。我为此考虑 simple_html_dom ( http://simplehtmldom.sourceforge.net/ )。这将使它变得非常容易。这是一个如何提取标题和元文本(描述)的示例。

<?php
require 'simple_html_dom.php';

$html = file_get_html('https://wordpress.org/plugins/wp-job-manager/');
$title = $html->find ("h1", class_="plugin-title").text];
$text  = $html->find(class_="entry-meta").text];

echo $title->plaintext."<br>\n";
echo $texte->text;
?>

查看源代码https ://wordpress.org/plugins/wp-job-manager/ 我们为每个 wordpress-plugin 提供以下元数据集:

Version: 1.9.5.12 
installations: 10,000+    
WordPress Version: 5.0 or higher 
Tested up to: 5.4 PHP  
Version: 5.6 or higher    
Tags 3 Tags: database member sign-up form volunteer
Last updated: 19 hours ago
plugin-ratings

该项目由两部分组成:循环部分:循环访问此 URL https://de.wordpress.org/plugins/browse/popular/并收集大约 50 到 80 个 URL(这似乎很简单)。解析器部分:我有一些问题 - 正确获取标签和插件评级的数据......

更新:插件 api。可以在这里提供帮助 - 一个很好的方法是 cf Getting a list of ALL plugins

标签: phpwordpresscurlweb-scraping

解决方案


推荐阅读