例子,php百度天气小偷程序。
复制代码代码如下:
<?php
//获取在百度里面搜索 xx(地名)天气 抓取的天气信息
$url = "http://www.baidu.com/s?wd=%B1%B1%BE%A9%CC%EC%C6%F8"; //此为在百度里搜 北京天气 的返回地址
$content = file_get_contents($url);
preg_match_all('/<div id="weat">(.+?)<\/table>/si',$content,$temp);
preg_match_all('/<td>(.+?)<\/td>/si',$temp[1][0],$temp1);
var_dump ($temp1[1][0]);
输出信息:
string(243) "北京天气
今天:温度22℃ - 24℃。白天:多云, 大阵雨,夜间:多云。东风2级转1-2级
明天:温度23℃ - 28℃。白天:多云,夜间:多云。东南风2级"
index.php
<?php
$con = file_get_contents("http://it.sohu.com/s2010/5651/s274087241/index.shtml");
$preg="/·<a href=(.*) target=_blank>(.*)<\/a>/U";
preg_match_all($preg,$con,$arr);
foreach($arr[1] as $id=>$v){
echo "<a href=view.php?url=$v>".$arr[2][$id]."</a><br>";
}
?>
view.php
<?php
$con = file_get_contents($_GET[url]);
$preg="/<h1>(.*)<\/h1>/";
preg_match($preg,$con,$arr);
echo "<h1>".$arr[1]."</h1>";
echo "<hr>";
$preg2="/<div class=\"text clear\" id=\"contentText\" collection=\"Y\">(.*)<\/div>/s";
preg_match($preg2,$con,$arr2);
echo $arr2[1];
?>