Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 刮取页面关键字、描述和标题的功能?_Php_Regex_Web Scraping - Fatal编程技术网

Php 刮取页面关键字、描述和标题的功能?

Php 刮取页面关键字、描述和标题的功能?,php,regex,web-scraping,Php,Regex,Web Scraping,我编写了简单的3个函数来刮取简单html页面的标题、描述和关键字 这是第一个刮取标题的函数 function getPageTitle ($url) { $content = $url; if (eregi("<title>(.*)</title>", $content, $array)) { $title = $array[1]; return $title; } } 函数getPageTit

我编写了简单的3个函数来刮取简单html页面的标题、描述和关键字 这是第一个刮取标题的函数

function getPageTitle ($url)
{
    $content = $url;
    if (eregi("<title>(.*)</title>", $content, $array)) {
        $title = $array[1];
        return $title;
    }
}
函数getPageTitle($url) { $content=$url; if(eregi((.*),$content,$array)){ $title=$array[1]; 返回$title; } } 而且效果很好 这是两个功能,用于刮取描述和关键字,以及那些不起作用的功能

function getPageKeywords($url)
{
    $content = $url; 
    if ( preg_match('/<meta[\s]+[^>]*?name[\s]?=[\s\"\']+keywords[\s\"\']+content[\s]?=[\s\"\']+(.*?)[\"\']+.*?>/i', $content, $array)) { 
        $keywords = $array[1];  
        return $keywords; 
    }  
}
function getPageDesc($url)
{
    $content = $url; 
    if ( preg_match('/<meta[\s]+[^>]*?name[\s]?=[\s\"\']+description[\s\"\']+content[\s]?=[\s\"\']+(.*?)[\"\']+.*?>/i', $content, $array)) { 
        $desc = $array[1];  
        return $desc; 
    }  
}
函数getPageKeywords($url)
{
$content=$url;
如果(preg\u match('/]*?name[\s]?=[\s\“\']+关键字[\s\“\']]+内容[\s]?=[\s\“\']+(.*?[\“\']+.>/i',$content,$array)){
$keywords=$array[1];
返回$keywords;
}  
}
函数getPageDesc($url)
{
$content=$url;
如果(preg\u match('/]*?name[\s]?=[\s\“\']+description[\s\“\']+content[\s]?=[\s\“\']+(.*?[\“\']+.>/i',$content,$array)){
$desc=$array[1];
返回$desc;
}  
}
我知道preg_赛线可能有问题,但我真的不知道
我尝试了很多东西,但都不起作用

最好是使用php的原生语言来解析HTML,而不是正则表达式,你也可以使用,尽管在今天的时代,很多网站甚至不再添加关键字、描述标签,所以你不能依赖它们一直存在。但以下是使用DOMDocument的方法:

<?php 
$source = file_get_contents('http://php.net');

$dom = new DOMDocument("1.0","UTF-8");
@$dom->loadHTML($source);
$dom->preserveWhiteSpace = false;

//Get Title
$title = $dom->getElementsByTagName('title')->item(0)->nodeValue;

$description = '';
$keywords = '';
foreach($dom->getElementsByTagName('meta') as $metas) {
    if($metas->getAttribute('name') =='description'){ $description = $metas->getAttribute('content'); }
    if($metas->getAttribute('name') =='keywords'){    $keywords = $metas->getAttribute('content');    }
}

print_r($title);
print_r($description);
print_r($keywords);
?> 

最好使用php的本机语言来解析HTML,而不是正则表达式,你也可以使用,尽管在今天的时代,很多网站甚至不再添加关键字和描述标签,所以你不能依赖它们总是在那里。但以下是使用DOMDocument的方法:

<?php 
$source = file_get_contents('http://php.net');

$dom = new DOMDocument("1.0","UTF-8");
@$dom->loadHTML($source);
$dom->preserveWhiteSpace = false;

//Get Title
$title = $dom->getElementsByTagName('title')->item(0)->nodeValue;

$description = '';
$keywords = '';
foreach($dom->getElementsByTagName('meta') as $metas) {
    if($metas->getAttribute('name') =='description'){ $description = $metas->getAttribute('content'); }
    if($metas->getAttribute('name') =='keywords'){    $keywords = $metas->getAttribute('content');    }
}

print_r($title);
print_r($description);
print_r($keywords);
?> 

为什么不使用get\u meta\u标记



注意您可以将参数更改为URL、本地文件或字符串。

为什么不使用get\u meta\u标记



注意您可以将参数更改为URL、本地文件或字符串。

只需注意:
eregi
不推荐使用。使用正则表达式解析HTML比使用简单的标记对更复杂;当您尝试开始解析标记属性时,需要切换到PHP Dom:问题是名称、描述和内容属性必须符合您匹配的顺序。第三个重要的一点,仅仅因为它位于网页上并不意味着您有权以任何方式使用数据(在没有权限的情况下。你试过了吗?这就像jQuery DOM解析一样。它来找你了……他饿了。只需注意:
eregi
不受欢迎。使用正则表达式解析HTML比简单的标记对更复杂;当你试着开始解析标记属性时,你需要切换到PHP DOM:问题是名称,说明和内容属性必须按照您匹配的顺序。第三个重要的一点,仅仅因为它位于网页上并不意味着您有权以任何方式使用数据(未经许可)。您尝试过吗?这就像jQuery DOM解析一样。它正朝着您走来……他渴望着。