Beautifulsoap

Peter Otten __peter__ at web.de
Wed Mar 16 13:05:56 EDT 2016


eproser at gmail.com wrote:

> 
> Yes, for my hobby i want extract odds.
> 
> The code is:
> 
> from bs4 import BeautifulSoup
> 
> import urllib2
> 
> url =
> "http://www.betexplorer.com/soccer/england/premier-league-2014-2015/results/"
> 
> content = urllib2.urlopen(url).read()
> 
> soup = BeautifulSoup(content)
> 
> odds = soup.find_all("td", class_="odds")
> for odd in odds:
>   print odd
> 
> And I have this list of object:
> 
> ...
> <td class="odds" data-odd="3.70"></td>
> <td class="odds" data-odd="3.65"></td>
> <td class="odds" data-odd="3.48"></td>
> 
> From this object I'm interesting only 3.70 3.65 3.48 etc
> 
> I'm no a programmer, my hobby is analyze soccer matchs to try to win :-)
> I chose python because I think is the best :-)
> 
> Thank you in advance

The interactive interpreter is a good tool to try to access various elements 
in the "soup":

$ python -i yourcode.py # enter the interpreter when your script terminates
[snip] 
<td class="odds" data-odd="3.48"></td>
<td class="odds best-betrate" data-odd="2.04"></td>
>>> 
>>> odd
<td class="odds best-betrate" data-odd="2.04"></td>
>>> odd.attrs["data-odd"]
'2.04'

If you want to do calculations you have to convert the string to a number 
first:

>>> float(odd.attrs["data-odd"])
2.04

You probably want the match, too, so it may be better to go for the <tr> 
elements in the first table:

>>> for tr in soup.table.find_all("tr", class_="strong"):                                         
...     print tr.find(class_="first-cell").string,
...     print float(tr.find(class_="odds").attrs["data-odd"]) 
... 
Aston Villa - Burnley 2.2
Crystal Palace - Swansea 1.81
Hull City - Manchester United 3.1
[snip]





More information about the Python-list mailing list