当前位置:首页 » 编程语言 » php截取网页内容

php截取网页内容

发布时间: 2023-09-05 12:58:54

php获取指定网页内容

一、用file_get_contents函数,以post方式获取url

<?php

$url='http://www.domain.com/test.php?id=123';

$data=array('foo'=>'bar');

$data= http_build_query($data);

$opts=array(

'http'=>array(

'method'=>'POST',

'header'=>"Content-type: application/x-www-form-urlencoded " .

"Content-Length: " .strlen($data) ." ",

'content'=>$data

)

);

$ctx= stream_context_create($opts);

$html= @file_get_contents($url,'',$ctx);

二、用file_get_contents以get方式获取内容

<?php

$url='http://www.domain.com/?para=123';

$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 Cookie:cook1=c3;cook2=c4 " .

"Content-Length: " .strlen($data) ." ",

'content'=>$data

)

);

$context= stream_context_create($opts);

$html=fopen('http://www.test.com/zzzz.php?id=i3&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,'http://www.domain.com/');

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,$timeout);

$file_contents= curl_exec($ch);

curl_close($ch);

echo$file_contents;

?>

⑵ php怎么抓取其它网站数据

最基本的原理就是获取该网页的内容之后 通过正则去匹配 获取自己想要的内容

⑶ php中想要抓取网页中某一段的数据的代码

<?php
$url='abc.com/';
$data=get_file($url);

$pattern='你的内容正则表达式';
perg_match($pattern,$data,$match);

print_r($match);

function get_file($url)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
return $data;
}
?>

⑷ 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获取指定网页内容

此类方法一共有三种

  1. 第一种方法

<?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);

?>

热点内容
国服没有脚本吗 发布:2025-03-13 02:52:57 浏览:702
机器人解压 发布:2025-03-13 02:52:18 浏览:955
怎么在服务器上部署网站 发布:2025-03-13 02:52:15 浏览:207
android弹出键盘布局 发布:2025-03-13 02:46:22 浏览:379
单耳安卓蓝牙耳机怎么使用教程 发布:2025-03-13 02:36:22 浏览:518
配置apache以域名访问 发布:2025-03-13 02:22:34 浏览:559
android视频录制播放 发布:2025-03-13 02:10:32 浏览:601
php从数据库中读取数据 发布:2025-03-13 02:10:27 浏览:942
热血传奇任务脚本 发布:2025-03-13 02:06:16 浏览:303
t4服务器什么牌子好 发布:2025-03-13 02:02:39 浏览:633