php網絡爬蟲,實現采集功能
阿新 • • 發佈:2018-02-07
col clas shtml read 起點 相關 www. 輸出 爬蟲
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"; } }
php網絡爬蟲,實現采集功能