-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrap.py
40 lines (32 loc) · 1.39 KB
/
scrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import requests
from bs4 import BeautifulSoup
import pandas as pd
players = 10000
url = f"https://www.yottachess.com/morePlayers?players={players}"
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', {'class': 'stable'})
rows = table.find_all('tr')
data = []
for row in rows:
columns = row.find_all('td')
if len(columns) > 0:
rank = columns[0].text.strip()
name = columns[1].text.strip()
rating = columns[2].text.strip()
country_img = columns[3].find('img')
country = " ".join(country_img['alt'].split()[1:]).upper() if country_img else ''
elo_classical = columns[4].text.strip()
elo_rapid = columns[5].text.strip()
elo_blitz = columns[6].text.strip()
year = columns[7].text.strip()
games = columns[8].text.strip()
data.append([rank, name, rating, country, elo_classical, elo_rapid, elo_blitz, year, games])
# Create a pandas dataframe
df = pd.DataFrame(data, columns=["Rank", "Name", "Rating", "Country", "Elo Classical", "Elo Rapid", "Elo Blitz", "Year", "Games"])
# Save the dataframe to CSV
df.to_csv("players.csv", index=False)
print("Data saved to players.csv")
else:
print(f"Error: {response.status_code}")