Repository navigation
Expand file tree
/
Copy pathcode.py
More file actions
60 lines (44 loc) · 1.69 KB
/
Copy pathcode.py
File metadata and controls
60 lines (44 loc) · 1.69 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import requests
from bs4 import BeautifulSoup
import re
import pandas as pd
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}
try:
response = requests.get("https://en.wikipedia.org/wiki/FIFA_World_Cup", headers=headers)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Failed to fetch the page: {e}")
exit()
soup = BeautifulSoup(response.text, "html.parser")
tables = soup.find_all("table", class_="wikitable")
if len(tables) < 2:
print("Couldn't find enough tables on the page. The page structure may have changed")
exit()
results_table = tables[1]
rows = results_table.find_all("tr")
tournaments = []
for row in rows[2:]:
cells = row.find_all(["td", "th"])
values = [cell.get_text(separator=" ", strip=True) for cell in cells]
if len(values) == 10 and values[3] != "":
tournaments.append(values)
def remove_footnotes(text):
return re.sub(r"\[.*?\]", "", text).strip()
def fix_spacing(text):
text = re.sub(r"\(\s+", "(", text)
text = re.sub(r"\s+\)", ")", text)
return text
column_names = ["edition", "year", "host", "champion", "final_score", "runner_up", "third_place", "third_place_score", "fourth_place", "teams"]
cleaned_tournaments = []
for values in tournaments:
cleaned_values = [fix_spacing(remove_footnotes(v)) for v in values]
record = dict(zip(column_names, cleaned_values))
cleaned_tournaments.append(record)
if len(cleaned_tournaments) == 0:
print("Warning: no tournaments were collected. Check the scraping logic.")
exit()
df = pd.DataFrame(cleaned_tournaments)
df.to_csv("world_cup_results.csv", index=False)
print("Saved successfully!")