0

Trying to convert this scraped string list of odds fractions into a list of float decimals in the same format for use in calculations.

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])
Twiggy Garcia
  • 37
  • 1
  • 7

1 Answers1

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

Then call:

odds = [convert(item.get_text(strip=True)) for item in row.find_all('span', class_='odds')]
ferhatelmas
  • 3,818
  • 1
  • 21
  • 25
  • when implemented that returns 'Constanza Vega Nadia Podoroska ' which is only 1 match, not the entire table. – Twiggy Garcia Mar 23 '15 at 14:11
  • @TwiggyGarcia sorry there was a typo, edited. could you please check again? – ferhatelmas Mar 23 '15 at 14:33
  • when i try and integrate it gives me "ValueError: need more than 1 value to unpack" – Twiggy Garcia Mar 23 '15 at 14:42
  • @TwiggyGarcia Then, there are values that don't conform to the structure we expect. For example, when splitted, it's expected to be two values but may not be there. Let me make function more robust. – ferhatelmas Mar 23 '15 at 14:44
  • Traceback (most recent call last): line 22, in odds = [convert(item.get_text(strip=True)) for item in row.find_all('span', class_='odds')] ", line 22, in odds = [convert(item.get_text(strip=True)) for item in row.find_all('span', class_='odds')] File ", line 13, in convert l = len(ls) TypeError: object of type 'map' has no len() – Twiggy Garcia Mar 23 '15 at 15:08
  • @TwiggyGarcia last change for py3 since map returns iterator instead of list in py3. Wrapped map via list to convert it into a list. – ferhatelmas Mar 23 '15 at 15:14
  • this edit gives me a float decimal value for only 1 match 'Constanza Vega 3 Nadia Podoroska 0.2222222222222222' i still can't return the original list with decimals as a float – Twiggy Garcia Mar 23 '15 at 15:20
  • @TwiggyGarcia original is `['(3)', '(2/9)']` so what's the problem. – ferhatelmas Mar 23 '15 at 15:38
  • i have edited my code - this video will show you what i'm talking about - http://we.tl/N3mYZo0UGy – Twiggy Garcia Mar 23 '15 at 16:11
  • @TwiggyGarcia hadn't seen the video, no problem. Result is good to hear! – ferhatelmas Mar 23 '15 at 17:02