用PHP获取网页部分数据

2024-12-17 12:02:34
推荐回答(2个)
回答1:

如果你要

之间的所有源码,用 preg_match 就可以,不用preg_match_all ,如果你要里面的所有的

标签中的内容,可以用preg_match_all //提取所有代码 $pattern = '/
(.+?)
/is'; preg_match($pattern, $string, $match); //$match[0] 即为

之间的所有源码 echo $match[0]; //然后再提取

之间的内容 $pattern = '/(.+?)li>/is'; preg_match_all($pattern, $match[0], $results); $new_arr=array_unique($results[0]); foreach($new_arr as $kkk){ echo $kkk; }

回答2:

用正则表达式,是最快的,你看下面:

$url = 'http://www.baidu.com'; //这儿填页面地址
$info=file_get_contents($url);
preg_match('|(.*?)<\/title>|i',$info,$m);<br>echo $m[1];<br>?></p> </div> </div> <div class="clear"></div> </div> </div> </div> <div class="wendaright"> <div class="wdluluerwema"> <div class="wdxgwttop">相关问答</div> <div class="wdxgwtnr"> </div> <div class="clear"></div> </div> <!-- 其他随机问答['id'=>alphaID($like['zid'])] --> <div class="wdluluerwema"> <div class="wdxgwttop">最新问答</div> <div class="wdxgwtnr"> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/2059158303896219187.html">2020年集成灶十大公认品牌排行榜?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/66923273.html">09江西省公务员考试</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/89067948.html">关于波斯王子4的问题</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/1924077712298856307.html">没有初中毕业证是中专在校生。还没毕业能去当兵吗?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/1518065776913398260.html">华为怎么看手机电池容量是多少毫安</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/172828981.html">FL studio里的音色库没声不发声</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/100036623.html">从老虎滩到付家庄出来,,又该怎么坐车。</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/88892868.html">帝国时代3的历史问题</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/486162812.html">青岛市国立设计公司,发展怎么样,福利待遇好吗?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/525423356331546405.html">计算机软件著作权登记有效期是多久?</a></div> </div> </div> </div> </div> <div class="clear"></div> <div class="footer"> <!-- 移动底部导航 --> <div class="fanhuitop"><a href="#top" ref="nofollow"><img src="https://www.g230.com/static/old/img/fhtop.png" alt="返回顶部" title="返回顶部"></a></div> <div class="dibu"> <div class="dibu"> </div> </div> <div class="banquan"> <p>内容全部来源于网络收集,如有侵权,请联系网站删除:QQ:24596024</p> </div> </div> </div> </div> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?78733b427b0a9dce19688d903e89fc12"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>