當前位置:首頁 » 編程語言 » php網頁內容抓取

php網頁內容抓取

發布時間: 2022-05-31 00:36:16

1. php 如何獲取到一個網頁的內容

1.file_get_contents
PHP代碼

復制代碼 代碼如下:

<?php
$url = "http://www.jb51.net";
$contents = file_get_contents($url);
//如果出現中文亂碼使用下面代碼
//$getcontent = iconv("gb2312", "utf-8",$contents);
echo $contents;
?>

2.curl
PHP代碼

復制代碼 代碼如下:

<?php
$url = "http://www.jb51.net";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//在需要用戶檢測的網頁里需要增加下面兩行
//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 ("http://www.jb51.net", "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擴
展。

2. php獲取網頁源碼內容有哪些辦法

1、使用file_get_contents獲得網頁源代碼。這個方法最常用,只需要兩行代碼即可,非常簡單方便。

2、使用fopen獲得網頁源代碼。這個方法用的人也不少,不過代碼有點多。

3、使用curl獲得網頁源代碼。使用curl獲得網頁源代碼的做法,往往是需要更高要求的人使用,例如當你需要在抓取網頁內容的同時,得到網頁header信息,還有ENCODING編碼的使,USERAGENT的使用等等。

3. php獲取網頁源碼內容有哪些辦法

可以參考以下幾種方法:

方法一: file_get_contents獲取

<span style="white-space:pre"></span>$url="http://www..com/";

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

('http://www.hxfzzx.com/news/fzfj/');<span style="white-space:pre"></span>echo $fh;

拓展資料

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

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

4. PHP 抓取網頁內容的幾個函數

獲取所有內容url保存到文件function get_index($save_file, $prefix="index_"){
$count = 68;
$i = 1; if (file_exists($save_file)) @unlink($save_file);
$fp = fopen($save_file, "a+") or die("Open ". $save_file ." failed"); while($i<$count){
$url = $prefix . $i .".htm"; echo "Get ". $url ."...";
$url_str = get_content_url(get_url($url)); echo " OKn"; fwrite($fp, $url_str);
++$i;
} fclose($fp);
}

5. PHP怎樣抓取網頁代碼中動態顯示的數據

你是想抓別人網頁上ajax動態載入的數據吧?

1、要找到它的ajax載入的URL地址

2、利用PHP的file_get_contents($url)函數讀取那個url地址。

3、對抓取到的內容進行分析或正則過濾。

6. php抓取網頁指定的內容

我給你一個思路, 代碼我也不會給的, 會被網路刪的.

抓取網上的數據, 一般用正則去匹配. 你可以匹配開頭為<div class="so_weather">的, 然後匹配結尾. 結尾盡量是這個開頭div的下一個同級div, 如<div id="asda">, 這樣. 然後得到的數據用strip_tags函數將html代碼都去了, 得到的結果就是你想要的

7. php獲得網頁源代碼抓取網頁內容的幾種方法

最簡單的是用 file(URL); 函數,把整個網頁讀入到一個數組中。還可以用 file_get_contents(URL); 函數,把整個網頁讀成一個字元串。

8. php獲取指定網頁內容

一、用file_get_contents函數,以post方式獲取url

<?php

$url='http://www.domain.com/test.php?id=123';

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

$data= http_build_query($data);

$opts=array(

'http'=>array(

'method'=>'POST',

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

"Content-Length: " .strlen($data) ." ",

'content'=>$data

)

);

$ctx= stream_context_create($opts);

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

二、用file_get_contents以get方式獲取內容

<?php

$url='http://www.domain.com/?para=123';

$html=file_get_contents($url);

echo$html;

?>

三、用fopen打開url, 以get方式獲取內容

<?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方式獲取內容

<?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 Cookie:cook1=c3;cook2=c4 " .

"Content-Length: " .strlen($data) ." ",

'content'=>$data

)

);

$context= stream_context_create($opts);

$html=fopen('http://www.test.com/zzzz.php?id=i3&id2=i4','rb',false,$context);

$w=fread($html,1024);

echo$w;

?>

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

<?php

$ch= curl_init();

$timeout= 5;

curl_setopt ($ch, CURLOPT_URL,'http://www.domain.com/');

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,$timeout);

$file_contents= curl_exec($ch);

curl_close($ch);

echo$file_contents;

?>

熱點內容
雲伺服器寬頻單位 發布:2025-02-11 20:48:11 瀏覽:538
安卓數據線公頭是哪個 發布:2025-02-11 20:45:42 瀏覽:812
網址原始密碼是什麼 發布:2025-02-11 20:33:52 瀏覽:72
怎麼創建伺服器我的世界網易 發布:2025-02-11 20:18:36 瀏覽:467
伺服器電腦與客戶端的連接 發布:2025-02-11 20:18:32 瀏覽:36
安卓哪個系統最流暢好用 發布:2025-02-11 20:17:44 瀏覽:879
蘋果平板安卓模擬器哪個好用 發布:2025-02-11 20:17:01 瀏覽:834
手機谷歌伺服器怎麼樣 發布:2025-02-11 20:08:37 瀏覽:221
編譯簡單游戲 發布:2025-02-11 20:02:10 瀏覽:866
php測評系統 發布:2025-02-11 19:42:58 瀏覽:294