首页 > 解决方案 > Changing a string to a float when doing web scraping Python

问题描述

I am trying to web scrape some income expense data from the Transfermarkt web page. I have made a for loop to check the data for multiple years. I am tried to turn the numerical data from a string to a float using the code below, but am getting an error message. Tried to use the code .text.rstrip(' €') to remove the €-symbol, but did not work. Any recommendations?

code: for year_url in years_url:

url = 'https://www.transfermarkt.com/premier-league/transfers/wettbewerb/GB1/plus/?saison_id=' + str(year_url) + '&s_w=&leihe=0&leihe=1&intern=0&intern=1'
response = requests.get(url, headers={'User-Agent': 'Custom5'})
financial_data = response.text
soup = BeautifulSoup(financial_data, 'html.parser')

grouped_data = soup.find('div', {'class':'transferbilanz'})

income = float(grouped_data.find_all('span', {'class': 'greentext'})[0].text.rstrip(' €').replace('.', '')) * 0.89
income_per_club = float(grouped_data.find_all('span', {'class': 'greentext'})[1].text.rstrip(' €').replace('.', '')) * 0.89
income_per_player = float(grouped_data.find_all('span', {'class': 'greentext'})[2].text.rstrip(' €').replace('.', '')) * 0.89
year = year_url

income_list.append(income)
income_per_club_list.append(income_per_club)
income_per_player_list.append(income_per_player)
year_list.append(year) 

Output:


ValueError                                Traceback (most recent call last)
<ipython-input-20-8acf4def8ed4> in <module>
      8     grouped_data = soup.find('div', {'class':'transferbilanz'})
      9 
---> 10     income = float(grouped_data.find_all('span', {'class': 'greentext'})[0].text.rstrip(' €').replace('.', '')) * 0.89
     11     income_per_club = float(grouped_data.find_all('span', {'class': 'greentext'})[1].text.rstrip(' €').replace('.', '')) * 0.89
     12     income_per_player = float(grouped_data.find_all('span', {'class': 'greentext'})[2].text.rstrip(' €').replace('.', '')) * 0.89

**ValueError: could not convert string to float: '€178,300,000'**

标签: pythonweb-scraping

解决方案



推荐阅读