导航:首页 > 编程语言 > 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正则抓取网页相关的资料

热点内容
图片合并到一个文件夹 浏览:573
mysql执行cmd命令 浏览:70
有为财经源码 浏览:344
驾照预约计时app是什么软件 浏览:223
非对称加密怎么计算 浏览:55
应用被加密该怎么办 浏览:716
程序员b2等级 浏览:236
微信应用分身怎么加密 浏览:892
黑羽命令 浏览:93
冰箱压缩机上面的黑胶 浏览:597
单片机连线是什么线 浏览:757
宽带加密方式选择 浏览:340
javaweb博客 浏览:70
linux监控目录 浏览:446
51单片机iic通信的引脚 浏览:769
cmd命令如何进入c盘 浏览:291
金山pdf独立版 浏览:241
信息在文件夹怎么看 浏览:134
云服务器包月之后还有额外费用吗 浏览:977
安卓版死神来了第27关怎么过 浏览:980