Last active
March 28, 2023 12:10
-
-
Save MrBliz/36bd1885e983236e5373743df57d8e99 to your computer and use it in GitHub Desktop.
An F# Script to scrape the results of a football team from the 11v11 website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "nuget: FSharp.Data, 5.0.2" | |
open System | |
open FSharp.Data | |
type Result = | |
{ Date: DateOnly | |
Match: string | |
HomeScore: int | |
AwayScore: int | |
HomeOrAway: string | |
Competition: string } | |
let doc = | |
HtmlDocument.Load("https://www.11v11.com/teams/sheffield-united/tab/matches/season/") | |
let teamName = "Sheffield United" | |
let homeScore = 5 | |
let awayScore = 2 | |
let homeOrAway = "H" | |
let seasonLinks: string list = | |
let listItems = doc.CssSelect("ul#season li a") | |
Console.WriteLine $"{listItems |> List.length} Seasons Found" | |
listItems | |
|> List.map (fun n -> n.Attribute("href") |> (fun a -> a.Value().ToString())) | |
let season (row: string) = | |
let seasonDoc = HtmlDocument.Load(row) | |
seasonDoc.CssSelect("#pageContent table tr") | |
let filterRow (row: HtmlNode) = | |
let tds = row.Descendants["td"] | |
not (Seq.isEmpty tds) | |
let extractResult (row: HtmlNode) = | |
let tds = row.Descendants["td"] | |
let date = tds |> Seq.item 0 |> (fun x -> DateOnly.Parse(x.InnerText().ToString())) | |
let game = tds |> Seq.item 1 |> (fun x -> x.InnerText()) | |
let score = | |
tds | |
|> Seq.item 3 | |
|> (fun x -> x.InnerText()) | |
|> (fun x -> | |
let index = x.IndexOf('-') | |
let first = x.Substring(0, index) | |
let second = x.Substring(index + 1) | |
let trimmed = second.Split(' ', '\r', '\n') | |
(int (first), int (trimmed[0]))) | |
let competition = tds |> Seq.item 4 |> (fun x -> x.InnerText()) | |
let teams = game.ToString().Split('v', StringSplitOptions.TrimEntries) | |
let homeOrAway = if (teams[0] = teamName) then "H" else "A" | |
let result = | |
{ Result.Date = date | |
Match = game | |
HomeOrAway = homeOrAway | |
HomeScore = fst score | |
AwayScore = snd score | |
Competition = competition } | |
result | |
let results = | |
seasonLinks | |
|> List.collect season | |
|> List.filter filterRow | |
|> List.map extractResult | |
|> List.filter (fun x -> x.HomeScore = homeScore && x.AwayScore = awayScore && x.HomeOrAway = homeOrAway) | |
|> List.iter (fun x -> printfn $"{x.Date} : {x.Match} : {x.HomeScore}:{x.AwayScore} : {x.Competition}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment