導航:首頁 > 編程語言 > php提取網頁內容

php提取網頁內容

發布時間:2023-01-03 12:52:42

php抓取網頁指定內容

<?php
/*
* 如下: 方法有點笨
* 抓取網頁內容用 PHP 的正則
* 用JS每隔5分鍾刷新當前頁面---即重新獲取網頁內容
*
* 註: $mode中--<title></title>-更改為所需內容(如 $mode = "#<a(.*)</a>#";>獲取所有鏈接)
*
* window.location.href="http://localhost//refesh.php";中的http://localhost//refesh.php
* 更改為自己的URL----作用:即刷新當前頁面
*
* setInterval("ref()",300000);是每隔300000毫秒(即 5 * 60 *1000 毫秒即5分鍾)執行一次函數 ref()
*
* print_r($arr);輸出獲得的所有內容 $arr是一個數組 可根據所需輸出一部分(如 echo $arr[1][0];)
* 若要獲得所有內容 可去掉
* $mode = "#<title>(.*)</title>#";
if(preg_match_all($mode,$content,$arr)){
print_r($arr);
echo "<br/>";
echo $arr[1][0];
}
再加上 echo $content;
*/
$url = "http://www..com"; //目標站
$fp = @fopen($url, "r") or die("超時");

$content=file_get_contents($url);
$mode = "#<title>(.*)</title>#";
if(preg_match_all($mode,$content,$arr)){
//print_r($arr);
echo "<br/>";
echo $arr[1][0];
}
?>
<script language="javaScript" type="text/javascript">
<--
function ref(){
window.location.href="http://localhost//refesh.php";
}
setInterval("ref()",300000);
//-->
</script>

② 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擴
展。

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

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

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

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

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

④ php獲取指定網頁內容

此類方法一共有三種

  1. 第一種方法

<?php

$c = curl_init();

$url = 'www.badcatxt.com';

curl_setopt($c, CURLOPT_URL, $url);

curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($c);
curl_close($c);

$pos = strpos($data,'utf-8');

if($pos===false){$data = iconv("gbk","utf-8",$data);}

preg_match("/<title>(.*)</title>/i",$data, $title);

echo $title[1];

?>

第二種方法:使用file()函數

<?php

$lines_array = file('http://www.badcatxt.com/');

$lines_string = implode('', $lines_array);

$pos = strpos($lines_string,'utf-8');

if($pos===false){$lines_string = iconv("gbk","utf-8",$lines_string);}

eregi("<title>(.*)</title>", $lines_string, $title);

echo $title[1];

?>

第三種方法:使用file_get_contents

<?php

$content=file_get_contents("http://www.badcatxt.com/");

$pos = strpos($content,'utf-8');

if($pos===false){$content = iconv("gbk","utf-8",$content);}

$postb=strpos($content,'<title>')+7;

$poste=strpos($content,'</title>');

$length=$poste-$postb;

echo substr($content,$postb,$length);

?>

⑤ 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還可以執行編譯後代碼,編譯可以達到加密和優化代碼運行,使代碼運行更快。

⑥ php怎麼抓取其它網站數據

可以用以下4個方法來抓取網站 的數據:

1. 用 file_get_contents 以 get 方式獲取內容:
?

$url = 'http://localhost/test2.php';
$html = file_get_contents($url);
echo $html;

2. 用fopen打開url,以get方式獲取內容
?

$url = 'http://localhost/test2.php';
$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函數,以post方式獲取url
?

$data = array(
'foo'=>'bar',
'baz'=>'boom',
'site'=>'www.jb51.net',
'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 = "http://localhost/test2.php";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

echo $result;

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

$url = 'http://localhost/test2.php?site=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);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;

⑦ PHP如何正則表達式提取網頁內容

如果你要<div class="nav" monkey="nav">和<div class="head-ad">之間的所有源碼,用 preg_match 就可以,不用preg_match_all ,如果你要裡面的所有的 <li></li>標簽中的內容,可以用preg_match_all

//提取所有代碼
$pattern = '/<div class="nav" monkey="nav">(.+?)<div class="head-ad">/is';
preg_match($pattern, $string, $match);
//$match[0] 即為<div class="nav" monkey="nav">和<div class="head-ad">之間的所有源碼
echo $match[0];

//然後再提取<li></li>之間的內容
$pattern = '/<li.*?>(.+?)<\/li>/is';

preg_match_all($pattern, $match[0], $results);
$new_arr=array_unique($results[0]);

foreach($new_arr as $kkk){
echo $kkk;

}

⑧ 用php 寫一段提取某個網頁中的某段的代碼

<?php
//獲取網頁內容
$str=file_get_contents('http://book.zongheng.com/showchapter/199430.html');
//刪除<divclass="chapter">之前的內容
if($x=strpos($str,'<divclass="chapter">'))$str=substr($str,$x);
//刪除<!--章節列表結束-->之後的內容
if($x=strpos($str,'<!--章節列表結束-->'))$str=substr($str,0,$x);
//輸出結果
echo$str;
?>

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

?>

⑩ 如何用php獲取一個網頁iframe中的內容

//1.先獲取到iframe的節點或者src地址,也就是iframe中的url地址,
//2.再次將地址傳入到php,可以通過ajax.
//3.php端可以使用
$url='iframe的src地址';
$html=file_get_contents($url);

閱讀全文

與php提取網頁內容相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:962
phpffmpeg轉碼 瀏覽:671
長沙好玩的解壓項目 瀏覽:142
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:732
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:301
PDF分析 瀏覽:484
h3c光纖全工半全工設置命令 瀏覽:141
公司法pdf下載 瀏覽:381
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:349
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:890
app轉賬是什麼 瀏覽:163