26号中午我在论坛发贴询问php如何获取网站首页外部链接和链接文字?,并说希望可以付费找人帮写,当时田心杨短消息联系我了,我将更详细要求发给了田,田答应¥50元帮我写这个文件,以下就是我发给田的功能需求文件:,当晚田写好后,我远程大概看了下,觉得应该没什么问题(远程看效果,其实只能看出大致的),就付了钱拿到了源码,但经过自己测试后,发觉存在不少问题,向田提出来,田也修改,修改过程中我提供了一些资料,比如获取网站描述的函数get_meta_tags,田自己也承认并不知道有这个函数,还有抓取链接匹配度较高的正则表达式,但修改的效果并不好,主要是抓取链接的正则匹配度不高,匹配成功的网站比例太少了,田以网站html代码不标准为由,说只匹配正规标准的html,晕,这理由可以说得过去,但田的代码只抓取有引号的链接,其他的都抓取不了,并且部分标准正规的html一样抓取有问题,最重要的是图片链接提取出来之后,链接文字全搞错了,这个功能需要田做不出来,这已经不是匹配度的问题,是文件根本就有错误了,我向田提出退款要求,田无意退钱,并让我上论坛找各位评评理!!我贴出代码,大家来看看吧,这代码在带有图片链接的网站抓取,链接文字全都搞错了,比如以下网站:
http://127.0.0.1/?url=www.historyfamily.cn
http://127.0.0.1/?url=www.baijiajiangtan.com.cn
http://127.0.0.1/?url=www.26262.net
我深深地知道,这类爬虫的功能不可能百分百完整,但田的文件在有图片链接的网站抓取完全搞错了,链接文字匹配得乱七八糟,实际上并没有按要求完成功能,各位测试可顺着抓取到的链接地址测试多一些网站地址就会发现,这个文件的源码存在很多问题,我要求抓取链接的正则匹配以下几种链接格式并不过份吧:
<a href="http://www.baidu.com">Baidu</a>
<a href='http://www.baidu.com'>Baidu</a>
<a href=http://www.baidu.com>Baidu</a>
<a class="123" title="abc" href="http://www.baidu.com">Baidu</a>
<a href= "http://www.baidu.com">Baidu</a>
<a href= http://www.baidu.com >Baidu</a>对于这件事,我不会没品到破口大骂,50元也不多,毕竟田也的确是花了时间去写文件的,我没有质疑田的php能力,只是其可能在抓取这个方面不熟悉,我只是感到郁闷!以下是田最终给的文件源码:
<?php
header('Content-Type:text/html;charset=utf-8');
error_reporting(0);
$url=$_GET['url']; //获得 url 地址$url_prefix='http://127.0.0.1/'; //配置,填上 url的前缀,填写 ? 前的所有
if($url): //存在 url 则执行操作
$content=file_get_contents('http://'.$url);
$domain=get_domain($url); //获得 URL 的域名
$charset=get_charset($content); //获得网页的字符集
$title=iconv($charset,'UTF-8',get_title($content)); //获得标题
$description=iconv($charset,'UTF-8',get_description($url)); //获得网页描述
$arr=match_links($content); //获得 url,数组
$arr=check_url($arr); //搜索 url 文字是否图片
$arr=fiter_url($arr,$url); //过滤 url $arr_url=$arr[0];
$arr_content=$arr[1];
?>
<br/>
<p><b>网站编码:</b><?php echo $charset;?></p>
<p><b>网站地址:</b><?php echo $url;?></p>
<p><b>网站标题:</b><?php echo $title;?></p>
<p><b>网站描述:</b><?php echo $description.$url;?></p>
<p><b>网站标题:</b><?php echo $title;?></p>
<p><b>友情连接:</b></p>
<?php
foreach($arr_url as $key=>$url) {
echo "<a href='$url_prefix?url=$url' target='_blank'>".iconv($charset,'utf-8',$arr_content[$key]).'</a><br/>'; }
?><?php
else :
?>//******************************** 这里是预留位置
URL不存在!!
//******************************** 可以填写任可东西,包括 HTML
<?php
endif;
//******************************************************* 以下为 函数//提取url中的域名部份
function get_domain($url){
$pattern = "/[\w-]+\.(com|net|org|gov|cc|biz|info|cn)(\.(cn|hk))*/"; //提取域名正则
preg_match($pattern, $url, $matches); //提取域名
if(count($matches) > 0) {
return $matches[0];
}
else{
$rs = parse_url($url);
$main_url = $rs["host"];
if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) {
return $main_url;
}
else{
$arr = explode(".",$main_url);
$count=count($arr);
$endArr = array("com","net","org","3322");//com.cn net.cn 等情况
if (in_array($arr[$count-2],$endArr)){
$domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
}
else{
$domain = $arr[$count-2].".".$arr[$count-1];
}
return $domain;
}
}
}//获取网页的 title 部份
function get_title($content) {
preg_match_all('/<title>([\d\D]*)<\/title>/i',$content,$match);
return $match[1][0];
}
//获得网页的 charset 字符集
function get_charset($content) {
preg_match_all('/<meta.*charset=([^\/>\'"]*)[\'"\/]?[^>]*[\/]?>/i', $content, $match);
return $match[1][0];
}function get_description($url) {
$meta=get_meta_tags('http://'.$url);
$des=$meta['description'];
if($des)
return $des;
else {
$content=file_get_contents('http://'.$url);
preg_match_all('/<body[^>]*>([\d\D])*<\/body>/i',$content,$match);
$return=delScript($match[0][0]);
$return=strip_tags($return);
return substr($return,0,300);
}
}
//检查 url 文字是否为 图片,有则改为图片alt
function check_url($arr) {
$arr_url=$arr[0];
$arr_content=$arr[1];
foreach($arr_content as $key=>$value) { //循环搜索 连接文字是否为 img 标签
if(stristr($value, '<img')) { //存在 img 标签,则操作
if(get_img_alt($value)) { //图片存在有 alt 内容
$arr_content[$key]=get_img_alt($value);
}
else { //不存在
$arr_content[$key]=$arr_url[$key];
}
}
$arr_content[$key]=strip_tags($arr_content[$key]);
}
return array($arr_url,$arr_content);
}//获得 <img 标签里的 alt 内容
//存在 alt 则返回 alt 内容,不存在返回 false
function get_img_alt($string) {
preg_match_all('/<img[^>]*alt=[\'"]?([^\'"]*)[\'"]?[^>]*[\/]?>/i',$string,$match);
if($match[1][0])
return $match[1][0];
else
return false;
}
//去除 script 脚 本
function delScript($string){
$pregfind = array("/<script.*>.*<\/script>/siU",'/on(mousewheel|mouseover|click|load|onload|submit|focus|blur)="[^"]*"/i');
$pregreplace = array('','');
$string = preg_replace($pregfind, $pregreplace, $string);
return $string;
}//获取 url
function match_links($document) {
preg_match_all("'<\s*a\s.*?href\s*=\s*([\"\'])?(?(1)(.*?)\\1|([^\s\>]+))[^>]*>?(.*?)</a>'isx",$document,$links);
while(list($key,$val) = each($links[2])) {
if(!empty($val))
$match[] = $val;
}
while(list($key,$val) = each($links[3])) {
if(!empty($val))
$match[] = $val;
}
return array($match,$links[4]);
}
//对url进行过滤
function fiter_url($arr,$domain) {
$arr_url=$arr[0]; //url
$arr_content=$arr[1]; //url content foreach($arr_url as $key=>$url) { //url截取主机
if(!stristr($url,'http')) {
unset($arr_url[$key]);
unset($arr_content[$key]);
}
$arr_tmp=parse_url($url);
$arr_url[$key]=$arr_tmp['host'];
}
$tmp_url=array();
$tmp_content=array();
foreach($arr_url as $key=>$url) {
if(!in_array($url,$tmp_url) and strlen($url)>0 and $domain!=$url) {
$tmp_url[]=$url;
$tmp_content[]=strip_tags($arr_content[$key]);
}
}
return array($tmp_url,$tmp_content);
}
//***************************************************************** END OF FILE
?>
http://127.0.0.1/?url=www.historyfamily.cn
http://127.0.0.1/?url=www.baijiajiangtan.com.cn
http://127.0.0.1/?url=www.26262.net
我深深地知道,这类爬虫的功能不可能百分百完整,但田的文件在有图片链接的网站抓取完全搞错了,链接文字匹配得乱七八糟,实际上并没有按要求完成功能,各位测试可顺着抓取到的链接地址测试多一些网站地址就会发现,这个文件的源码存在很多问题,我要求抓取链接的正则匹配以下几种链接格式并不过份吧:
<a href="http://www.baidu.com">Baidu</a>
<a href='http://www.baidu.com'>Baidu</a>
<a href=http://www.baidu.com>Baidu</a>
<a class="123" title="abc" href="http://www.baidu.com">Baidu</a>
<a href= "http://www.baidu.com">Baidu</a>
<a href= http://www.baidu.com >Baidu</a>对于这件事,我不会没品到破口大骂,50元也不多,毕竟田也的确是花了时间去写文件的,我没有质疑田的php能力,只是其可能在抓取这个方面不熟悉,我只是感到郁闷!以下是田最终给的文件源码:
<?php
header('Content-Type:text/html;charset=utf-8');
error_reporting(0);
$url=$_GET['url']; //获得 url 地址$url_prefix='http://127.0.0.1/'; //配置,填上 url的前缀,填写 ? 前的所有
if($url): //存在 url 则执行操作
$content=file_get_contents('http://'.$url);
$domain=get_domain($url); //获得 URL 的域名
$charset=get_charset($content); //获得网页的字符集
$title=iconv($charset,'UTF-8',get_title($content)); //获得标题
$description=iconv($charset,'UTF-8',get_description($url)); //获得网页描述
$arr=match_links($content); //获得 url,数组
$arr=check_url($arr); //搜索 url 文字是否图片
$arr=fiter_url($arr,$url); //过滤 url $arr_url=$arr[0];
$arr_content=$arr[1];
?>
<br/>
<p><b>网站编码:</b><?php echo $charset;?></p>
<p><b>网站地址:</b><?php echo $url;?></p>
<p><b>网站标题:</b><?php echo $title;?></p>
<p><b>网站描述:</b><?php echo $description.$url;?></p>
<p><b>网站标题:</b><?php echo $title;?></p>
<p><b>友情连接:</b></p>
<?php
foreach($arr_url as $key=>$url) {
echo "<a href='$url_prefix?url=$url' target='_blank'>".iconv($charset,'utf-8',$arr_content[$key]).'</a><br/>'; }
?><?php
else :
?>//******************************** 这里是预留位置
URL不存在!!
//******************************** 可以填写任可东西,包括 HTML
<?php
endif;
//******************************************************* 以下为 函数//提取url中的域名部份
function get_domain($url){
$pattern = "/[\w-]+\.(com|net|org|gov|cc|biz|info|cn)(\.(cn|hk))*/"; //提取域名正则
preg_match($pattern, $url, $matches); //提取域名
if(count($matches) > 0) {
return $matches[0];
}
else{
$rs = parse_url($url);
$main_url = $rs["host"];
if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) {
return $main_url;
}
else{
$arr = explode(".",$main_url);
$count=count($arr);
$endArr = array("com","net","org","3322");//com.cn net.cn 等情况
if (in_array($arr[$count-2],$endArr)){
$domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
}
else{
$domain = $arr[$count-2].".".$arr[$count-1];
}
return $domain;
}
}
}//获取网页的 title 部份
function get_title($content) {
preg_match_all('/<title>([\d\D]*)<\/title>/i',$content,$match);
return $match[1][0];
}
//获得网页的 charset 字符集
function get_charset($content) {
preg_match_all('/<meta.*charset=([^\/>\'"]*)[\'"\/]?[^>]*[\/]?>/i', $content, $match);
return $match[1][0];
}function get_description($url) {
$meta=get_meta_tags('http://'.$url);
$des=$meta['description'];
if($des)
return $des;
else {
$content=file_get_contents('http://'.$url);
preg_match_all('/<body[^>]*>([\d\D])*<\/body>/i',$content,$match);
$return=delScript($match[0][0]);
$return=strip_tags($return);
return substr($return,0,300);
}
}
//检查 url 文字是否为 图片,有则改为图片alt
function check_url($arr) {
$arr_url=$arr[0];
$arr_content=$arr[1];
foreach($arr_content as $key=>$value) { //循环搜索 连接文字是否为 img 标签
if(stristr($value, '<img')) { //存在 img 标签,则操作
if(get_img_alt($value)) { //图片存在有 alt 内容
$arr_content[$key]=get_img_alt($value);
}
else { //不存在
$arr_content[$key]=$arr_url[$key];
}
}
$arr_content[$key]=strip_tags($arr_content[$key]);
}
return array($arr_url,$arr_content);
}//获得 <img 标签里的 alt 内容
//存在 alt 则返回 alt 内容,不存在返回 false
function get_img_alt($string) {
preg_match_all('/<img[^>]*alt=[\'"]?([^\'"]*)[\'"]?[^>]*[\/]?>/i',$string,$match);
if($match[1][0])
return $match[1][0];
else
return false;
}
//去除 script 脚 本
function delScript($string){
$pregfind = array("/<script.*>.*<\/script>/siU",'/on(mousewheel|mouseover|click|load|onload|submit|focus|blur)="[^"]*"/i');
$pregreplace = array('','');
$string = preg_replace($pregfind, $pregreplace, $string);
return $string;
}//获取 url
function match_links($document) {
preg_match_all("'<\s*a\s.*?href\s*=\s*([\"\'])?(?(1)(.*?)\\1|([^\s\>]+))[^>]*>?(.*?)</a>'isx",$document,$links);
while(list($key,$val) = each($links[2])) {
if(!empty($val))
$match[] = $val;
}
while(list($key,$val) = each($links[3])) {
if(!empty($val))
$match[] = $val;
}
return array($match,$links[4]);
}
//对url进行过滤
function fiter_url($arr,$domain) {
$arr_url=$arr[0]; //url
$arr_content=$arr[1]; //url content foreach($arr_url as $key=>$url) { //url截取主机
if(!stristr($url,'http')) {
unset($arr_url[$key]);
unset($arr_content[$key]);
}
$arr_tmp=parse_url($url);
$arr_url[$key]=$arr_tmp['host'];
}
$tmp_url=array();
$tmp_content=array();
foreach($arr_url as $key=>$url) {
if(!in_array($url,$tmp_url) and strlen($url)>0 and $domain!=$url) {
$tmp_url[]=$url;
$tmp_content[]=strip_tags($arr_content[$key]);
}
}
return array($tmp_url,$tmp_content);
}
//***************************************************************** END OF FILE
?>
解决方案 »
- 这段代码应该怎样用循环函数实现??
- Fatal error: Uncaught exception 'Exception' with message 'Connect failed: Access
- 生成zip文件,解压出现损坏错误
- 无奈,真的很无奈,if与while的差距就那么大吗
- 请教个mysql问题
- php生成条形码问题
- 请教一个PHP正则表达式的书写
- 静态页面里面怎么可以用角本的形式添加PHP页
- 请问PHP如何获取虚拟目录?
- redhat linux 8.0 为什么没有默认配置php的apache支持?
- PHP文件无法再IE运行
- 用move_uploaded_file上传文件时,能设定文件属性吗?
www.baijiajiangtan.com.cn
www.26262.net这三个网站的html哪里不规范了????还有其他更多的……