Skip to content

Instantly share code, notes, and snippets.

@nyamsprod
Last active September 29, 2023 07:13
Show Gist options
  • Save nyamsprod/8beee44afedf0413f7c18c2d236e2629 to your computer and use it in GitHub Desktop.
Save nyamsprod/8beee44afedf0413f7c18c2d236e2629 to your computer and use it in GitHub Desktop.
using Html Table to parse a bbc table
┌────────────────┬───┬───┬───┬───┬────┬────┬─────┬─────┬──────┐
│ Team │ P │ W │ D │ L │ F │ A │ GD │ Pts │ Rank │
├────────────────┼───┼───┼───┼───┼────┼────┼─────┼─────┼──────┤
│ Man City │ 6 │ 6 │ 0 │ 0 │ 16 │ 3 │ 13 │ 18 │ 1 │
│ Liverpool │ 6 │ 5 │ 1 │ 0 │ 15 │ 5 │ 10 │ 16 │ 2 │
│ Brighton │ 6 │ 5 │ 0 │ 1 │ 18 │ 8 │ 10 │ 15 │ 3 │
│ Tottenham │ 6 │ 4 │ 2 │ 0 │ 15 │ 7 │ 8 │ 14 │ 4 │
│ Arsenal │ 6 │ 4 │ 2 │ 0 │ 11 │ 6 │ 5 │ 14 │ 5 │
│ Aston Villa │ 6 │ 4 │ 0 │ 2 │ 12 │ 10 │ 2 │ 12 │ 6 │
│ West Ham │ 6 │ 3 │ 1 │ 2 │ 11 │ 10 │ 1 │ 10 │ 7 │
│ Newcastle │ 6 │ 3 │ 0 │ 3 │ 16 │ 7 │ 9 │ 9 │ 8 │
│ Man Utd │ 6 │ 3 │ 0 │ 3 │ 7 │ 10 │ -3 │ 9 │ 9 │
│ Crystal Palace │ 6 │ 2 │ 2 │ 2 │ 6 │ 7 │ -1 │ 8 │ 10 │
│ Fulham │ 6 │ 2 │ 2 │ 2 │ 5 │ 10 │ -5 │ 8 │ 11 │
│ Nottm Forest │ 6 │ 2 │ 1 │ 3 │ 7 │ 9 │ -2 │ 7 │ 12 │
│ Brentford │ 6 │ 1 │ 3 │ 2 │ 9 │ 9 │ 0 │ 6 │ 13 │
│ Chelsea │ 6 │ 1 │ 2 │ 3 │ 5 │ 6 │ -1 │ 5 │ 14 │
│ Everton │ 6 │ 1 │ 1 │ 4 │ 5 │ 10 │ -5 │ 4 │ 15 │
│ Wolves │ 6 │ 1 │ 1 │ 4 │ 6 │ 12 │ -6 │ 4 │ 16 │
│ Bournemouth │ 6 │ 0 │ 3 │ 3 │ 5 │ 11 │ -6 │ 3 │ 17 │
│ Luton │ 5 │ 0 │ 1 │ 4 │ 3 │ 11 │ -8 │ 1 │ 18 │
│ Burnley │ 5 │ 0 │ 1 │ 4 │ 4 │ 13 │ -9 │ 1 │ 19 │
│ Sheff Utd │ 6 │ 0 │ 1 │ 5 │ 5 │ 17 │ -12 │ 1 │ 20 │
└────────────────┴───┴───┴───┴───┴────┴────┴─────┴─────┴──────┘
<?php
declare(strict_types=1);
use Bakame\HtmlTable\Parser;
use Bakame\HtmlTable\Section;
use Symfony\Component\CssSelector\CssSelectorConverter;
use function Laravel\Prompts\table;
require 'vendor/autoload.php';
$table = Parser::new()
->excludeAllSections()
->includeSection(Section::tbody)
->tableXPathPosition((new CssSelectorConverter())->toXPath('.gs-o-table'))
->tableHeader([
2 => 'Team',
3 => 'P',
4 => 'W',
5 => 'D',
6 => 'L',
7 => 'F',
8 => 'A',
9 => 'GD',
10 => 'Pts',
0 => 'Rank',
])
->parseFile('https://www.bbc.com/sport/football/tables')
;
table($table->getHeader(), [...$table]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment