① php抓取网页指定内容
<?php
/*
* 如下: 方法有点笨
* 抓取网页内容用 PHP 的正则
* 用JS每隔5分钟刷新当前页面---即重新获取网页内容
*
* 注: $mode中--<title></title>-更改为所需内容(如 $mode = "#<a(.*)</a>#";>获取所有链接)
*
* window.location.href="http://localhost//refesh.php";中的http://localhost//refesh.php
* 更改为自己的URL----作用:即刷新当前页面
*
* setInterval("ref()",300000);是每隔300000毫秒(即 5 * 60 *1000 毫秒即5分钟)执行一次函数 ref()
*
* print_r($arr);输出获得的所有内容 $arr是一个数组 可根据所需输出一部分(如 echo $arr[1][0];)
* 若要获得所有内容 可去掉
* $mode = "#<title>(.*)</title>#";
if(preg_match_all($mode,$content,$arr)){
print_r($arr);
echo "<br/>";
echo $arr[1][0];
}
再加上 echo $content;
*/
$url = "http://www..com"; //目标站
$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="http://localhost//refesh.php";
}
setInterval("ref()",300000);
//-->
</script>
② PHP 如何获取到一个网页的内容
1.file_get_contents
PHP代码
复制代码 代码如下:
<?php
$url = "http://www.jb51.net";
$contents = file_get_contents($url);
//如果出现中文乱码使用下面代码
//$getcontent = iconv("gb2312", "utf-8",$contents);
echo $contents;
?>
2.curl
PHP代码
复制代码 代码如下:
<?php
$url = "http://www.jb51.net";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//在需要用户检测的网页里需要增加下面两行
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
//curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
?>
3.fopen->fread->fclose
PHP代码
复制代码 代码如下:
<?php
$handle = fopen ("http://www.jb51.net", "rb");
$contents = "";
do {
$data = fread($handle, 1024);
if (strlen($data) == 0) {
break;
}
$contents .= $data;
} while(true);
fclose ($handle);
echo $contents;
?>
注:
1.
使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置
allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件。
2.使用curl必须空间开启curl。方法:windows下修改php.ini,将extension=php_curl.dll前面的分
号去掉,而且需要拷贝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安装curl扩
展。
③ PHP怎样抓取网页代码中动态显示的数据
你是想抓别人网页上ajax动态载入的数据吧?
1、要找到它的ajax载入的URL地址
2、利用PHP的file_get_contents($url)函数读取那个url地址。
3、对抓取到的内容进行分析或正则过滤。
④ php获取指定网页内容
此类方法一共有三种
第一种方法
<?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);
?>
⑤ 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怎么抓取其它网站数据
可以用以下4个方法来抓取网站 的数据:
1. 用 file_get_contents 以 get 方式获取内容:
?
$url = 'http://localhost/test2.php';
$html = file_get_contents($url);
echo $html;
2. 用fopen打开url,以get方式获取内容
?
$url = 'http://localhost/test2.php';
$fp = fopen($url, 'r');
stream_get_meta_data($fp);
$result = '';
while(!feof($fp))
{
$result .= fgets($fp, 1024);
}
echo "url body: $result";
fclose($fp);
3. 用file_get_contents函数,以post方式获取url
?
$data = array(
'foo'=>'bar',
'baz'=>'boom',
'site'=>'www.jb51.net',
'name'=>'nowa magic');
$data = http_build_query($data);
//$postdata = http_build_query($data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $data
//'timeout' => 60 * 60 // 超时时间(单位:s)
)
);
$url = "http://localhost/test2.php";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
4、使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展
$url = 'http://localhost/test2.php?site=jb51.net';
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $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如何正则表达式提取网页内容
如果你要<div class="nav" monkey="nav">和<div class="head-ad">之间的所有源码,用 preg_match 就可以,不用preg_match_all ,如果你要里面的所有的 <li></li>标签中的内容,可以用preg_match_all
//提取所有代码
$pattern = '/<div class="nav" monkey="nav">(.+?)<div class="head-ad">/is';
preg_match($pattern, $string, $match);
//$match[0] 即为<div class="nav" monkey="nav">和<div class="head-ad">之间的所有源码
echo $match[0];
//然后再提取<li></li>之间的内容
$pattern = '/<li.*?>(.+?)<\/li>/is';
preg_match_all($pattern, $match[0], $results);
$new_arr=array_unique($results[0]);
foreach($new_arr as $kkk){
echo $kkk;
}
⑧ 用php 写一段提取某个网页中的某段的代码
<?php
//获取网页内容
$str=file_get_contents('http://book.zongheng.com/showchapter/199430.html');
//删除<divclass="chapter">之前的内容
if($x=strpos($str,'<divclass="chapter">'))$str=substr($str,$x);
//删除<!--章节列表结束-->之后的内容
if($x=strpos($str,'<!--章节列表结束-->'))$str=substr($str,0,$x);
//输出结果
echo$str;
?>
⑨ 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获取一个网页iframe中的内容
//1.先获取到iframe的节点或者src地址,也就是iframe中的url地址,
//2.再次将地址传入到php,可以通过ajax.
//3.php端可以使用
$url='iframe的src地址';
$html=file_get_contents($url);