? ???? ????? PHP ? ??? ?? ???? ???
????? ???? ??? ? get ???? ???? ?? ???? ???? ? ??? ??? ?????.
?? ??? get??? ???? ?? ???? ??? ???? ? ????. URL? ???? ??? ????? ???? ??? ????? HTML ??? ??? ? ????. ??? ??? ????.
function get($url) { //初使化curl $ch = curl_init(); //請求的url,由形參傳入 curl_setopt($ch, CURLOPT_URL, $url); //將得到的數(shù)據(jù)返回 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不處理頭信息 curl_setopt($ch, CURLOPT_HEADER, 0); //連接超過10秒超時 curl_setopt($ch, CURLOPT_TIMEOUT, 10); //執(zhí)行curl $output = curl_exec($ch); //關(guān)閉資源 curl_close($ch); //返回內(nèi)容 return $output; }
?? NetEase?? ??? ???? ??? URL? ???? ?? ??? get ???? ?????.
?? get ???? ???? URL? ??? ? ????. ? URL? ???? ????? HTML? ?????.
???? ??? New Media Observation Network? ?? ?? ??????: http://www.xmtnews.com/events.
??? ?? ??:
1. ??? ??? html? ?????.
? ??? ?? HTML ??? ?????.
<section class="ov">
? ?? ??? ????.
<div class="hr-10"></div>
preg_match? ???? ???? ???? ?? ???? ?????. ??? ?? ??? HTML???. ???? HTML? $area ??? ?????.
? ???? ???? ??? ????.
<section class="ov">(.*?)<div class="hr-10"><\/div>/mis'
2. ??? ??
??? ?? URL? ??????. ??>??? ?? ??? <h3> ??? ??? ??????. preg_match_all? ???? ???? ?????.preg_match_all('/<h3><a href="(.*?)" title=".*?" class="headers" target="_blank">(.*?)<\/a><\/h3>/mis', $area, $find);url? ??? ???? ??? $find? ?? $find ??? ???? ???? ??? ? ? ????. ??? ?? ???? ? ?? ??? ? ?? URL? ?? ??? ?? ????. ?? ?? ??? ??? ????:
<?php $content = get('http://www.xmtnews.com/events'); preg_match('/<section class="ov">(.*?)<div class="hr-10"><\/div>/mis', $content, $match); //將正則匹配到的內(nèi)容賦值給$area $area = $match[1]; preg_match_all('/<h3><a href="(.*?)" title=".*?" class="headers" target="_blank">(.*?)<\/a><\/h3>/', $area, $find); var_dump($find); function get($url) { //初使化curl $ch = curl_init(); //請求的url,由形參傳入 curl_setopt($ch, CURLOPT_URL, $url); //將得到的數(shù)據(jù)返回 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不處理頭信息 curl_setopt($ch, CURLOPT_HEADER, 0); //連接超過10秒超時 curl_setopt($ch, CURLOPT_TIMEOUT, 10); //執(zhí)行curl $output = curl_exec($ch); //關(guān)閉資源 curl_close($ch); //返回內(nèi)容 return $output; } ?>