Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1.  
  2. from bs4 import BeautifulSoup
  3. import requests
  4. import csv
  5.  
  6. def find_best_counters(soup2):
  7.     best_counters = []
  8.     #function for finding best counters
  9.     hero_dict = {}
  10.     table = soup2.find(id = "table-right")
  11.     for counter in table.find_all('tr'):
  12.         list_of_td = counter.find_all('td')
  13.         list_of_div_style = list_of_td[2].find_all('div')
  14.         try:
  15.             wins = int(list_of_div_style[0].text)
  16.         except:
  17.             wins = 0
  18.         try:
  19.             losses = int(list_of_div_style[1].text)
  20.         except:
  21.             losses = 0
  22.         winrate = wins/(wins+losses)
  23.         if (wins+losses) >= 7:
  24.             hero_dict[list_of_td[1].text] = winrate
  25.    
  26.     for x in range(0,5):
  27.         key_min = min(hero_dict.keys(), key=(lambda k:hero_dict[k]))
  28.         hero_winrate = hero_dict.pop(key_min)
  29.         best_counters.append(key_min+ ' ' + '(' + str( hero_winrate) + ')' +' ' + '(' + str(wins+losses) + ')' )
  30.        
  31.     return best_counters
  32.  
  33. csv_file = open('hero_counters.csv', 'w')
  34. csv_writer = csv.writer(csv_file)
  35. csv_writer.writerow(['Heros to counter', 'Hero1', 'Hero2', 'Hero3', 'Hero4', 'Hero5'])
  36. source = requests.get('http://www.dota2protracker.com/meta').text
  37.  
  38. soup = BeautifulSoup(source, 'html.parser')
  39. for hero in soup.find_all('div', class_='most_picked_hero_name'):
  40.     hero_name = hero.a.text.strip()
  41.     hero_url = 'http://www.dota2protracker.com' + hero.a.get('href')
  42.     hero_source = requests.get(str(hero_url)).text
  43.     soup2 = BeautifulSoup(hero_source, 'html.parser')
  44.     best = find_best_counters(soup2)
  45.        
  46.    
  47.     csv_writer.writerow([hero_name, best[0], best[1], best[2], best[3], best[4]])
  48.    
  49. csv_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement