首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

Perl 处置 html文件

2012-06-25 
Perl 处理 html文件用perl 读取一个url地址,并获取html代码。返回的代码格式大致如下。HTML codehtmlbody

Perl 处理 html文件
用perl 读取一个url地址,并获取html代码。
返回的代码格式大致如下。

HTML code
<html>    <body>    <table></table>    <table>        <tr>            <td>                <table class="pane">                    <tr class="pane">                        <td colspan="2"></td>                    </tr>                    <tr>                        <td></td>                        <td>my content1</td>                    </tr>                    <tr class="pane">                        <td colspan="2"></td>                    </tr>                    <tr>                        <td></td>                        <td>my content2</td>                    </tr>                </table>            </td>        </tr>    </table></body></html>


想取到 这里边的值,各位高手帮忙看下如何实现。
就是取my content1,my content2.

逻辑:取class = pane的table,在此table中排出class=pane 的tr,对其他的tr取 第二个td的值。
HTML code
<table class="pane"> <tr>    <td></td>    <td>my content1</td>  </tr></table>


本人没接触过perl,不会写,要改别人的东西,没办法啊。各位兄弟帮下忙了,多谢啦。


[解决办法]
Perl code
use HTML::Element;use HTML::TreeBuilder;$tree = new HTML::TreeBuilder;$tree->parse_file('C:\Users\xxxx\Desktop\CVE\Tool\perl\mergehtml\xxx\1.html');#html file pathforeach my $table ( $tree->find_by_attribute('class','pane') ){        foreach my $td ( $table->find_by_tag_name('td') )    {        if($td->as_text()=~/my/)#这里的正则式自己改下        {            print $td->as_text()."\n";            }        }} 

热点排行