Skip to content

Commit

Permalink
added more advanced scraping example
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 26, 2012
1 parent 42919db commit dc28984
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

3.44 2012-09-25
3.44 2012-09-27
- Improved javascript and stylesheet helpers to not generate type
attributes.
- Improved documentation.
Expand Down
21 changes: 21 additions & 0 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -721,6 +721,27 @@ sense for a standalone parser.
say 'Heading: ', shift->all_text;
});

# Recurse through children and extract all text manually
sub all_text {
my $elements = shift;
for my $e ($elements->each) {

# Text before this element
print $e->text_before(0);

# Also include alternate text for images
print $e->{alt} if $e->type eq 'img';

# Text from children
my $children = $e->children;
$children->size ? all_text($children) : print $e->text(0);
}

# Text after last element
say $elements->[-1]->text_after(0);
}
all_text($tx->res->dom->children);

Especially for unit testing your L<Mojolicious> applications this can be a
very powerful tool.

Expand Down

0 comments on commit dc28984

Please sign in to comment.