将碎片列表的馏分转换为浮动的刮取的html / cleanest方法

时间:2015-03-22 13:47:41

标签: python html web-scraping beautifulsoup python-3.4

尝试将此碎片的赔率分数列表转换为相同格式的浮点小数列表,以便在计算中使用。

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])

1 个答案:

答案 0 :(得分:0)

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')]