I'm trying to web scrape from this link, https://www.premierleague.com/stats/top/players/goals?se=-1 And I don't seem to be successful.
Below is the code I tried.
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
import csv`
url = "https://www.premierleague.com/stats/top/players/goals?se=-1"
html = urlopen(url)
bs = BeautifulSoup(html, 'html.parser')
#print(bs)
listings = []
for rows in bs.find_all("tr"):
if("oddrow" in rows["class"]) or ("evenrow" in rows["class"]):
name = rows.find("div", class_="playerName").a.get_text()
country = rows.find_all("td")[1].get_text()
goals = rows.find_all("td")[4].get_text()
listings.append([name, country, goals])
with open("EPL_TEST.csv", 'a', encoding = 'utf-8') as toWrite:
writer = csv.writer(toWrite)
writer.writerows(listings)
print("Data Fetched")
This is the error I'm getting: C:\Users\Siddhardh\Desktop\Python\Projects\FinalProject\venv\Scripts\python.exe C:/Users/Siddhardh/Desktop/Python/Projects/FinalProject/Scraping.py
Traceback (most recent call last): File "C:/Users/Siddhardh/Desktop/Python/Projects/FinalProject/Scraping.py", line 16, in <module>
if("oddrow" in rows["class"]) or ("evenrow" in rows["class"]): File "C:\Users\Siddhardh\Desktop\Python\Projects\FinalProject\venv\lib\site-packages\bs4\element.py", line 1016, in __getitem__ return self.attrs[key] KeyError: 'class'
Process finished with exit code 1
I need to get the names, country and goals of all the players into a CSV file.
P.S. Pardon my editing skills. This is my first post here. I'll learn.