echo "br/";
echo $arr[1][0];
}
再加上 echo$content;
*/
$url = ""; //目标站
$fp = @fopen($url, "r") or die("超时");
$content=file_get_contents($url);
$mode = "#title(.*)/title#";
if(preg_match_all($mode,$content,$arr)){
//print_r($arr);
echo "br/";
echo $arr[1][0];
}
?
script language="JavaScript" type="text/javascript"
--
function ref(){
window.location.href="";
}
setInterval("ref()",300000);
//--
/script
php获取指定网页内容一、用file_get_contents函数,以post方式获取url
?php
$url= '';
$data= https://www.04ip.com/post/array('foo'= 'bar');
$data= https://www.04ip.com/post/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= https://www.04ip.com/post/array('foo2'= 'bar2','foo3'='bar3');
$data= https://www.04ip.com/post/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的cURL库进行网页抓取 使用PHP的cURL库可以简单和有效地去抓网页 你只需要运行一个脚本 然后分析一下你所抓取的网页 然后就可以以程序的方式得到你想要的数据php抓取网页数据了 无论是你想从从一个链接上取部分数据 或是取一个XML文件并把其导入数据库 那怕就是简单的获取网页内容 cURL 是一个功能强大的PHP库 本文主要讲述如果使用这个PHP库
启用 cURL 设置
首先 php抓取网页数据我们得先要确定我们的PHP是否开启php抓取网页数据了这个库 你可以通过使用php_info()函数来得到这一信息
﹤?phpphpinfo();?﹥
如果你可以在网页上看到下面的输出 那么表示cURL库已被开启
如果你看到的话 那么你需要设置你的PHP并开启这个库 如果你是在Windows平台下 那么非常简单 你需要改一改你的php ini文件的设置 找到php_curl dll 并取消前面的分号注释就行了 如下所示
//取消下在的注释extension=php_curldll
如果你是在Linux下面 那么 你需要重新编译你的PHP了 编辑时 你需要打开编译参数——在configure命令上加上 –with curl参数
一个小示例
如果一切就绪 下面是一个小例程
﹤?php// 初始化一个 cURL 对象$curl = curl_init();
推荐阅读
- 李佳琦杨幂直播间说了什么的简单介绍
- 亳州专业sap支持服务,亳州哪里spa做得好
- 游戏动作回答技巧,游戏里面的动作是怎么实现的
- 声优直播录屏,声优直播在哪里听
- linux命令脚本制作 linux脚本执行命令如何写
- sqlserver2012要求,sqlserver2012必须装在c盘吗
- 折叠屏手机屏幕有什么好处,折叠屏手机真的有用吗
- u盘音响怎么不放音乐了,u盘在音响上读不出来怎么办
- go语言公司 go语言是哪个公司的