欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

php怎么抓取網(wǎng)頁數(shù)據(jù),php抓取網(wǎng)頁數(shù)據(jù)并存放于plist中

PHP 如何獲取到一個網(wǎng)頁的內(nèi)容

1.file_get_contents

創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設計、成都網(wǎng)站建設與策劃設計,清徐網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設十年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:清徐等地區(qū)。清徐做網(wǎng)站價格咨詢:18982081108

PHP代碼

復制代碼 代碼如下:

?php

$url = "";

$contents = file_get_contents($url);

//如果出現(xiàn)中文亂碼使用下面代碼

//$getcontent = iconv("gb2312", "utf-8",$contents);

echo $contents;

?

2.curl

PHP代碼

復制代碼 代碼如下:

?php

$url = "";

$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;

?

3.fopen-fread-fclose

PHP代碼

復制代碼 代碼如下:

?php

$handle = fopen ("", "rb");

$contents = "";

do {

$data = fread($handle, 1024);

if (strlen($data) == 0) {

break;

}

$contents .= $data;

} while(true);

fclose ($handle);

echo $contents;

?

注:

1.

使用file_get_contents和fopen必須空間開啟allow_url_fopen。方法:編輯php.ini,設置

allow_url_fopen = On,allow_url_fopen關閉時fopen和file_get_contents都不能打開遠程文件。

2.使用curl必須空間開啟curl。方法:windows下修改php.ini,將extension=php_curl.dll前面的分

號去掉,而且需要拷貝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安裝curl擴

展。

php獲取指定網(wǎng)頁內(nèi)容

一、用file_get_contents函數(shù),以post方式獲取url

?php

$url=?'';

$data=?array('foo'=?'bar');

$data= http_build_query($data);

$opts=?array(

'http'=?array(

'method'=?'POST',

'header'="Content-type: application/x-www-form-urlencoded\r\n" ?.

"Content-Length: " ?.?strlen($data) .?"\r\n",

'content'=?$data

)

);

$ctx= stream_context_create($opts);

$html= @file_get_contents($url,'',$ctx);

二、用file_get_contents以get方式獲取內(nèi)容

?php

$url='';

$html=?file_get_contents($url);

echo$html;

?

三、用fopen打開url, 以get方式獲取內(nèi)容

?php

$fp=?fopen($url,'r');

$header= stream_get_meta_data($fp);//獲取報頭信息

while(!feof($fp)) {

$result.=?fgets($fp, 1024);

}

echo"url header: {$header} br":

echo"url body: $result";

fclose($fp);

?

四、用fopen打開url, 以post方式獲取內(nèi)容

?php

$data=?array('foo2'=?'bar2','foo3'='bar3');

$data= http_build_query($data);

$opts=?array(

'http'=?array(

'method'=?'POST',

'header'="Content-type: application/x-www-form-

urlencoded\r\nCookie:cook1=c3;cook2=c4\r\n" ?.

"Content-Length: " ?.?strlen($data) .?"\r\n",

'content'=?$data

)

);

$context= stream_context_create($opts);

$html=?fopen(';id2=i4','rb',false,?$context);

$w=fread($html,1024);

echo$w;

?

五、使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經(jīng)打開了curl擴展

?php

$ch= curl_init();

$timeout= 5;

curl_setopt ($ch, CURLOPT_URL,?'');

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,?$timeout);

$file_contents= curl_exec($ch);

curl_close($ch);

echo$file_contents;

?

php獲取網(wǎng)頁源碼內(nèi)容有哪些辦法

可以參考以下幾種方法:

方法一: file_get_contents獲取

span style="white-space:pre"?/span$url="";

span style="white-space:pre"?/span$fh= file_get_contents

('');span style="white-space:pre"?/spanecho $fh;

方法二:使用fopen獲取網(wǎng)頁源代碼

span style="white-space:pre"?/span$url="";

span style="white-space:pre"?/span$handle = fopen ($url, "rb");

span style="white-space:pre"?/span$contents = "";

span style="white-space:pre"?/spanwhile (!feof($handle)) {

span style="white-space:pre"??/span$contents .= fread($handle, 8192);

span style="white-space:pre"?/span}

span style="white-space:pre"?/spanfclose($handle);

span style="white-space:pre"?/spanecho $contents; //輸出獲取到得內(nèi)容。

方法三:使用CURL獲取網(wǎng)頁源代碼

$url="";

$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

$curl = curl_init();?//創(chuàng)建一個新的CURL資源

curl_setopt($curl, CURLOPT_URL, $url);?//設置URL和相應的選項

curl_setopt($curl, CURLOPT_HEADER, 0);? //0表示不輸出Header,1表示輸出

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);?//設定是否顯示頭信息,1顯示,0不顯示。//如果成功只將結(jié)果返回,不自動輸出任何內(nèi)容。如果失敗返回FALSE

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($curl, CURLOPT_ENCODING, '');?//設置編碼格式,為空表示支持所有格式的編碼

//header中“Accept-Encoding: ”部分的內(nèi)容,支持的編碼格式為:"identity","deflate","gzip"。

curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

//設置這個選項為一個非零值(象 “Location: “)的頭,服務器會把它當做HTTP頭的一部分發(fā)送(注意這是遞歸的,PHP將發(fā)送形如 “Location: “的頭)。

$data = curl_exec($curl);

echo $data;

//echo curl_errno($curl); //返回0時表示程序執(zhí)行成功

curl_close($curl);?//關閉cURL資源,并釋放系統(tǒng)資源

拓展資料

PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本預處理器”)是一種通用開源腳本語言。語法吸收了C語言、Java和Perl的特點,利于學習,使用廣泛,主要適用于Web開發(fā)領域。PHP 獨特的語法混合了C、Java、Perl以及PHP自創(chuàng)的語法。它可以比CGI或者Perl更快速地執(zhí)行動態(tài)網(wǎng)頁。

用PHP做出的動態(tài)頁面與其他的編程語言相比,PHP是將程序嵌入到HTML(標準通用標記語言下的一個應用)文檔中去執(zhí)行,執(zhí)行效率比完全生成HTML標記的CGI要高許多;PHP還可以執(zhí)行編譯后代碼,編譯可以達到加密和優(yōu)化代碼運行,使代碼運行更快。

參考資料:PHP(超文本預處理器)-百度百科

php抓取網(wǎng)頁源碼方法

可以使用file_get_content函數(shù)來獲取源代碼,你只需要把網(wǎng)站傳入這個函數(shù),獲取后是一個字符串,你需要格式化代碼就可以了

PHP獲取網(wǎng)頁內(nèi)容的幾種方法

簡單的收集下PHP下獲取網(wǎng)頁內(nèi)容的幾種方法:

用file_get_contents,以get方式獲取內(nèi)容。

用fopen打開url,以get方式獲取內(nèi)容。

使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經(jīng)打開了curl擴展。

用file_get_contents函數(shù),以post方式獲取url。

用fopen打開url,以post方式獲取內(nèi)容。

用fsockopen函數(shù)打開url,獲取完整的數(shù)據(jù),包括header和body。

php怎么抓取其它網(wǎng)站數(shù)據(jù)

可以用以下4個方法來抓取網(wǎng)站 的數(shù)據(jù):

1. 用 file_get_contents 以 get 方式獲取內(nèi)容:

?

$url = '';

$html = file_get_contents($url);

echo $html;

2. 用fopen打開url,以get方式獲取內(nèi)容

?

$url = '';

$fp = fopen($url, 'r');

stream_get_meta_data($fp);

$result = '';

while(!feof($fp))

{

$result .= fgets($fp, 1024);

}

echo "url body: $result";

fclose($fp);

3. 用file_get_contents函數(shù),以post方式獲取url

?

$data = array(

'foo'='bar',

'baz'='boom',

'site'='',

'name'='nowa magic');

$data = http_build_query($data);

//$postdata = http_build_query($data);

$options = array(

'http' = array(

'method' = 'POST',

'header' = 'Content-type:application/x-www-form-urlencoded',

'content' = $data

//'timeout' = 60 * 60 // 超時時間(單位:s)

)

);

$url = "";

$context = stream_context_create($options);

$result = file_get_contents($url, false, $context);

echo $result;

4、使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經(jīng)打開了curl擴展

$url = '';

$ch = curl_init();

$timeout = 5;

curl_setopt ($ch, CURLOPT_URL, $url);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents = curl_exec($ch);

curl_close($ch);

echo $file_contents;

當前題目:php怎么抓取網(wǎng)頁數(shù)據(jù),php抓取網(wǎng)頁數(shù)據(jù)并存放于plist中
文章網(wǎng)址:http://chinadenli.net/article26/dsepijg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供Google響應式網(wǎng)站服務器托管移動網(wǎng)站建設云服務器用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務器托管