Skip to content

Instantly share code, notes, and snippets.

@merryt
Created January 21, 2019 00:37
Show Gist options
  • Select an option

  • Save merryt/94b0f9bc16fad91d7b1dc91954dc1b53 to your computer and use it in GitHub Desktop.

Select an option

Save merryt/94b0f9bc16fad91d7b1dc91954dc1b53 to your computer and use it in GitHub Desktop.
from requests import get
from requests.exceptions import RequestException
from contextlib import closing
from bs4 import BeautifulSoup
def simple_get(url):
"""
Attempts to get the content at `url` by making an HTTP GET request.
If the content-type of response is some kind of HTML/XML, return the
text content, otherwise return None.
"""
try:
with closing(get(url, stream=True)) as resp:
if is_good_response(resp):
return resp.content
else:
return None
except RequestException as e:
print('Error during requests to {0} : {1}'.format(url, str(e)))
return None
def is_good_response(resp):
"""
Returns True if the response seems to be HTML, False otherwise.
"""
content_type = resp.headers['Content-Type'].lower()
return (resp.status_code == 200
and content_type is not None
and content_type.find('html') > -1)
for i in range(1, 16):
raw_html = simple_get('https://www.scorespro.com/basketball/ajaxdata.php?country=usa&comp=nba&league=&season=2018-2019&status=results&page='+ str(i))
html = BeautifulSoup(raw_html, 'html.parser')
for data_block in html.findAll('div', attrs={'class': 'compgrp'}):
if (data_block.find('table')):
for table in data_block.findAll('table'):
total_score = int(float(table.find("td", attrs={'title':'Total Points'}).text.strip()))
if(total_score > 275):
overtime = table.find("td", attrs={'class':'status'}).text.strip()
awayteam = table.find("td", attrs={'class':'awayteam'}).text.strip()
hometeam = table.find("td", attrs={'class':'hometeam'}).text.strip()
print("-----")
print(table.find("td", attrs={'class':'kick_t'}).text.strip())
print ("{} vs {} scored {} in {}".format(hometeam, awayteam, total_score, overtime))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment