php实现实时获取当天天气小工具
//获取天气预报网站的网页内容
$html = file_get_contents("http://www.weather.com.cn/weather1d/101210101.shtml");
//正则表达式
$reg = ‘#hour3data.+?\[".+?,.+?,(?<tianqi>.+?),(?<wendu>.+?),#‘;
//如果匹配成功,就输出温度相关的信息
if(preg_match($reg, $html, $mat)){
echo "今天".$mat[‘tianqi‘].",温度".$mat[‘wendu‘];
}
根据qq号获取昵称和头像
$url = "http://r.pengyou.com/fcg-bin/cgi_get_portrait.fcg?uins=1579715173";
$html = file_get_contents($url);
$reg = ‘#.+?\["(.+?)",.+?,.+?,.+?,.+?,.+?,"(.+?)"#‘;
if(preg_match($reg, $html, $mat)){
//由于防盗链,无法直接使用腾讯的头像链接,所以要先下载到本地
file_put_contents("1.jpg",file_get_contents($mat[1]));
echo "<img src=‘./1.jpg‘ />".$mat[2];
}
根据ip获取地址信息
$ip = "14.215.177.38";
$html = file_get_contents("http://ip.chinaz.com/".$ip);
$regex = ‘#<p class="WhwtdWrap bor-b1s col-gray03">[\s\S]+?<span class="Whwtdhalf w50-0">(.+?)</span>[\s\S]+?</p>#‘;
if(preg_match($regex, $html, $mat)){
echo $mat[1];
}
从起点采集一本指定的小说所有的章节内容,合并到一个txt文件
$html = file_get_contents("http://book.qidian.com/info/1004608738");
$regex = ‘#<li data-rid="\d+?"><a href="(.+?)"[\s\S]+?>(.+?)</a>[\s\S]+?</li>#‘;
if(preg_match_all($regex, $html, $mats)){
foreach($mats[1] as $k => $v){
$html1 = file_get_contents("http:".$v);
$regex1 = ‘#<div class="read-content j_readContent">([\s\S]+?)</div>#‘;
//匹配内容
if(preg_match($regex1, $html1, $mat)){
$mat[1] = preg_replace(‘#<.+?>|\s+?#‘, "",$mat[1]);
$content = "\r\n".$mats[2][$k]."\r\n".$mat[1];
file_put_contents("1.txt", $content, FILE_APPEND);
} else {
echo "内容没有匹配成功";
}
echo $mats[2][$k]."\n";
}
}