在批量的數(shù)據(jù)采集在php中很少會(huì)使用file_get_contents函數(shù)來操作,但是如果是小量的我們可以使用file_get_contents函數(shù)操作,因?yàn)樗坏糜枚液唵我讓W(xué),下面我來介紹file_get_contents用法與使用過程中的問題解決辦法。
先來看問題,file_get_contents不能獲取帶端口的網(wǎng)址,例如如下代碼:
file_get_contents('http://localhost:12345');
沒有任何獲取,解決方法是:關(guān)閉selinux
1 永久方法 – 需要重啟服務(wù)器
修改/etc/selinux/config文件中設(shè)置SELINUX=disabled ,然后重啟服務(wù)器。
2 臨時(shí)方法 – 設(shè)置系統(tǒng)參數(shù)
使用命令setenforce 0附:
setenforce 1 設(shè)置SELinux 成為enforcing模式
setenforce 0 設(shè)置SELinux 成為permissive模式
file_get_contents超時(shí),代碼如下:
- function _file_get_contents($url)
- {
- $context = stream_context_create(array(
- 'http' => array(
- 'timeout' => 180 //超時(shí)時(shí)間,單位為秒
- )
- ));
- return @file_get_contents($url, 0, $context);
- }
好了上面的問題得到解決之后我們可以開始采集了,代碼如下:
- <?php
- //全國,判斷條件是$REQUEST_URI是否含有html
- if (!strpos($_SERVER["REQUEST_URI"],".html"))
- {
- $page="http://qq.ip138.com/weather/";
- $html = file_get_contents($page,'r');
- $pattern="/<B>全國主要城市、縣當(dāng)天和未來五天天氣趨勢預(yù)報(bào)在線查詢</B>(.*?)<center style="padding:3px">/si";
- //正則匹配之間的html
- preg_match($pattern,$html,$pg);
- echo "";
- //正則替換遠(yuǎn)程地址為本地地址
- $p=preg_replace('//weather/(w+)/index.htm/', 'tq.php/$1.html', $pg[1]);
- echo $p;
- }
- //省,判斷條件是$REQUEST_URI是否含有?
- else if(!strpos($_SERVER["REQUEST_URI"],"?")){
- //yoyo推薦的使用分割獲得數(shù)據(jù),這里是獲得省份名稱
- $province=explode("/",$_SERVER["REQUEST_URI"]);
- $province=explode(".",$province[count($province)-1]);
- $province=$province[0];
- //被注釋掉的是我自己寫出來的正則,感覺寫的不好,但效果等同上面
- //preg_match('/[^/]+[.(html)]$/',$_SERVER["REQUEST_URI"],$pro);
- //$province=preg_replace('/.html/','',$pro[0]);
- $page="http://qq.ip138.com/weather/".$province."/index.htm";
- //獲取html數(shù)據(jù)之前先嘗試打開頁面,防止惡意輸入地址導(dǎo)致出錯(cuò)
- if (!@fopen($page, "r")) {
- die("對(duì)不起,該地址不存在!<a href=javascript:history.back(1)>點(diǎn)擊這里返回</a>");
- exit(0);
- }
- $html = file_get_contents($page,'r');
- $pattern="/五天天氣趨勢預(yù)報(bào)</B>(.*?)請(qǐng)輸入輸入市/si";
- preg_match($pattern,$html,$pg);
- echo "";
- //正則替換,獲取省份,城市
- $p=preg_replace('//weather/(w+)/(w+).htm/', '$2.html?pro=$1', $pg[1]);
- echo $p;
- }
- else {
- //市,通過get傳遞省份
- $pro=$_REQUEST['pro'];
- $city=explode("/",$_SERVER["REQUEST_URI"]);
- $city=explode(".",$city[count($city)-1]);
- $city=$city[0];
- //preg_match('/[^/]+[.(html)]+[?]/',$_SERVER["REQUEST_URI"],$cit);
- //$city=preg_replace('/.html?/','',$cit[0]);
- $page="http://qq.ip138.com/weather/".$pro."/".$city.".htm";
- if (!@fopen($page, "r")) {
- die("對(duì)不起,該地址不存在!<a href=javascript:history.back(1)>點(diǎn)擊這里返回</a>");
- exit(0);
- }
- $html = file_get_contents($page,'r');
- $pattern="/五天天氣趨勢預(yù)報(bào)</B>(.*?)請(qǐng)輸入輸入市/si";
- preg_match($pattern,$html,$pg);
- echo "";
- //獲取真實(shí)的圖片地址
- $p=preg_replace('//image//', 'http://qq.ip138.com/image/', $pg[1]);
- echo $p;
- }
- ?>
如果上面辦法無法采集到數(shù)據(jù)我們可以使用以下代碼來處理,代碼如下:
- <?php
- $url = "http://www.companysz.com";
- $ch = curl_init();
- $timeout = 5;
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- //在需要用戶檢測的網(wǎng)頁里需要增加下面兩行
- //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- //curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);
- $contents = curl_exec($ch);
- curl_close($ch);
- echo $contents;
- ?>
新聞熱點(diǎn)
疑難解答