⑴ 用php獲取網頁部分數據
如果你要
和
之間的所有源碼,用 preg_match 就可以,不用preg_match_all ,如果你要裡面的所有的
標簽中的內容,可以用preg_match_all //提取所有代碼 $pattern = '/
(.+?)
/is'; preg_match($pattern, $string, $match); //$match[0] 即為
和
之間的所有源碼 echo $match[0]; //然後再提取
之間的內容 $pattern = '/(.+?)li>/is'; preg_match_all($pattern, $match[0], $results); $new_arr=array_unique($results[0]); foreach($new_arr as $kkk){ echo $kkk; }
⑵ php獲取指定網頁內容
此類方法一共有三種
第一種方法
<?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_content函數來獲取源代碼,你只需要把網站傳入這個函數,獲取後是一個字元串,你需要格式化代碼就可以了
⑷ 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 如何獲取到一個網頁的內容
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獲取網頁內容的幾種方法
簡單的收集下PHP下獲取網頁內容的幾種方法:
用file_get_contents,以get方式獲取內容。
用fopen打開url,以get方式獲取內容。
使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經打開了curl擴展。
用file_get_contents函數,以post方式獲取url。
用fopen打開url,以post方式獲取內容。
用fsockopen函數打開url,獲取完整的數據,包括header和body。