将分数的字符串列表从刮掉的html/最干净的方法转换为浮点数

converting string list of fractions into float from scraped html / cleanest method

正在尝试将这个抓取的赔率分数字符串列表转换为相同格式的浮点数列表,以用于计算。

import requests
from bs4 import BeautifulSoup

url = requests.get('http://www.oddschecker.com/tennis/match-coupon')
html = url.content
soup = BeautifulSoup(html)

for row in soup.find_all("tr", {"data-market-id": True}):
    participants = [item.get_text(strip=True) for item in row.find_all('span', class_='fixtures-bet-name')]
    odds = [item.get_text(strip=True) for item in row.find_all('span', class_='odds')]

    print (participants[0], odds[0], participants[1], odds[1])
def convert(item):
    ls = list(map(int, item.strip('()').split('/')))
    l = len(ls)
    if l == 1:
        return ls[0]
    elif l == 2:
        a, b = ls
        return float(a) / b if b else 0
    raise RuntimeError('More than 2 values!')

然后调用:

odds = [convert(item.get_text(strip=True)) for item in row.find_all('span', class_='odds')]