使用HTML :: TableExtract从表中删除整行

时间:2021-04-25 09:52:06

Is there a way to remove an entire row (html tags 'n all) from an HTML Table with HTML::TableExtract?

有没有办法从带有HTML :: TableExtract的HTML表中删除整行(html标签'全部)?

Mucking around with the sample code from CPAN, this is what I've tried so far:

来自CPAN的示例代码,这是我到目前为止所尝试的:

use HTML::TableExtract qw(tree);

my $te = HTML::TableExtract->new( headers => [qw(name type members)] );

# get $html_string out of a file...

$te->parse($html_string);

my $table = $te->first_table_found();
my $table_tree = $table->tree;
$table_tree->row(4)->replace_content('');
my $document_tree = $te->tree;
my $document_html = $document_tree->as_HTML;

# write $document_html to a file ...

Now, as the name suggests, 'replace_content()' in the line $table_tree->row(4)->replace_content(''); removes the content of row 4, but the row itself remains in markup. I need to get the tags and everything in-between removed as well.

现在,顾名思义,行'$ table_tree-> row(4) - > replace_content('')中的'replace_content()';删除第4行的内容,但该行本身仍保留在标记中。我需要删除标签,并删除其间的所有内容。

Any ideas?

有任何想法吗?

1 个解决方案

#1


3  

What you want is the parent and delete methods

你想要的是父方法和删除方法

See the docs for HTML::Element and for HTML::Element::delete

请参阅HTML :: Element和HTML :: Element :: delete的文档

UPDATE

UPDATE

Ok, click that checkmark and mark this one as answered....Here it is:

好的,单击该复选标记并将此标记为已解答....这是:

my($p) = $table_tree->row(4)->parent();
$p->delete;

Also, NOTE, you need the () parens around $p! If you don't have parens don't get back a reference.

另外,请注意,你需要$ p周围的pare!如果你没有parens不回来参考。

For me, with the above Perl code working on this HTML,

对我来说,使用上面的Perl代码处理这个HTML,

<table>
   <tr><td>name</td><td>type</td><td>members</td></tr>
   <tr><td>row1</td><td>row1</td> <td>row1</td></tr>
   <tr><td>row2</td><td>row2</td> <td>row2</td></tr>
   <tr><td>row3</td><td>row3</td> <td>row3</td></tr>
   <tr><td>row4</td><td>row4</td> <td>row4</td></tr>
</table>

I get this as a result of printing $document_html

因为打印$ document_html,我得到了这个

<table>
   <tr><td>name</td><td>type</td><td>members</td></tr>
   <tr><td>row1</td><td>row1</td><td>row1</td></tr>
   <tr><td>row2</td><td>row2</td><td>row2</td></tr>
   <tr><td>row3</td><td>row3</td><td>row3</td></tr>
</table>

Notice that there is no empty <tr></tr>

请注意,没有空

#1


3  

What you want is the parent and delete methods

你想要的是父方法和删除方法

See the docs for HTML::Element and for HTML::Element::delete

请参阅HTML :: Element和HTML :: Element :: delete的文档

UPDATE

UPDATE

Ok, click that checkmark and mark this one as answered....Here it is:

好的,单击该复选标记并将此标记为已解答....这是:

my($p) = $table_tree->row(4)->parent();
$p->delete;

Also, NOTE, you need the () parens around $p! If you don't have parens don't get back a reference.

另外,请注意,你需要$ p周围的pare!如果你没有parens不回来参考。

For me, with the above Perl code working on this HTML,

对我来说,使用上面的Perl代码处理这个HTML,

<table>
   <tr><td>name</td><td>type</td><td>members</td></tr>
   <tr><td>row1</td><td>row1</td> <td>row1</td></tr>
   <tr><td>row2</td><td>row2</td> <td>row2</td></tr>
   <tr><td>row3</td><td>row3</td> <td>row3</td></tr>
   <tr><td>row4</td><td>row4</td> <td>row4</td></tr>
</table>

I get this as a result of printing $document_html

因为打印$ document_html,我得到了这个

<table>
   <tr><td>name</td><td>type</td><td>members</td></tr>
   <tr><td>row1</td><td>row1</td><td>row1</td></tr>
   <tr><td>row2</td><td>row2</td><td>row2</td></tr>
   <tr><td>row3</td><td>row3</td><td>row3</td></tr>
</table>

Notice that there is no empty <tr></tr>

请注意,没有空