
Greyhound Mystique
-
- Posts: 3140
- Joined: Sun Jan 31, 2010 8:06 pm
Just noticed that was almost a year ago and I haven't done anything with Python since
I'll try and take a look later as I could do with getting back into coding myself.

- MemphisFlash
- Posts: 2335
- Joined: Fri May 16, 2014 10:12 pm
never used python, i'm still getting to gips with power querys in excel.
is it easy to learn for an old'en like me.
is it easy to learn for an old'en like me.
-
- Posts: 88
- Joined: Wed Jan 22, 2020 1:13 pm
Code: Select all
import re
import requests
from bs4 import BeautifulSoup
from requests_html import HTMLSession
def extract_times(input):
times_regex = re.compile(r'Best: (.....)sLast: (.....)s')
best_times_regex = re.compile(r'Best: (.....)s')
match = times_regex.search(input)
best_match = best_times_regex.search(input)
if match:
if float(match.group(2)) < float(match.group(1)):
return float(match.group(2))
else:
return round((float(match.group(1)) + float(match.group(2))) / 2, 2)
return 100
def main():
session = HTMLSession()
baseUrl = "https://www.sportinglife.com"
results = []
res = requests.get("https://www.sportinglife.com/greyhounds/racecards")
soup = BeautifulSoup(res.text, "html.parser")
summary = list(filter(None, [link.find_parent('a', class_='') for link in soup.find_all('span', class_='gh-meeting-race-time')]))
x = 0
for tag in summary:
link = tag.get('href')
res = session.get(baseUrl + link)
soup = BeautifulSoup(res.text, "html.parser")
race = soup.find('h1').get_text()
grade = soup.find(class_='gh-racecard-summary-race-class gh-racecard-summary-always-open').get_text()
summary = soup.find_all(class_="gh-racing-runner-key-info-container")
Runners = dict()
for runner in summary:
Trap = runner.find(class_="gh-racing-runner-cloth").get_text()
Name = re.sub(r'\(.*\)', '', runner.find(class_="gh-racing-runner-greyhound-name").get_text())
Average_time = extract_times(runner.find(class_="gh-racing-runner-greyhound-sub-info").get_text())
Runners[Average_time] = Trap + '. ' + Name
if bool(Runners) == True and ('OR' in grade or 'A' in grade):
x = sorted(((k, v) for k, v in Runners.items()))
if (x[1][0] - x[0][0]) >= 0.1:
timeDiff = round((x[1][0] - x[0][0]), 2)
results.append(race + ', ' + x[0][1] + ', class ' + grade + ', time difference ' + str(timeDiff))
results.sort()
file = open('dogs_1.txt', mode='w')
for line in results: file.write(line+'\n')
file.close()
main()
-
- Posts: 3140
- Joined: Sun Jan 31, 2010 8:06 pm
Thanks, any idea why the old code had stopped working ?
-
- Posts: 88
- Joined: Wed Jan 22, 2020 1:13 pm
can't quite remember well because i've worked on the code last summer. but i remember that the code wasn't retrieving any race link from the main page in the first place.spreadbetting wrote: ↑Sat Dec 19, 2020 2:05 pmThanks, any idea why the old code had stopped working ?
- MemphisFlash
- Posts: 2335
- Joined: Fri May 16, 2014 10:12 pm
can you do a step by step guide on how to load it
-
- Posts: 3140
- Joined: Sun Jan 31, 2010 8:06 pm
You'd need to have python installed on your PC and all the relevant libraries, from there it's no more than opening the file and pressing run.
https://www.python.org/downloads/release/python-391/ your OS needs to be above win 7
If you want to instal it I can take you from there
-
- Posts: 88
- Joined: Wed Jan 22, 2020 1:13 pm
1. if you don't have python installed, download the installer from here https://www.python.org/downloads/
2. during the installation, make sure to check the "Add to PATH" checkbox.
3. open notepad, paste the code and save the file with ".py" extension (<something>.py)
4. open Command Prompt and change the working directory to where you have your python script: cd <your directory>
5. run the python script: python <your filename>
if it doesn't work try with "python3". if that doesn't work either, you probably haven't added python to PATH
6. wait 1/2 min until the program finishes.
7. check the new txt file that has appeared in the folder where the script is. the file is overwritten every time you run the script.
And that's the quickest way to run a python code, just by the command line. No additional software.
Example:
You do not have the required permissions to view the files attached to this post.
-
- Posts: 88
- Joined: Wed Jan 22, 2020 1:13 pm
Yes, I forgot to mention that you need to install requests and beautifulsoup4 libraries for the script to run. you can do that by typing "pip(or pip3) install beautifulsoup4, requests" command in the command promptspreadbetting wrote: ↑Sat Dec 19, 2020 3:44 pmYou'd need to have python installed on your PC and all the relevant libraries
I lost a lot of time installing the libraries and PATH but now it works thx vlad you are the menVladimir CC wrote: ↑Sat Dec 19, 2020 3:59 pm1. if you don't have python installed, download the installer from here https://www.python.org/downloads/
2. during the installation, make sure to check the "Add to PATH" checkbox.
3. open notepad, paste the code and save the file with ".py" extension (<something>.py)
4. open Command Prompt and change the working directory to where you have your python script: cd <your directory>
5. run the python script: python <your filename>
if it doesn't work try with "python3". if that doesn't work either, you probably haven't added python to PATH
6. wait 1/2 min until the program finishes.
7. check the new txt file that has appeared in the folder where the script is. the file is overwritten every time you run the script.
And that's the quickest way to run a python code, just by the command line. No additional software.
Example:
example.jpg



just returned a scrap line I thought it returned all the lines of the day

voilá
" 21:28 Hove Sat 19 December 2020, 4. Tommys Pluto, class OR, time difference 0.14 "
- MemphisFlash
- Posts: 2335
- Joined: Fri May 16, 2014 10:12 pm
so what have i done wrong
You do not have the required permissions to view the files attached to this post.
run this in to cmd
pip install beautifulsoup4
pip3 install beautifulsoup4
i tink you need the libraries thing ....
- MemphisFlash
- Posts: 2335
- Joined: Fri May 16, 2014 10:12 pm
getting this error now
You do not have the required permissions to view the files attached to this post.