方法一:file_get_contents

$url = 'http://www.baidu.com'; //要抓取的页面
$info=file_get_contents($url);
//preg_match('|<title>(.*?)<\/title>|i',$info,$m);
$m1=preg_match('|<title>(.*?)<\/title>|',$info,$m);
//var_dump($m1);
 echo $m[1]; //获取标题
 echo '<br/>';

方法二:curl

$url = "http://www.baidu.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$html = curl_exec($ch);
curl_close($ch);
 
$m1=preg_match('|<title>(.*?)<\/title>|',$html,$ms);
//echo "<textarea style='width:800px;height:600px;'>".$html."</textarea>";  
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
echo $ms[1]; //获取标题
Last modification:June 15th, 2019 at 05:52 pm