我是一个全新的PHP开发新手,我想提取一个元标签的内容。
我有这段代码,它允许我提取# that元素的内容。
// 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;
}但我想要更改此代码,以获得income中的评论数量。
下面是meta标签,我将在其中提取评论的数量:
<meta item="desc" content="Comments:645">我说得够清楚了吗,你听懂了吗?
如果我说得不够清楚,可以问我?
感谢你的帮助
发布于 2013-11-29 17:31:55
有两种方法可以做到这一点。您可以使用原生PHP函数:get_meta_tags(),如下所示:
$tags = get_meta_tags('http://yoursite.com');
$comments = $tags['desc'];或者你可以使用RegEx,但是上面的方法会更实用。
发布于 2013-11-29 17:34:01
您正在寻找的可能是屏幕抓取。
这是像php、python或ruby这样的编程语言在内存中加载网站并使用各种选择器从其中获取内容的过程。屏幕抓取主要用于有大量有趣数据但没有json或xml的网站
在用谷歌搜索之后,我偶然发现了这篇文章:PHP equivalent of PyQuery or Nokogiri?
这篇文章解释了更多关于网络屏幕抓取的内容:http://en.wikipedia.org/wiki/Web_scraping
发布于 2013-11-29 17:30:47
查找使用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-valueshttps://stackoverflow.com/questions/20282239
复制相似问题