我用simple html dom获取某网页的所有图片,但是想去除所有的广告。
广告图片基本都储存与广告商的服务器,所有域名不同。于是我的思路是增加一个图片域名路径的判断,来过滤广告。下面是我获取图片的代码,可以正常获取所有的图片。
<?php
header('Content-type:text/html; charset=utf-8');
require_once 'simple_html_dom.php';
$v = 'http://www.jqueryimage.com/';
$html = file_get_html($v);
foreach($html->find('img') as $element) {
if( preg_match('#^http:\/\/(.*)\.(jpg|gif|png)$#i',$element->src)){
$image = $element->src;
echo $image.'<hr />';
}
}
?>这是echo出的前几条记录
http://www.jqueryimage.com/images/demo/jquery-xfade.gif
--------------------------------------------------------------------------------
http://www.jqueryimage.com/images/buttons/homepage.jpg
--------------------------------------------------------------------------------
http://www.jqueryimage.com/images/buttons/demo.jpg
--------------------------------------------------------------------------------
http://www.jqueryimage.com/images/buttons/download.jpg
--------------------------------------------------------------------------------
http://img.adv.dadapro.net/myshare/shared/buttons/share001btn.gif //广告图片,域名不同。
--------------------------------------------------------------------------------
http://www.jqueryimage.com/images/demo/zClip.jpg下面是域名截取的代码,可以获取网页的一级域名。
<?php
preg_match_all('#https?://(.*?)($|/)#m', urldecode(stripcslashes($v)), $r));
echo $r[1][0];
?>如何结合2者,过滤所有的广告图片,留下正确的图片呢?求教。
广告图片基本都储存与广告商的服务器,所有域名不同。于是我的思路是增加一个图片域名路径的判断,来过滤广告。下面是我获取图片的代码,可以正常获取所有的图片。
<?php
header('Content-type:text/html; charset=utf-8');
require_once 'simple_html_dom.php';
$v = 'http://www.jqueryimage.com/';
$html = file_get_html($v);
foreach($html->find('img') as $element) {
if( preg_match('#^http:\/\/(.*)\.(jpg|gif|png)$#i',$element->src)){
$image = $element->src;
echo $image.'<hr />';
}
}
?>这是echo出的前几条记录
http://www.jqueryimage.com/images/demo/jquery-xfade.gif
--------------------------------------------------------------------------------
http://www.jqueryimage.com/images/buttons/homepage.jpg
--------------------------------------------------------------------------------
http://www.jqueryimage.com/images/buttons/demo.jpg
--------------------------------------------------------------------------------
http://www.jqueryimage.com/images/buttons/download.jpg
--------------------------------------------------------------------------------
http://img.adv.dadapro.net/myshare/shared/buttons/share001btn.gif //广告图片,域名不同。
--------------------------------------------------------------------------------
http://www.jqueryimage.com/images/demo/zClip.jpg下面是域名截取的代码,可以获取网页的一级域名。
<?php
preg_match_all('#https?://(.*?)($|/)#m', urldecode(stripcslashes($v)), $r));
echo $r[1][0];
?>如何结合2者,过滤所有的广告图片,留下正确的图片呢?求教。
header('Content-type:text/html; charset=utf-8');
require_once 'simple_html_dom.php';
$v = 'http://www.jqueryimage.com/';
$html = file_get_html($v);
foreach($html->find('img') as $element) {
if( preg_match('#^http:\/\/(.*)\.(jpg|gif|png)$#i',$element->src)){ $urls = parse_url($element->src);
if($urls['host'] == 'www.jqueryimage.com') {
echo "图片:" . $element->src;
} else {
echo "广告图片:" . $element->src;
}
}
}
?>
firefox有个插件叫imagehost grabber,里面有自写抓图规则的功能,你可以参考一下
xpath/, imagehost grabber,马上搜索一下去。谢谢了。