Advertisement
Guest User

soccer

a guest
Oct 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.50 KB | None | 0 0
  1. #!/usr/bin/ python
  2. # -*- coding: UTF-8 -*-
  3. import requests
  4. import browser_cookie3
  5. from bs4 import BeautifulSoup
  6. import pytz
  7. from datetime import datetime
  8. import lxml.html as lh
  9.  
  10.  
  11. def good_timezone_converter(input_dt, current_tz='UTC', target_tz='Europe/Budapest'):
  12.     current_tz = pytz.timezone(current_tz)
  13.     target_tz = pytz.timezone(target_tz)
  14.     target_dt = current_tz.localize(input_dt).astimezone(target_tz)
  15.     return target_tz.normalize(target_dt)
  16.  
  17.  
  18. def date_convert(old_date):
  19.     c_date = datetime.strptime(old_date, '%H:%M')
  20.     old_timezone = pytz.timezone("UTC")
  21.     new_timezone = pytz.timezone("Europe/Budapest")
  22.     my_date_in_local_timezone = old_timezone.localize(c_date).astimezone(new_timezone)
  23.  
  24.     return my_date_in_local_timezone.strftime('%H:%M')
  25.  
  26.  
  27. def poor_time_converter(old_time):
  28.     hour = int(old_time[:2]) + 1
  29.     if hour < 10:
  30.         return '0' + str(hour) + old_time[2:5]
  31.     elif hour > 23:
  32.         return '0' + str(hour - 24) + old_time[2:5]
  33.     return str(hour) + old_time[2:5]
  34.  
  35.  
  36. def table_index_find(tree):
  37.     path = '//*[@id="container"]/div[5]/table[1]/tr[1]/td'
  38.     tables = tree.xpath(path)[0]
  39.     label = tables[0].getchildren()[0].getchildren()[0].getchildren()[0].text
  40.     if label == 'Tables':
  41.         index = '2'
  42.     else:
  43.         index = '1'
  44.     return index
  45.  
  46.  
  47. def table_content(url, table_id):
  48.     # print(url)
  49.     response = requests.get(url, cookies=cj)
  50.     tree = lh.fromstring(response.text)
  51.     league_table_xpath = '//*[@id="container"]/div[5]/table[1]/tr[1]/td/table[' + \
  52.                          table_index_find(tree) + ']/tr/td[' + str(table_id) + ']/table'
  53.     table = tree.xpath(league_table_xpath)
  54.     home_rank = 0
  55.     away_rank = 0
  56.     team_home = ''
  57.     team_away = ''
  58.     megfelel = ' ERROR'
  59.  
  60.     if response.ok:
  61.         for i in range(2, len(table[0])):
  62.             table_children = table[0][i].getchildren()
  63.             team = table_children[1].text.strip()
  64.             gp = table_children[2][0].text
  65.             pts = table_children[3][0].text.replace('\r\n', '')
  66.  
  67.             if team == hazai:
  68.                 team_home = ' (HOME rank:' + str(i-1) + ' gp:' + str(gp) + ' pts:' + str(pts) + ' / '
  69.                 home_rank = i-1
  70.             if team == vendeg:
  71.                 team_away = 'AWAY rank:' + str(i-1) + ' gp:' + str(gp) + ' pts:' + str(pts) + ')'
  72.                 away_rank = i-1
  73.         if home_rank < away_rank:
  74.             megfelel = ' IGEN'
  75.         else:
  76.             megfelel = ''
  77.  
  78.     return team_home + team_away + megfelel
  79.  
  80.  
  81. def stat_link_find(act_data, table_id):
  82.     result = ''
  83.     for field in field_list:
  84.         data = act_data.contents[field]
  85.         if len(data) != 0:
  86.             if data.contents[0].text == 'stats':
  87.                 stat_url = 'http://www.soccerstats.com/' + data.contents[0].attrs['href']
  88.                 result = table_content(stat_url.encode(encoding='UTF-8', errors='strict'), table_id)
  89.     return result
  90.  
  91.  
  92. def stats_find():
  93.     a = 0
  94.  
  95.     for link in g_data:
  96.         for field in field_list:
  97.             data = link.contents[field]
  98.             if len(data) != 0:
  99.                 if data.contents[0].text == 'stats':
  100.                     a += 1
  101.     return a
  102.  
  103.  
  104. cj = browser_cookie3.load()
  105. r = requests.get("http://www.soccerstats.com/matches.asp?matchday=6", cookies=cj)
  106. soup = BeautifulSoup(r.content, 'lxml')
  107. g_data = soup.find_all("tr", {"class": "trow8"})
  108. # league_table_xpath = '//*[@id="container"]/div[5]/table[1]/tr[1]/td/table[2]/tr/td[1]/table'
  109. field_list = (7, 8, 9)
  110.  
  111.  
  112. for item in g_data:
  113.     datum = item.contents[0].contents[0].text
  114.     kezdes = item.contents[0].contents[2].text.strip()
  115.     liga = item.contents[2].text
  116.     hazai = item.contents[3].text.strip()
  117.     vendeg = item.contents[6].text.strip()
  118.     """
  119.    - stats:        g_data[99].contents[7].contents[0].attrs['href']
  120.    - h2h:          g_data[99].contents[8].contents[0].attrs['href']
  121.    - League stats: g_data[99].contents[9].contents[0].attrs['href']
  122.    """
  123.     try:
  124.         print(kezdes + " " + liga + " " + hazai + " - " + vendeg + stat_link_find(item, 1))
  125.     except:
  126.         pass
  127.  
  128.     # print(stat_link_find(item))
  129.  
  130.  
  131. button = g_data[0].find_all("a", {"class": "button"})
  132.  
  133. print('\nSoccerstats ma: ', len(g_data), '/', stats_find())
  134.  
  135.  
  136. # TODO: megpróbálni oop felépítésben a programot elkészíteni
  137. # TODO: - class létrehozása
  138. # TODO: - get_matches()
  139. # TODO: - get_stat()
  140. # 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