Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bs4 import BeautifulSoup
- import requests
- import csv
- def find_best_counters(soup2):
- best_counters = []
- #function for finding best counters
- hero_dict = {}
- table = soup2.find(id = "table-right")
- for counter in table.find_all('tr'):
- list_of_td = counter.find_all('td')
- list_of_div_style = list_of_td[2].find_all('div')
- try:
- wins = int(list_of_div_style[0].text)
- except:
- wins = 0
- try:
- losses = int(list_of_div_style[1].text)
- except:
- losses = 0
- winrate = wins/(wins+losses)
- if (wins+losses) >= 7:
- hero_dict[list_of_td[1].text] = winrate
- for x in range(0,5):
- key_min = min(hero_dict.keys(), key=(lambda k:hero_dict[k]))
- hero_winrate = hero_dict.pop(key_min)
- best_counters.append(key_min+ ' ' + '(' + str( hero_winrate) + ')' +' ' + '(' + str(wins+losses) + ')' )
- return best_counters
- csv_file = open('hero_counters.csv', 'w')
- csv_writer = csv.writer(csv_file)
- csv_writer.writerow(['Heros to counter', 'Hero1', 'Hero2', 'Hero3', 'Hero4', 'Hero5'])
- source = requests.get('http://www.dota2protracker.com/meta').text
- soup = BeautifulSoup(source, 'html.parser')
- for hero in soup.find_all('div', class_='most_picked_hero_name'):
- hero_name = hero.a.text.strip()
- hero_url = 'http://www.dota2protracker.com' + hero.a.get('href')
- hero_source = requests.get(str(hero_url)).text
- soup2 = BeautifulSoup(hero_source, 'html.parser')
- best = find_best_counters(soup2)
- csv_writer.writerow([hero_name, best[0], best[1], best[2], best[3], best[4]])
- csv_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement