Skip to content

Commit cce61d5

Browse files
committed
scraping data from driver standings table and readme updated
1 parent f08c95d commit cce61d5

File tree

6 files changed

+79
-7
lines changed

6 files changed

+79
-7
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
# F1 API
22

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

src/links/links.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
interface Links {
1+
interface staticLinks {
22
drivers: string
33
teams: string
44
}
55

6-
export const links: Links = {
6+
interface dynamicLinks {
7+
driverStanding_1: string
8+
driverStanding_2: string
9+
}
10+
11+
export const staticLinks: staticLinks = {
712
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"
919
}

src/scraper/driver-data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import axios from 'axios'
22
import cheerio from 'cheerio'
33

4-
import { links } from '../links/links'
4+
import { staticLinks } from "../links/links";
55

66
interface drivers {
77
name: string
@@ -16,7 +16,7 @@ export function getDriverData() {
1616

1717
let driversArray: drivers[] = []
1818

19-
axios(links.drivers).then(response => {
19+
axios(staticLinks.drivers).then(response => {
2020
const html = response.data
2121
const $ = cheerio.load(html)
2222

src/scraper/driver-standings.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

src/scraper/team-data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import axios from "axios";
22
import cheerio from "cheerio"
33

4-
import { links } from "../links/links"
4+
import { staticLinks } from "../links/links";
55

66
interface teams {
77
name: string
@@ -16,7 +16,7 @@ export function getTeamsData() {
1616

1717
let teamsArray: teams[] = []
1818

19-
axios(links.teams).then(response => {
19+
axios(staticLinks.teams).then(response => {
2020
const html = response.data
2121
const $ = cheerio.load(html)
2222

src/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { getDriverData } from "./scraper/driver-data";
22
import { getTeamsData } from "./scraper/team-data";
3+
import { getDriverStandings } from "./scraper/driver-standings";
34

45
// getDriverData();
56
// getTeamsData()
7+
// getDriverStandings(2022)

0 commit comments

Comments
 (0)