File tree Expand file tree Collapse file tree 6 files changed +79
-7
lines changed Expand file tree Collapse file tree 6 files changed +79
-7
lines changed Original file line number Diff line number Diff line change 1
1
# F1 API
2
2
3
+ The following scraper scrapes this given [ website] ( https://www.formula1.com/en.html )
4
+
5
+ ## Functions added
6
+
7
+ - getDriverData
8
+ Fetch the current list of F1 drivers along with their information
9
+
10
+ - getTeamsData
11
+ Fetch the current list of F1 teams along with their information
12
+
13
+ - getDriverStandings
14
+ Fetch F1 driver standings from points table
Original file line number Diff line number Diff line change 1
- interface Links {
1
+ interface staticLinks {
2
2
drivers : string
3
3
teams : string
4
4
}
5
5
6
- export const links : Links = {
6
+ interface dynamicLinks {
7
+ driverStanding_1 : string
8
+ driverStanding_2 : string
9
+ }
10
+
11
+ export const staticLinks : staticLinks = {
7
12
drivers : "https://www.formula1.com/en/drivers.html" ,
8
- teams : "https://www.formula1.com/en/teams.html"
13
+ teams : "https://www.formula1.com/en/teams.html" ,
14
+ }
15
+
16
+ export const dynamicLinks : dynamicLinks = {
17
+ driverStanding_1 : "https://www.formula1.com/en/results.html" ,
18
+ driverStanding_2 : "drivers.html"
9
19
}
Original file line number Diff line number Diff line change 1
1
import axios from 'axios'
2
2
import cheerio from 'cheerio'
3
3
4
- import { links } from ' ../links/links'
4
+ import { staticLinks } from " ../links/links" ;
5
5
6
6
interface drivers {
7
7
name : string
@@ -16,7 +16,7 @@ export function getDriverData() {
16
16
17
17
let driversArray : drivers [ ] = [ ]
18
18
19
- axios ( links . drivers ) . then ( response => {
19
+ axios ( staticLinks . drivers ) . then ( response => {
20
20
const html = response . data
21
21
const $ = cheerio . load ( html )
22
22
Original file line number Diff line number Diff line change
1
+ import axios from "axios"
2
+ import cheerio from "cheerio"
3
+
4
+ import { dynamicLinks } from "../links/links"
5
+
6
+ interface driverStanding {
7
+ position : number
8
+ driver : string
9
+ nationality : string
10
+ car : string
11
+ points : number
12
+ }
13
+
14
+ export function getDriverStandings ( year : number ) {
15
+
16
+ let driverStandingArray : driverStanding [ ] = [ ]
17
+
18
+ axios ( `${ dynamicLinks . driverStanding_1 } /${ year } /${ dynamicLinks . driverStanding_2 } ` ) . then (
19
+ response => {
20
+ const html = response . data
21
+ const $ = cheerio . load ( html )
22
+ //
23
+ $ ( 'tr' ) . each (
24
+ function ( ) {
25
+ const position : number = parseInt ( $ ( this ) . find ( ' td:nth-child(2) ' ) . text ( ) )
26
+
27
+ const firstName : string = $ ( this ) . find ( ' a.dark.bold.ArchiveLink > span:nth-child(1)' ) . text ( )
28
+ const lastName : string = $ ( this ) . find ( ' a.dark.bold.ArchiveLink > span:nth-child(2)' ) . text ( )
29
+
30
+ const driver = firstName . concat ( " " , lastName )
31
+ const nationality : string = $ ( this ) . find ( ' td.dark.semi-bold.uppercase' ) . text ( )
32
+ const car : string = $ ( this ) . find ( 'td:nth-child(5) > a.grey.semi-bold.uppercase.ArchiveLink' ) . text ( )
33
+ const points : number = parseInt ( $ ( this ) . find ( ' td:nth-child(6) ' ) . text ( ) )
34
+
35
+ if ( position !== NaN && points !== NaN && driver . length !== 0 && nationality . length !== 0 && car . length !== 0 ) {
36
+ const driverStandingsObj : driverStanding = {
37
+ position,
38
+ driver,
39
+ nationality,
40
+ car,
41
+ points
42
+ }
43
+ driverStandingArray . push ( driverStandingsObj )
44
+ }
45
+ } )
46
+ console . log ( driverStandingArray )
47
+ } )
48
+ }
Original file line number Diff line number Diff line change 1
1
import axios from "axios" ;
2
2
import cheerio from "cheerio"
3
3
4
- import { links } from "../links/links"
4
+ import { staticLinks } from "../links/links" ;
5
5
6
6
interface teams {
7
7
name : string
@@ -16,7 +16,7 @@ export function getTeamsData() {
16
16
17
17
let teamsArray : teams [ ] = [ ]
18
18
19
- axios ( links . teams ) . then ( response => {
19
+ axios ( staticLinks . teams ) . then ( response => {
20
20
const html = response . data
21
21
const $ = cheerio . load ( html )
22
22
Original file line number Diff line number Diff line change 1
1
import { getDriverData } from "./scraper/driver-data" ;
2
2
import { getTeamsData } from "./scraper/team-data" ;
3
+ import { getDriverStandings } from "./scraper/driver-standings" ;
3
4
4
5
// getDriverData();
5
6
// getTeamsData()
7
+ // getDriverStandings(2022)
You can’t perform that action at this time.
0 commit comments