當前位置:首頁 » 編程語言 » php原鏈接

php原鏈接

發布時間: 2025-01-23 10:49:56

『壹』 萬能採集網站php源碼

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

可以參考以下幾種方法:

方法一:file_get_contents獲取

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

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

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

方法二:使用fopen獲取網頁源代碼

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

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

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

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

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

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

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

spanstyle="white-space:pre"?/spanecho$contents;//輸出獲取到得內容。

方法三:使用CURL獲取網頁源代碼

$url="";

$UserAgent='Mozilla/4.0(compatible;MSIE7.0;WindowsNT6.0;SLCC1;.NETCLR2.0.50727;.NETCLR3.0.04506;.NETCLR3.5.21022;.NETCLR1.0.3705;.NETCLR1.1.4322)'

$curl=curl_init();?//創建一個新的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不顯示。//如果成功只將結果返回,不自動輸出任何內容。如果失敗返回FALSE

curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);

curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);

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

//header中「Accept-Encoding:」部分的內容,支持的編碼格式為:"identity","deflate","gzip"。

curl_setopt($curl,CURLOPT_USERAGENT,$UserAgent);

curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);

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

$data=curl_exec($curl);

echo$data;

//echocurl_errno($curl);//返回0時表示程序執行成功

curl_close($curl);?//關閉cURL資源,並釋放系統資源

拓展資料

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

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

『貳』 用PHP獲取鏈接及圖片路徑的方法

<?php

$str="Thisisatest.Thisisatest.Thisisa<ahref=http://link1.com><imgsrc=http://img1.jpg/></a>test.Thisisatest.Thisisatest. ".
"Thisisatest.Thisisatest.<ahref=http://link2.com><imgsrc=http://img2.jpg/></a>Thisisatest.Thisisatest.Thisisatest. ".
"<ahref=http://link3.com><imgsrc=http://img3.jpg/></a>";

$regex='/<as+href=(.*)s*><imgs+src=(.*)s*/></a>/';
$output=array();

if(preg_match_all($regex,$str,$matches)!==false){
if(isset($matches[1])&&isset($matches[2])){
$links=$matches[1];
$imgs=$matches[2];

foreach($linksas$key=>$link){
$img=isset($imgs[$key])?$imgs[$key]:'';
$output[]="<ahref="{$link}"><imgsrc="{$img}"/></a>";
}
}
}

var_mp($output);

『叄』 php怎麼抓取這個鏈接https://locate.apple.com/cn/zh/service/pt=3&lat=23.134521&lon=113.358803的源碼

<?php
function dg_string($data,$flagA, $flagB, $start = 0){//配套截取字元串
$flagAL=strlen($flagA);
$flagBL=strlen($flagB);
$rn='';
$a=$b=0;
if(($findA=strpos($data,$flagA, $start))!==false){
$a=1;
$tmpA=$findA;
$findB=$findA+$flagAL;
$findA=$findB;
while($a!=$b){
if(($findB = strpos($data, $flagB, $findB))!==false){
$b++;
if(($findA = strpos($data, $flagA, $findA))!==false){
if($findA>$findB){
if($a==$b){
//結束
$findB+=$flagBL;
$rn=substr($data,$tmpA,$findB-$tmpA);
} else {
$a++;
$findB=$findA+$flagAL;
$findA=$findB;
}
} else {
$a++;
$findA+=$flagAL;
$findB+=$flagBL;
}
} else {
if($a==$b){
//結束
$findB+=$flagBL;
$rn=substr($data,$tmpA,$findB-$tmpA);
} else {
//標記不完整
$findB+=$flagBL;
}
}
} else {
//標記不完整
$rn=substr($data,$tmpA);
$rn.=str_repeat($flagB,$a-$b);
break;
}
}
}
return $rn;
}
$html = file_get_contents('https://locate.apple.com/cn/zh/service/?pt=3&lat=23.134521&lon=113.358803');//獲取源碼
$find = strpos($html, 'window.resourceLocator.setup');
$json1 = dg_string($html, '{', '}', $find);//獲取第一個JSON數據
$find = strpos($html, 'window.resourceLocator.storeSetup');
$json2 = dg_string($html, '{', '}', $find);//獲取第二個JSON數據
$arr1 = json_decode($json1, true);//第一個JSON數據轉為數組
$arr2 = json_decode($json2, true);//第二個JSON數據轉為數組
print_r($arr1);
print_r($arr2);
//得到了數組,你想獲取哪個參數都行了,你自己看著辦吧,樓主可親測代碼
?>

熱點內容
comtaobaotaobao文件夾 發布:2025-03-07 04:53:18 瀏覽:691
你存儲的畫面 發布:2025-03-07 04:51:03 瀏覽:417
lol低畫質什麼配置 發布:2025-03-07 04:50:59 瀏覽:351
服務搭建伺服器租用 發布:2025-03-07 04:42:16 瀏覽:427
php判斷當前 發布:2025-03-07 04:38:36 瀏覽:334
qq為什麼老讓改密碼 發布:2025-03-07 04:36:00 瀏覽:467
微信密碼保護怎麼取消 發布:2025-03-07 04:33:48 瀏覽:276
網線的賬號密碼是在什麼中設置的 發布:2025-03-07 04:32:07 瀏覽:255
網站伺服器ip地址改動 發布:2025-03-07 04:31:57 瀏覽:901
elasticsearch存儲數據 發布:2025-03-07 04:24:52 瀏覽:252