'Transfermarkt webscrapping failed
I have created the code below to scrape data from Transfermarkt but it gives error
ERROR
IndexError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_11132/2460347319.py in 2 3 #Let's look at the first name in the Players list. ----> 4 Players[0].text
IndexError: list index out of range
import requests
from bs4 import BeautifulSoup
import pandas as pd
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}
page = "https://www.transfermarkt.co.uk/transfers/transferrekorde/statistik/top/plus/0/galerie/0?saison_id=2000"
pageTree = requests.get(page, headers=headers)
pageSoup = BeautifulSoup(pageTree.content, 'html.parser')
Players = pageSoup.find_all("a", {"class": "spielprofil_tooltip"})
#Let's look at the first name in the Players list.
Players[0].text
Thanks
Solution 1:[1]
Your link has no tags 'a' with the specified class "spielprofil_tooltip".
So the python list is empty and the zero index has None in the value place.
Players = pageSoup.find_all("a", {"class": "spielprofil_tooltip"})
Players[0].text # convert a value that not exist raise a exception
Solution 2:[2]
In case you are still looking for scrapping player data from Transfermarkt, you can have a look at this class.
There are a few other datasets that the project is scraping that you may also be interested in, I'd suggest to have a look here.
Disclaimer: I wrote that scraper.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Franz Kurt |
Solution 2 | David |