Snippets

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

Created by sironekotoro last modified
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} ) // ''; #コメントが空の場合がありうる
}

Comments (1)

  1. bend loyed

    We will really benefit from edu email, and you may even obtain free or discounted software if you use it. If you have any spare time, you may join me in playing monopoly online

HTTPS SSH

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