python - 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'**
解决方案
推荐阅读
- python - Python:请求 URL 并将 JSON 数据返回到脚本
- wear-os - 活动停止后保持服务活动
- bash - 我有一个需要在目录中搜索的键列表,然后打印未找到的键
- ios - 使用 SwiftPM 库归档 Xcode 项目失败
- python - 了解 Python 多重赋值
- nativescript - 以编程方式为 NativeScript Vue 中的标签设置“跨度”颜色在 FormattedString 中不起作用
- bash - 如何确定 X 在哪个显示器上呈现程序
- java - 在 IntelliJ 中为 AWS Lambda 应用程序运行 Docker:Docker 引发 404 客户端错误 (docker.errors.ImageNotFound)
- parsing - 将字符串解析为 Ast
- python - 如何使用 split() 命令来计算 txt 中的特定单词