1

想象一个 HTML 页面,它是一个具有重复结构的报告:

<html>
  <body>
    <h1>Big Hairy Report Page</h1>

    <div class="customer">
      <div class="customer_id">001</div>
      <div class="customer_name">Joe Blough</div>
      <div class="customer_addr">123 That Road</div>
      <div class="customer_city">Smallville</div>
      <div class="customer_state">Nebraska</div>
      <div class="order_info">
        <div class="shipping_details">
          <ul>
             <li>Large crate</li>
             <li>Fragile</li>
             <li>Express</li>
          </ul>
        </div>
        <div class="order_item">Deluxe Hoodie</div>
        <div class="payment">35.95</div>
        <div class="order_id">000123456789</div>
      </div>
      <div class="comment">StackOverflow rocks!</div>
    </div>

   <div class="customer">
     <div class="customer_id">002</div>
 ....  and so forth for a list of 150 customers

这种报表页面经常出现。我的目标是使用HTML::TreeBuilder::XPath.

我知道做基础并将文件读入$tree。但是,如何才能简洁地遍历该树并获得每个客户的相关信息集群?例如,如何根据此信息创建按客户编号排序的地址标签列表?如果我想按州对所有客户信息进行排序怎么办?

我不是要整个 perl(我可以读取我的文件,输出到文件等)。我只需要帮助理解如何向 HTML::TreeBuilder::XPath 询问这些相关数据包,然后如何取消引用它们。如果用输出语句来表达这一点更容易(即,Joe Blough 订购了 1 件 Deluxe Hoodie 并留下了 1 条评论),那么这也很酷。

非常感谢那些解决这个问题的人,这对我来说似乎有点不知所措。

4

3 回答 3

3

这将满足您的需求。

它首先将所有<div class="customer">元素拉入数组@customers并从中提取信息。

我以您的地址标签为例,按客户编号排序(我假设您的意思是带有 的字段class="customer_id")。所有地址值都从数组中提取到 hash%customers中,以客户 ID 和元素类的名称为键。然后按照 ID 的顺序打印信息。

use strict;
use warnings;

use HTML::TreeBuilder::XPath;

my $tree = HTML::TreeBuilder::XPath->new_from_file('html.html');

my @customers = $tree->findnodes('/html/body/div[@class="customer"');

my %customers;
for my $cust (@customers) {
  my $id = $cust->findvalue('div[@class="customer_id"]');
  for my $field (qw/ customer_name customer_addr customer_city customer_state /) {
    my $xpath = "div[\@class='$field']";
    my $val = $cust->findvalue($xpath);
    $customers{$id}{$field} = $val;
  }
}

for my $id (sort keys %customers) {

  my $info = $customers{$id};

  print "Customer ID $id\n";
  print $info->{customer_name}, "\n";
  print $info->{customer_addr}, "\n";
  print $info->{customer_city}, "\n";
  print $info->{customer_state}, "\n";
  print "\n";
}

输出

Customer ID 001
Joe Blough
123 That Road
Smallville
Nebraska
于 2013-03-06T08:04:17.310 回答
1

我将使用XML::LibXML,因为它更快并且我很熟悉它,但是如果您愿意,将我发布的内容从 XML::LibXML 转换为 HTML::TreeBuilder::XPath 应该非常简单。

use XML::LibXML qw( );

sub get_text { defined($_[0]) ? $_[0]->textContent() : undef }

my $doc = XML::LibXML->load_html(...);

my @customers;
for my $cust_node ($doc->findnodes('/html/body/div[@class="customer"]')) {
   my $id   = get_text( $cust_node->findnodes('div[@class="customer_id"]') );
   my $name = get_text( $cust_node->findnodes('div[@class="customer_name"]') );
   ...
   push @customers, {
      id   => $id,
      name => $name,
      ...
   };
}

实际上,鉴于数据的规律性,您不必对字段名称进行硬编码。

use XML::LibXML qw( );

sub parse_list {
   my ($node) = @_;
   return [
      map parse_field($_),
       $node->findnodes('li')
   ];
}

sub parse_field {
   my ($node) = @_;
   my @children = $node->findnodes('*');
   return $node->textContent() if !@children;
   return parse_list($children[0]) if $children[0]->nodeName() eq 'ul';
   return {
      map { $_->getAttribute('class') => parse_field($_) }
       @children
   };
}

{
   my $doc = XML::LibXML->load_html( ... );
   my @customers =
      map parse_field($_),
       $doc->findnodes('/html/body/div[@class="customer"]');

   ...
}
于 2013-03-06T03:06:48.300 回答
1
use HTML::TreeBuilder::XPath;
...

my @customers;
my $tree = HTML::TreeBuilder::XPath->new_from_content( $mech->content() );

foreach my $customer_section_node ( $tree->findnodes('//div[ @class = "customer" ]') ) {

   my $customer = {};
   $customer->{id} = find_customer_id($customer_section_node);
   $customer->{name} = find_customer_name($customer_section_node);
   ...
   push @customers, $customer;
}

$tree->delete();

sub find_customer_id {
    my $node = shift;

    my ($id) = $node->findvalues('.//div[ @class = "customer_id" ]');
    return $id
}
于 2013-03-06T04:18:39.810 回答