本文目錄一覽:
- 1、php獲取指定網頁內容
- 2、php獲取指定網站的文章標題以及連接
- 3、php如何通過地址去獲取一個網頁的標題title裡面的內容
- 4、php網頁怎麼抓取一部分正文作為標題
- 5、求一個簡易的php爬蟲提取網頁的title
- 6、php遠程讀取標題編碼問題
php獲取指定網頁內容
一、用file_get_contents函數,以post方式獲取url
?php
$url= ”;
$data= array(‘foo’= ‘bar’);
$data= http_build_query($data);
$opts= array(
‘http’= array(
‘method’= ‘POST’,
‘header’=”Content-type: application/x-www-form-urlencoded\r\n” .
“Content-Length: ” . strlen($data) . “\r\n”,
‘content’= $data
)
);
$ctx= stream_context_create($opts);
$html= @file_get_contents($url,”,$ctx);
二、用file_get_contents以get方式獲取內容
?php
$url=”;
$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\r\nCookie:cook1=c3;cook2=c4\r\n” .
“Content-Length: ” . strlen($data) . “\r\n”,
‘content’= $data
)
);
$context= stream_context_create($opts);
$html= fopen(‘;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, ”);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents= curl_exec($ch);
curl_close($ch);
echo$file_contents;
?
php獲取指定網站的文章標題以及連接
寫一個正則匹配就可以了
$html=’li………………/li’;
preg_match_all(‘/lia href=”(.*)”(.*)\/aspan style=”color:#F00;”(.*)\/span\/li/Ui’,$html,$data);
print_R($data);//就有數據了注意空格那些都要和代碼里的一致
php如何通過地址去獲取一個網頁的標題title裡面的內容
具體代碼如下:
?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_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 獨特的語法混合了C、Java、Perl以及PHP自創的語法。
它可以比CGI或者Perl更快速地執行動態網頁。用PHP做出的動態頁面與其他的編程語言相比,PHP是將程序嵌入到HTML(標準通用標記語言下的一個應用)文檔中去執行,
執行效率比完全生成HTML標記的CGI要高許多;
PHP還可以執行編譯後代碼,編譯可以達到加密和優化代碼運行,使代碼運行更快。
php網頁怎麼抓取一部分正文作為標題
截取 前n個字元不可以嗎?
$config[‘title’] = mb_substr($config[‘title’], 0, 20);
求一個簡易的php爬蟲提取網頁的title
header(“Content-Type: text/html; charset=gbk”);
$url = “”;
$fcontents = file_get_contents($url);
if (ereg(“title(.*)/title”, $fcontents, $regs)){echo “ok”;}else{echo “error”;}
echo “br”;
print_r($regs);
php遠程讀取標題編碼問題
沒法定義,
php不會自動轉碼,或者http協議里也不會按照你的參數設置自動轉碼
所以,這需要你自己用代碼去轉換編碼
思路:
1.連接網頁讀取數據.
2.從header頭信息里或者網頁代碼里獲取網頁的編碼方式(字符集,gbk,utf8等)
3.根據需要把數據轉換成你要的字符集
4.解析數據
注:
1.抓數據,若使用file(),file_get_contents()等,網頁字符集信息,可從html代碼里用正則匹配出來
meta
http-equiv=”Content-Type”
content=”text/html;
charset=gb2312″
/
2.若使用fsockopen()抓數據,
可從http響應頭裡取的字符集.但響應頭裡也可能沒有字符集信息.最好再結合html頭部信息解析下
3.php的正則,建議使用
preg庫,
那個功能和性能都更好些
原創文章,作者:WDSG,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/139889.html