我是PHP开发的新手,我想提取元标记的内容。
我有这个代码,允许我提取元素#quad的内容。
// Pull in PHP Simple HTML DOM Parser
include("simplehtmldom/simple_html_dom.php");
// Settings on top
$sitesToCheck = array(
// id is the page ID for selector
array("url" => "http://www.arsenal.com/first-team/players", "selector" => "#squad"),
array("url" => "http://www.liverpoolfc.tv/news", "selector" => "ul[style='height:400px;']")
);
$savePath = "cachedPages/";
$emailContent = "";
// For every page to check...
foreach($sitesToCheck as $site) {
$url = $site["url"];
// Calculate the cachedPage name, set oldContent = "";
$fileName = md5($url);
$oldContent = "";
// Get the URL's current page content
$html = file_get_html($url);
// Find content by querying with a selector, just like a selector engine!
foreach($html->find($site["selector"]) as $element) {
$currentContent = $element->plaintext;;
}
// If a cached file exists
if(file_exists($savePath.$fileName)) {
// Retrieve the old content
$oldContent = file_get_contents($savePath.$fileName);
}
// If different, notify!
if($oldContent && $currentContent != $oldContent) {
// Build simple email content
$emailContent = "Hey, the following page has changed!\n\n".$url."\n\n";
}
// Save new content
file_put_contents($savePath.$fileName,$currentContent);
}
// Send the email if there's content!
if($emailContent) {
// Sendmail!
mail("me@myself.name","Sites Have Changed!",$emailContent,"From: alerts@myself.name","\r\n");
// Debug
echo $emailContent;
}
但我想更改此代码以获取收入中的评论数量。
这是元标记,我只是提取注释的数量:
<meta item="desc" content="Comments:645">
我清楚了,你了解我吗?
如果我不够明确,请问我?
感谢您的帮助
答案 0 :(得分:1)
有两种方法可以做到这一点。您可以使用本机PHP函数:get_meta_tags()
,如下所示:
$tags = get_meta_tags('http://yoursite.com');
$comments = $tags['desc'];
或者您可以使用RegEx,但上述内容会更实用。
答案 1 :(得分:1)
您正在寻找的可能是屏幕抓取。
这是一个编程语言如php,python或ruby在内存中加载网站并使用各种选择器从中获取内容的过程。 屏幕抓取主要用于具有大量有趣数据但没有json或xml API的网站
我正在搜索它,我偶然发现了这篇文章: PHP equivalent of PyQuery or Nokogiri?本文详细介绍了有关网页抓取的内容: http://en.wikipedia.org/wiki/Web_scraping
答案 2 :(得分:0)
寻找使用domDocument
$dom = new domDocument;
$dom->loadHTML($htmlPage);
$metas = $dom->documentElement->getElementsByTagName('meta');
$ar = array();
foreach ($metas as $meta) {
$name = $meta->getAttribute('name');
$value = $meta->getAttribute('content');
$ar[$name] = $value;
}
print_r($ar); // print array meta-values