PHP 抓取网页内容天气-IP对应地理位置等

来源:互联网 发布:羽毛球训练软件 编辑:程序博客网 时间:2024/04/30 09:28
作者:杨勇,思远,2012-12-27

对一些字符比较多的站点进行采集时,用到了preg_match、preg_match_all,但是发现正则会失效。

有两种修改方法:

1、ini_set('pcre.backtrack_limit', 1000000); //默认的只有100000

2、修改 php.ini 的pcre.backtrack_limit参数,使之支持更大的字符串。

==============
//抓取淘宝页中的服饰一栏:

ini_set('pcre.backtrack_limit', 1000000); //PHP.INI,默认的只有100000

$html=file_get_contents("http://www.taobao.com/"); 
//echo $html;
$parLeft=preg_quote('<div class="category-item cat-clothes">','/');//开始
$parRight=preg_quote('<!--end-->    </div>','/');#结束
$par='/'.$parLeft.'(.*)'.$parRight.'/isU';  //取出中间(.*)的内容

$ArrAdd=array();
preg_match_all($par,$html,$ArrAdd); 
print_r($ArrAdd);

==============================

//抓取 天气:
ini_set('pcre.backtrack_limit', 1000000); //PHP.INI,默认的只有100000

$html=file_get_contents("http://www.weather.com.cn/weather/101200101.shtml"); 
//echo $html;
$parLeft=preg_quote('<div class="jxyb" id="weather6h">','/');//开始
$parRight=preg_quote('</div>','/');#结束
$par='/'.$parLeft.'(.*)'.$parRight.'/isU';  //取出中间(.*)的内容

$ArrAdd=array();
preg_match_all($par,$html,$ArrAdd); 
print_r($ArrAdd);

=================================
//抓取IP地址对应的地理位置:

$ip = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"]; 
$ip = ($ip) ? $ip : $_SERVER["REMOTE_ADDR"]; 
$ip='122.82.231.128';
 //用百度查询IP,试过很多IP查询方式,感觉只有百度的准确一些 
$html=file_get_contents('http://www.baidu.com/s?wd='.$ip); 
//echo $html;

$parleft=preg_quote('>来&nbsp;&nbsp;&nbsp;自:&nbsp;<strong>','/');
$parright=preg_quote('</strong></p><p class="op_ip_search">','/');
$par='/'.$parleft.'(.*)'.$parright.'/i';  //取出中间(.*)的内容
$ArrAdd=array();
preg_match_all($par,$html,$ArrAdd); 
//print_r($ArrAdd);
 echo 'IP对应地址:'.$ArrAdd[1][0].'<br>';