Snippets

sironekotoro connpassのページ下部にあるフィード欄の情報を取得する

You are viewing an old version of this snippet. View the current version.
Revised by sironekotoro 26f1f60
use strict;
use warnings;
use feature qw/say/;
use Data::Dumper;
use Encode qw/encode_utf8/;
use URI qw/new/;
use Web::Scraper qw/scraper/;

# connpassのフィード欄を取得したい
my $url = URI->new('https://perl-entrance-tokyo.connpass.com/event/89771/');

# 取得したいフィード欄(ページ下部)は
# xpathで
#  <div class="group_box" id="feed">
# のタグで囲まれた範囲の
#  thumb36_list
# というclass属性を持つdivをscrapeする。返り値は配列リファレンス

my $scraper = scraper {
    process '//div[@id="feed"]//div[contains( @class , "thumb36_list" )] ',

        'feeds[]' => scraper {    # 上記xpath内をさらにscrapeする

            # class 属性が date の pタグのTEXT部を取得する
            process '//p[@class="date"]',
            'time' => 'TEXT',
            # class 属性が message の pタグの中にあり、
            # 最初に出現するaタグのTEXT部を取得する
            process '//p[@class="message"]/a[1]',
            'user' => 'TEXT',
            # class 属性が message の pタグのTEXT部を取得する
            process '//p[@class="message"]',
            'action' => 'TEXT',
            # class 属性が icon_comment の spanタグのTEXT部を取得する
            process '//span[@class="icon_comment"]',
            'comment' => 'TEXT',
    }
};

my $res = $scraper->scrape($url);

for my $feed ( @{ $res->{feeds} } ) {
    say '-' x 10;
    say encode_utf8( $feed->{time} );
    say encode_utf8( $feed->{user} );
    say encode_utf8( $feed->{action} );
    say encode_utf8( $feed->{comment} ) // ''; #コメントが空の場合がありうる
}

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.