Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ python
- # -*- coding: UTF-8 -*-
- import requests
- import browser_cookie3
- from bs4 import BeautifulSoup
- import pytz
- from datetime import datetime
- import lxml.html as lh
- def good_timezone_converter(input_dt, current_tz='UTC', target_tz='Europe/Budapest'):
- current_tz = pytz.timezone(current_tz)
- target_tz = pytz.timezone(target_tz)
- target_dt = current_tz.localize(input_dt).astimezone(target_tz)
- return target_tz.normalize(target_dt)
- def date_convert(old_date):
- c_date = datetime.strptime(old_date, '%H:%M')
- old_timezone = pytz.timezone("UTC")
- new_timezone = pytz.timezone("Europe/Budapest")
- my_date_in_local_timezone = old_timezone.localize(c_date).astimezone(new_timezone)
- return my_date_in_local_timezone.strftime('%H:%M')
- def poor_time_converter(old_time):
- hour = int(old_time[:2]) + 1
- if hour < 10:
- return '0' + str(hour) + old_time[2:5]
- elif hour > 23:
- return '0' + str(hour - 24) + old_time[2:5]
- return str(hour) + old_time[2:5]
- def table_index_find(tree):
- path = '//*[@id="container"]/div[5]/table[1]/tr[1]/td'
- tables = tree.xpath(path)[0]
- label = tables[0].getchildren()[0].getchildren()[0].getchildren()[0].text
- if label == 'Tables':
- index = '2'
- else:
- index = '1'
- return index
- def table_content(url, table_id):
- # print(url)
- response = requests.get(url, cookies=cj)
- tree = lh.fromstring(response.text)
- league_table_xpath = '//*[@id="container"]/div[5]/table[1]/tr[1]/td/table[' + \
- table_index_find(tree) + ']/tr/td[' + str(table_id) + ']/table'
- table = tree.xpath(league_table_xpath)
- home_rank = 0
- away_rank = 0
- team_home = ''
- team_away = ''
- megfelel = ' ERROR'
- if response.ok:
- for i in range(2, len(table[0])):
- table_children = table[0][i].getchildren()
- team = table_children[1].text.strip()
- gp = table_children[2][0].text
- pts = table_children[3][0].text.replace('\r\n', '')
- if team == hazai:
- team_home = ' (HOME rank:' + str(i-1) + ' gp:' + str(gp) + ' pts:' + str(pts) + ' / '
- home_rank = i-1
- if team == vendeg:
- team_away = 'AWAY rank:' + str(i-1) + ' gp:' + str(gp) + ' pts:' + str(pts) + ')'
- away_rank = i-1
- if home_rank < away_rank:
- megfelel = ' IGEN'
- else:
- megfelel = ''
- return team_home + team_away + megfelel
- def stat_link_find(act_data, table_id):
- result = ''
- for field in field_list:
- data = act_data.contents[field]
- if len(data) != 0:
- if data.contents[0].text == 'stats':
- stat_url = 'http://www.soccerstats.com/' + data.contents[0].attrs['href']
- result = table_content(stat_url.encode(encoding='UTF-8', errors='strict'), table_id)
- return result
- def stats_find():
- a = 0
- for link in g_data:
- for field in field_list:
- data = link.contents[field]
- if len(data) != 0:
- if data.contents[0].text == 'stats':
- a += 1
- return a
- cj = browser_cookie3.load()
- r = requests.get("http://www.soccerstats.com/matches.asp?matchday=6", cookies=cj)
- soup = BeautifulSoup(r.content, 'lxml')
- g_data = soup.find_all("tr", {"class": "trow8"})
- # league_table_xpath = '//*[@id="container"]/div[5]/table[1]/tr[1]/td/table[2]/tr/td[1]/table'
- field_list = (7, 8, 9)
- for item in g_data:
- datum = item.contents[0].contents[0].text
- kezdes = item.contents[0].contents[2].text.strip()
- liga = item.contents[2].text
- hazai = item.contents[3].text.strip()
- vendeg = item.contents[6].text.strip()
- """
- - stats: g_data[99].contents[7].contents[0].attrs['href']
- - h2h: g_data[99].contents[8].contents[0].attrs['href']
- - League stats: g_data[99].contents[9].contents[0].attrs['href']
- """
- try:
- print(kezdes + " " + liga + " " + hazai + " - " + vendeg + stat_link_find(item, 1))
- except:
- pass
- # print(stat_link_find(item))
- button = g_data[0].find_all("a", {"class": "button"})
- print('\nSoccerstats ma: ', len(g_data), '/', stats_find())
- # TODO: megpróbálni oop felépítésben a programot elkészíteni
- # TODO: - class létrehozása
- # TODO: - get_matches()
- # TODO: - get_stat()
- # TODO: miután megvannak a lekérdezések, leszűrni a számomra lehetséges meccseket
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement