Skip to content

Instantly share code, notes, and snippets.

@briandfoy
Created July 18, 2018 14:53
Show Gist options
  • Select an option

  • Save briandfoy/4260c48c42999be0721d34c5418a6774 to your computer and use it in GitHub Desktop.

Select an option

Save briandfoy/4260c48c42999be0721d34c5418a6774 to your computer and use it in GitHub Desktop.
Grab all the MLB stadium names
#!/Users/brian/bin/perls/perl-latest
use v5.28;
use Mojo::DOM;
use Mojo::UserAgent;
my $data = do { local $/; <DATA> };
my $url = 'http://m.mlb.com/teams/';
my $tx = Mojo::UserAgent->new->get( $url );
unless( $tx->success ) {
die "Could not fetch page!";
}
my @layout_div = $tx->result->dom
->find( 'section.league-container' )
->each;
my %teams;
foreach my $layout ( @layout_div ) {
# the league information is in an H4
# (although it has lots of H3s under it)
my $league = $layout->at( 'h4' )->text;
# Each team is in an LI
my @teams = $layout->find( 'li' )
->map( sub {
my $dom = $_;
my %selects = (
team => 'h3',
stadium => 'span.venue-name',
address => 'p > span.overBlue',
);
my %results = map {
$_ => eval { $dom->at( $selects{$_} )->all_text }
} keys %selects;
$results{address} =~ s/\A\s+//;
$results{address} =~ s/\v/, /g;
\%results;
} )
->each;
$teams{$league} = \@teams;
}
use Mojo::Util qw(dumper);
say dumper( \%teams );
__END__
{
"American League" => [
{
"address" => "333 West Camden Street, Baltimore, MD 21201",
"stadium" => "Oriole Park at Camden Yards",
"team" => "Baltimore Orioles"
},
{
"address" => "4 Yawkey Way, Boston, MA 2215",
"stadium" => "Fenway Park",
"team" => "Boston Red Sox"
},
{
"address" => "333 West 35th Street, Chicago, IL 60616",
"stadium" => "Guaranteed Rate Field",
"team" => "Chicago White Sox"
},
{
"address" => "2401 Ontario Street, Cleveland, OH 44115",
"stadium" => "Progressive Field",
"team" => "Cleveland Indians"
},
{
"address" => "2100 Woodward Avenue, Detroit, MI 48201",
"stadium" => "Comerica Park",
"team" => "Detroit Tigers"
},
{
"address" => "501 Crawford Street, Houston, TX 77002",
"stadium" => "Minute Maid Park",
"team" => "Houston Astros"
},
{
"address" => "One Royal Way, Kansas City, MO 64129",
"stadium" => "Kauffman Stadium",
"team" => "Kansas City Royals"
},
{
"address" => "2000 Gene Autry Way, Anaheim, CA 92806",
"stadium" => "Angel Stadium",
"team" => "Los Angeles Angels of Anaheim"
},
{
"address" => "1 Twins Way, Minneapolis, MN 55403",
"stadium" => "Target Field",
"team" => "Minnesota Twins"
},
{
"address" => "One East 161st Street, Bronx, NY 10451",
"stadium" => "Yankee Stadium",
"team" => "New York Yankees"
},
{
"address" => "7000 Coliseum Way, Oakland, CA 94621",
"stadium" => "Oakland Coliseum",
"team" => "Oakland Athletics"
},
{
"address" => "P.O. Box 4100, Seattle, WA 98104",
"stadium" => "Safeco Field",
"team" => "Seattle Mariners"
},
{
"address" => "One Tropicana Drive, St. Petersburg, FL 33705",
"stadium" => "Tropicana Field",
"team" => "Tampa Bay Rays"
},
{
"address" => "1000 Ballpark Way, Arlington, TX 76011",
"stadium" => "Globe Life Park in Arlington",
"team" => "Texas Rangers"
},
{
"address" => "1 Blue Jays Way, Suite 3200, Toronto, Ontario, M5V1J1",
"stadium" => "Rogers Centre",
"team" => "Toronto Blue Jays"
}
],
"National League" => [
{
"address" => "401 East Jefferson Street, Phoenix, AZ 85004",
"stadium" => "Chase Field",
"team" => "Arizona Diamondbacks"
},
{
"address" => "755 Battery Avenue, Atlanta, GA 30339",
"stadium" => "SunTrust Park",
"team" => "Atlanta Braves"
},
{
"address" => "1060 West Addison, Chicago, IL 60613-4397",
"stadium" => "Wrigley Field",
"team" => "Chicago Cubs"
},
{
"address" => "100 Main Street, Cincinnati, OH 45202-4109",
"stadium" => "Great American Ball Park",
"team" => "Cincinnati Reds"
},
{
"address" => "2001 Blake Street, Denver, CO 80205-2000",
"stadium" => "Coors Field",
"team" => "Colorado Rockies"
},
{
"address" => "1000 Vin Scully Avenue, Los Angeles, CA 90012-1199",
"stadium" => "Dodger Stadium",
"team" => "Los Angeles Dodgers"
},
{
"address" => "501 Marlins Way, Miami, FL 33125",
"stadium" => "Marlins Park",
"team" => "Miami Marlins"
},
{
"address" => "One Brewers Way, Milwaukee, WI 53214",
"stadium" => "Miller Park",
"team" => "Milwaukee Brewers"
},
{
"address" => "Citi Field, Flushing, NY 11368",
"stadium" => "Citi Field",
"team" => "New York Mets"
},
{
"address" => "One Citizens Bank Way, Philadelphia, PA 19148",
"stadium" => "Citizens Bank Park",
"team" => "Philadelphia Phillies"
},
{
"address" => "115 Federal Street, Pittsburgh, PA 15212",
"stadium" => "PNC Park",
"team" => "Pittsburgh Pirates"
},
{
"address" => "100 Park Boulevard, San Diego, CA 92101",
"stadium" => "Petco Park",
"team" => "San Diego Padres"
},
{
"address" => "24 Willie Mays Plaza, San Francisco, CA 94107",
"stadium" => "AT&T Park",
"team" => "San Francisco Giants"
},
{
"address" => "700 Clark Street, St. Louis, MO 63102",
"stadium" => "Busch Stadium",
"team" => "St. Louis Cardinals"
},
{
"address" => "1500 South Capitol Street, SE, Washington, DC 20003-1507",
"stadium" => "Nationals Park",
"team" => "Washington Nationals"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment