很多站长朋友们都不太清楚php读取百度的页面,今天小编就来给大家整理php读取百度的页面,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php如何读取指定页面 2、 php获取指定网页内容 3、 用php代码获取百度搜索后页面里面的10个链接 4、 如何使用php模拟获取百度搜索结果并和百度 php如何读取指定页面这个需要用到ajax,不能把php代码直接放到<div id="box"></div>中,可以用jquery的
test.php
<?php
$url = "";//要读取的网页地址
$content = file_get_contents($url);//读取内容
echo $content;//输出读取的内容
?>
$.ajax({
url: "test.php",
cache: false,
success: function(html){
$("#box").html(html);
}
});
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代码获取百度搜索后页面里面的10个链接就是获取页面内容,然后处理。挺麻烦的...有些情况还得特别处理...你自己研究吧。
<?php
$str=file_get_contents("");
$arr = explode('<span class="g">',$str);
for($i=1;$i<count($arr);$i++){
$arrx = explode('/',$arr[$i]);
echo $i.$arrx[0].'<br>';
}
结果:
1
2
3
4 tonytony.cn
5
6
7 bbs.xjtcedu.com
8fanyi.baidu.com<
9 gbbs<
10
如何使用php模拟获取百度搜索结果并和百度直接模拟链接获取百度搜索结果达到300多KB,推送到客户端浏览器即使用Gzip压缩也要100KB左右,耗时接近1.0s。但是百度自己的第一个get内容到浏览器只有30KB左右。怎样才能和百度一样。试过PHP几种方式获取百度搜索结果但是其实都一样,样式什么都一起加载过来了。
例如:
$url=" /s?word=".$kw;
$html=file_get_contents($url);
//$html > 300KB
//Apache开启网页压缩之后发送到浏览器!
关于php读取百度的页面的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php读取百度的页面 php读取网页源代码的详细内容...