Home > Backend Development > PHP Tutorial > PHP抓取网页特定数据

PHP抓取网页特定数据

WBOY
Release: 2016-06-13 10:14:53
Original
1219 people have browsed it

求助:PHP抓取网页特定数据
我要抓取网页:http://www.pm2d5.com/City/xg_wangjiao.html网页的表格里PM2.5的具体数据,
有没有什么高效点的方法?谢谢。。

------解决方案--------------------
我这属于笨方法,无奈我的正则实在太烂

PHP code
// $html 是你要抓取的www.pm2d5.com/City/xg_wangjiao.html内容,这部分你自己写吧$pattern =  "/<td align="center" bgcolor='\"#DCEAF3\"'>[\s\S]+?/i";preg_match_all($pattern, $html, $tdTag);// var_dump($tdTag);// 要消除的匹配结果中的HTML标签$deleteStrHeader = '</td><td align="center" bgcolor="#DCEAF3"><font face="細明體 ,  新細明體 , Taipei, Arial" size="3">';$deleteStrFooter = '</font></td>';$pmvalue = array();foreach($tdTag[0] as $key=>$value){               // 当遍历到pm2.5那列时        if( ($key+1) % 6 === 0)        {            // 消除头尾HTML标签            $noHeaderStr = str_replace($deleteStrHeader, '', $value);            $cleanStr = str_replace($deleteStrFooter, '', $noHeaderStr);            // 转换为浮点型            $pmvalue[] = (float)trim($cleanStr);           }}var_dump($pmvalue);<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template