導航:首頁 > 編程語言 > phpcurl抓取網頁指定內容

phpcurl抓取網頁指定內容

發布時間:2022-06-04 10:36:06

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 CURL 抓取微信網頁的內容

給你簡單介紹幾個吧
一、file_get_contents函數
$content = file_get_contents("URL");//URL就是你要獲取的頁面的地址
二、利用curl擴展
代碼如下:
function getCurl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不輸出內容
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}

PS:需要安裝PHP的curl擴展

Ⅲ 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獲取指定地址 的網頁源文件

$info=file_get_contents('https://..com/question/1513631908043601820.html?entry=&ishq=1');

//info就是網頁的源文件可以直接輸出也可以保存

echofile_put_contents('./test.html',$info)?'保存成功':'保存失敗';//保存

Ⅳ 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的curl怎麼爬取網頁內容

編程爬蟲比較難入門。
建議使用操作簡單、功能強大的八爪魚。

八爪魚具有以下特點:

1、行業知名,全球300萬+用戶都在使用。
2、內置數百個主流網站採集模板,滿足絕大部分採集需求,會滑鼠點擊以及文本輸入即可採集數據。
3、智能採集,自動識別多種驗證碼,提供代理IP池,結合UA切換,可有效突破封鎖,順利採集數據。
4、可視化操作流程,眼見即可采(可採集市面上98%的網站),不管是圖片電話,還是自媒體論壇,支持所有業務渠道的爬蟲,滿足各種採集需求。
5、雲採集,5000台雲伺服器,24*7高效穩定採集,結合API可無縫對接內部系統,定期同步爬數據。
6、支持企業私有化部署,可部署在隔離內網環境。提供定製化部署方案,滿足多種數據安全級別。

Ⅶ 用php獲取指定網頁內容

functiongetRemoteRes($url,$postfields=NULL,$timeout=60){
$ci=curl_init();
curl_setopt($ci,CURLOPT_URL,$url);
curl_setopt($ci,CURLOPT_HEADER,FALSE);
curl_setopt($ci,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ci,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ci,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ci,CURLOPT_TIMEOUT,$timeout);
curl_setopt($ci,CURLOPT_POST,TRUE);
if(is_array($postfields)){
$field_str="";
foreach($postfieldsas$k=>$v){
$field_str.="&$k=".urlencode($v);
}
curl_setopt($ci,CURLOPT_POSTFIELDS,$field_str);
}
$response=curl_exec($ci);
if(curl_errno($ci)){
return'ERRNO!';
}else{
$httpStatusCode=curl_getinfo($ci,CURLINFO_HTTP_CODE);
if(200!==$httpStatusCode){
return'ERRNO!';
}
}
curl_close($ci);
return$response;
}
先用以上函數獲取指定的網頁,然後從返回的數據中解析出你要的數據.可以使用正則表達式來提取,這要根據你要獲取的頁面源代碼來判斷了.暫時未知,以上只是提供一個思路給你.

Ⅷ php中想要抓取網頁中某一段的數據的代碼

<?php
$url='abc.com/';
$data=get_file($url);

$pattern='你的內容正則表達式';
perg_match($pattern,$data,$match);

print_r($match);

function get_file($url)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
return $data;
}
?>

閱讀全文

與phpcurl抓取網頁指定內容相關的資料

熱點內容
CS編輯命令 瀏覽:947
程序員編碼是指什麼 瀏覽:525
在雲伺服器上安裝軟體 瀏覽:270
什麼app可以免費聽周董的歌 瀏覽:364
netmvcpdf 瀏覽:209
arp伺服器回送的是什麼地址 瀏覽:103
生物學pdf百度雲 瀏覽:963
markdown源碼包怎麼下載 瀏覽:598
餐飲app開發公司哪個好 瀏覽:637
解壓盒子2無廣告 瀏覽:623
華為鴻蒙系統怎麼放大單個文件夾 瀏覽:587
phpwin864 瀏覽:913
boll優化源碼公式 瀏覽:906
連接伺服器埠號是如何確定的 瀏覽:14
gsm源碼 瀏覽:475
單片機點陣滾動 瀏覽:407
加密資產平台 瀏覽:721
二級建築師pdf教材 瀏覽:324
單片機i和e系列 瀏覽:320
peb是什麼伺服器 瀏覽:389