-
Notifications
You must be signed in to change notification settings - Fork 2
/
sportsmole_matchreport.pl
141 lines (125 loc) · 3.26 KB
/
sportsmole_matchreport.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#! /usr/bin/perl -w
use strict;
use warnings;
use HTML::Entities;
use utf8;
use open ':std', ':encoding(utf8)';
####################################################################################
# A script to scrape match reports from sportsmole.co.uk as nice and handy xml-files
####################################################################################
my $url;
my @urls;
my $title;
my $date;
my $kickoff;
my $team1;
my $team2;
my $head;
my $teaser;
my $p;
my $filename;
my $result;
my $start_url = "https://www.sportsmole.co.uk/football/premier-league/2017-18/results.html";
# --> Define the start page (open this URL in your browswer and choose by the dropdown menu)
if ($start_url =~ /premier-league\/(.+?)\//) {
$filename = $1;
}
my $path = "/path/$filename.xml";
# --> Define path
############################
# no changes below this line
############################
unlink($path);
print "Fetching URLs…";
my $start_html = qx(curl -s '$start_url');
my @lines = split /\n/, $start_html;
foreach my $line (@lines) {
if ($line =~ m/href="(.+?)">Match Report<\/a>/) {
$url = "https://www.sportsmole.co.uk" . $1;
push @urls, $url;
}
}
print " Done!\n";
my $counter = 0;
my $length = scalar @urls;
open OUT, ">> $path" or die $!;
print OUT "<corpus>\n";
foreach my $url_game (@urls) {
my $html = qx(curl -s '$url_game');
$counter++;
print "Get no. $counter of $length\n";
my @lines = split /\n/, $html;
foreach my $line (@lines) {
if ($line =~ /<title>(.+?)<\/title>/) {
$title = $1;
}
if ($line =~ /property="og:description" content="(.+?)">/) {
$teaser = decode_entities($1);
$teaser =~ s/&/&/g;
}
if ($line =~ /class="game_header_score">(.+?)</) {
$result = $1;
}
if ($line =~ /datetime="(.+?)T(.+?):00\+.+?"/) {
$date = $1;
$kickoff = $2;
}
if ($line =~ /<h1 id="title_text" itemprop="headline">(.+?)<\/h1>/) {
$head = $1;
$head =~ s/&/&/g;
}
}
if ($html =~ /class="game_header_bar_team left">(.+?)</) {
$team1 = $1;
}
if ($html =~ /class="game_header_bar_team left"><span[\w\W]+?desktop_only">(.+?)</) {
$team1 = $1;
}
if ($html =~ /class="game_header_bar_team">(.+?)</) {
$team2 = $1;
}
if ($html =~ /class="game_header_bar_team"><span[\w\W]+?desktop_only">(.+?)</) {
$team2 = $1;
}
$team1 =~ s/&/&/g;
$team2 =~ s/&/&/g;
print OUT "<text>
<url>$url_game</url>
<title>$title</title>
<team1>$team1</team1>
<team2>$team2</team2>
<date>$date</date>
<kickoff>$kickoff</kickoff>
<result>$result</result>\n";
my @paragraphs = split /<p/, $html;
foreach my $paragraph (@paragraphs) {
if ($paragraph =~ m/^>([\w\W]+?)<\/p>/g) {
$p .= "\t<p>" . $1 . "</p>\n";
$p =~ s/<a[\w\W]+?>//g;
$p =~ s/<strong\n[\w\W]+?>//g;
$p =~ s/<div\n[\w\W]+?>//g;
$p =~ s/<img\n[\w\W]+?<\/span>//g;
$p =~ s/<\/strong>//g;
$p =~ s/<\/div>//g;
$p =~ s/<\/span>//g;
$p =~ s/<\/a>//g;
$p =~ s/\t<p><\/p>\n//g;
$p =~ s/<br\n\/>//g;
$p =~ s/\t<p><strong>.+?\n//gi;
}
}
$p =~ s/&/&/g;
print OUT "\t<head>$head</head>\n" if defined $head;
print OUT "\t<teaser>$teaser</teaser>\n" if defined $teaser;
print OUT $p;
print OUT "</text>\n";
undef $p;
undef $team1;
undef $team2;
undef $date;
undef $kickoff;
undef $head;
sleep rand 3;
}
print OUT "</corpus>\n";
close OUT;