Skip to content

Commit f08c95d

Browse files
committed
scraping data for f1 teams
1 parent abb9477 commit f08c95d

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

src/links/links.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
interface Links {
22
drivers: string
33
teams: string
4-
54
}
65

76
export const links: Links = {

src/scraper/driver-data.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface drivers {
1212
driverImage: string | undefined
1313
}
1414

15-
export function driverData() {
15+
export function getDriverData() {
1616

1717
let driversArray: drivers[] = []
1818

@@ -39,14 +39,9 @@ export function driverData() {
3939
nationalityImage,
4040
driverImage
4141
}
42-
4342
driversArray.push(driversObj)
44-
4543
}
46-
}
47-
)
48-
44+
})
4945
console.log(driversArray)
50-
5146
})
5247
}

src/scraper/team-data.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import axios from "axios";
2+
import cheerio from "cheerio"
3+
4+
import { links } from "../links/links"
5+
6+
interface teams {
7+
name: string
8+
drivers: string[]
9+
points: number
10+
rank: number
11+
carLogo: string | undefined
12+
carImage: string | undefined
13+
}
14+
15+
export function getTeamsData() {
16+
17+
let teamsArray: teams[] = []
18+
19+
axios(links.teams).then(response => {
20+
const html = response.data
21+
const $ = cheerio.load(html)
22+
23+
$('fieldset').each(
24+
function () {
25+
const name: string = $(this).find(' div:nth-child(1) > .listing-info > .name.f1-bold--m > .f1-color--black').text()
26+
const points: number = parseInt($(this).find('.points > div:nth-child(1)').text())
27+
const rank: number = parseInt($(this).find('.rank').text())
28+
29+
const driver1_0: string = $(this).find(' div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span:nth-child(1)').text()
30+
const driver1_1: string = $(this).find(' div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span:nth-child(2)').text()
31+
const driver2_0: string = $(this).find('div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > span:nth-child(1)').text()
32+
const driver2_1: string = $(this).find('div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > span:nth-child(2)').text()
33+
const driver1 = driver1_0.concat(" ", driver1_1)
34+
const driver2 = driver2_0.concat(" ", driver2_1)
35+
const drivers: string[] = [driver1, driver2]
36+
37+
const carLogo: string | undefined = $(this).find('div:nth-child(1) > div:nth-child(2) > div:nth-child(2) > picture:nth-child(1) > img:nth-child(2)').attr('data-src')
38+
const carImage: string | undefined = $(this).find('div:nth-child(1) > div:nth-child(4) > picture:nth-child(1) > img:nth-child(5)').attr('data-src')
39+
40+
if (name.length !== 0 && points !== NaN && rank !== NaN) {
41+
const teamObj: teams = {
42+
name,
43+
drivers,
44+
points,
45+
rank,
46+
carLogo,
47+
carImage,
48+
}
49+
teamsArray.push(teamObj)
50+
}
51+
})
52+
console.log(teamsArray)
53+
})
54+
}

src/server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
import { driverData } from "./scraper/driver-data";
1+
import { getDriverData } from "./scraper/driver-data";
2+
import { getTeamsData } from "./scraper/team-data";
23

3-
driverData();
4+
// getDriverData();
5+
// getTeamsData()

0 commit comments

Comments
 (0)