导航:首页 > 编程语言 > php正则抓取网页

php正则抓取网页

发布时间:2023-02-11 07:20:10

1. php正则表达式怎么抓取网页数据

会用正则就会抓取。
不会正则,一时半会也教不错。
不过,推荐你使用phpQuery这个框架,用jQuery的使用器来抓取数据。

2. 用PHP正则表达式提取页面内容

<?php
$theurl="http://www.kitco.cn/cn/";
if (!($contents = file_get_contents($theurl)))
{
echo 'Could not open URL';
exit;
}

/*
$contents=preg_replace('/<.+?>/', '', $contents);
*/

if (preg_match("/<td class=\"tableHeader\" align=\"left\">原油价格([^^]*?)<\/tr>/u",$contents,$matches))
{
print "A match was found:".strip_tags($matches[0]);
} else {
print "A match was not found.<br />";
}
?>

试试这样
------------------------------------
呵呵,上边这段已经把你那行注释掉了,先找到唯一的一段代码,取出来你想要的以后以后,再去掉标签,你运行一下试试
运行结果:
A match was found:原油价格 68.11 +0.95
应该是你想要的结果吧?

3. 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;

}

4. 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>

5. 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正则抓取网页相关的资料

热点内容
数据库查询系统源码 浏览:617
php5314 浏览:358
完美国际安装到哪个文件夹 浏览:669
什么app可以扫一扫做题 浏览:540
程序员编码论坛 浏览:924
淘点是什么app 浏览:660
中国高等植物pdf 浏览:454
51单片机时间 浏览:182
后台如何获取服务器ip 浏览:267
单片机流水灯程序c语言 浏览:235
程序员第二职业挣钱 浏览:240
运行里怎么输入服务器路径 浏览:843
pythonstepwise 浏览:512
刘一男词汇速记指南pdf 浏览:66
php认证级别 浏览:370
方舟编译啥时候推送 浏览:1012
php手机验证码生成 浏览:677
哲学思维pdf 浏览:17
凌达压缩机有限公司招聘 浏览:535
weblogic命令部署 浏览:39