| 1 | # ABSTRACT: http:://tieba.baidu.com | ||
| 2 | package Novel::Robot::Parser::tieba; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use utf8; | ||
| 6 | |||
| 7 | use base 'Novel::Robot::Parser'; | ||
| 8 | |||
| 9 | use HTML::Entities; | ||
| 10 | use Web::Scraper; | ||
| 11 | |||
| 12 | sub base_url { 'http://tieba.baidu.com' } | ||
| 13 | |||
| 14 | sub charset { 'utf8' } | ||
| 15 | |||
| 16 | sub site_type { 'tiezi' } | ||
| 17 | |||
| 18 | sub scrape_novel { | ||
| 19 | { title => { path => '//h3' }, | ||
| 20 | writer => { path => '//li[@class="d_name"]' }, | ||
| 21 | }; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub parse_novel_item { | ||
| 25 | my ( $self, $h ) = @_; | ||
| 26 | |||
| 27 | my $parse_query = scraper { | ||
| 28 | process '//div[contains(@class,"l_post ")]', | ||
| 29 | 'items[]' => scraper { | ||
| 30 | process '.', 'info' => '@data-field'; | ||
| 31 | process_first '//h1[@class="core_title_txt"]', 'title' => 'TEXT'; | ||
| 32 | process_first '//li[@class="d_name"]', 'writer' => 'TEXT'; | ||
| 33 | process_first '//div[contains(@class,"d_post_content ")]', content => 'HTML'; | ||
| 34 | }; | ||
| 35 | }; | ||
| 36 | my $ref = $parse_query->scrape( $h ); | ||
| 37 | |||
| 38 | my @items; | ||
| 39 | for my $f ( @{ $ref->{items} } ) { | ||
| 40 | next unless ( $f->{content} ); | ||
| 41 | $f->{writer} ||= 'unknown'; | ||
| 42 | if ( $f->{info} ) { | ||
| 43 | my $x = decode_entities( $f->{info} ); | ||
| 44 | ( $f->{id} ) = $x =~ /"post_no":(\d+),/s; | ||
| 45 | ( $f->{time} ) = $x =~ /"date":"(.+?)",/s; | ||
| 46 | delete( $f->{info} ); | ||
| 47 | } | ||
| 48 | push @items, $f; | ||
| 49 | } | ||
| 50 | return \@items; | ||
| 51 | } ## end sub parse_novel_item | ||
| 52 | |||
| 53 | sub parse_item_list { | ||
| 54 | my ( $self, $h ) = @_; | ||
| 55 | my $parse_item = scraper { | ||
| 56 | process_first '//link[@rel="canonical"]', 'base' => '@href'; | ||
| 57 | process_first '//li[@class="l_reply_num"]', 'page' => 'TEXT'; | ||
| 58 | }; | ||
| 59 | my $ref = $parse_item->scrape( $h ); | ||
| 60 | my ( $page ) = $ref->{page} =~ /共(\d+)页/s; | ||
| 61 | my @urls = map { "$ref->{base}?pn=$_" } ( 2 .. $page ); | ||
| 62 | return \@urls; | ||
| 63 | } | ||
| 64 | |||
| 65 | 1; |