-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrapedota.py
176 lines (151 loc) · 5.75 KB
/
scrapedota.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
Copyright (c) 2013 Joshua Machol
MIT, see LICENSE for more details.
"""
import concurrent.futures
import urllib.request
import operator
import random
import json
import bs4
import re
MAX_WORKERS = 8
def soupify(url):
print("soupifying '{}'".format(url))
page = urllib.request.urlopen(url)
soup = bs4.BeautifulSoup(page, "html.parser")
if not soup:
raise ValueError("soup is '{}'", soup)
return soup
def get_hero_urls(sample_size=None, hero_url=None):
soup = soupify("http://www.dota2.com/heroes/")
tags = soup(href=re.compile("http://www.dota2.com/hero/"))
if hero_url:
tags = [t for t in tags if t['href'].find(hero_url) > 0]
if sample_size:
tags = random.sample(tags, sample_size)
return [t['href'] for t in tags]
def get_hero_soups(sample_size=None, hero_url=None):
with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as e:
future_soups = [e.submit(soupify, url) for url in get_hero_urls(sample_size, hero_url)]
for future in concurrent.futures.as_completed(future_soups):
yield future.result()
class Hero:
def __init__(self, hero_id=''):
self.hero_id = hero_id
self.img_url = ''
self.portrait_img_url = ''
self.name = ''
self.title = ''
self.description = ''
self.lore = ''
self.allegiance = ''
self.primary_attribute = ''
self.atk_type = ''
self.roles = ''
self.int = ''
self.agi = ''
self.str = ''
self.dmg = ''
self.move_spd = ''
self.armor = ''
self.sight_range = ''
self.atk_range = ''
self.missile_spd = ''
self.abilities = [] # list of type Ability
class Ability:
def __init__(self, hero_id):
self.ability_id = ''
self.hero_id = hero_id
self.img_url = ''
self.vid_url = ''
self.name = ''
self.description = ''
self.lore = ''
self.mana_cost = ''
self.cooldown = ''
self.details = [] # list of type AbilityDetail
class AbilityDetail:
def __init__(self, ability_id):
self.ability_id = ability_id
self.name = ''
self.detail = ''
def _scrape_hero_ability(ability_soup, hero_id):
a = Ability(hero_id)
a.img_url = ability_soup.find(class_='overviewAbilityImg')['src']
vid_soup = ability_soup.find(class_='abilityVideoContainer')
if vid_soup:
iframe = vid_soup.find('iframe')
a.vid_url = iframe['src']
ability_row_description = (ability_soup.find(class_='abilityHeaderRowDescription') or
ability_soup.find(class_='overviewAbilityRowDescription'))
a.name = ability_row_description.find('h2').string
a.description = ability_row_description.find('p').text.rstrip()
lore_soup = ability_soup.find(class_='abilityLore')
if lore_soup:
a.lore = lore_soup.string
mana_soup = ability_soup.find(class_='mana')
if mana_soup:
a.mana_cost = list(mana_soup.stripped_strings)[1]
cooldown_soup = ability_soup.find(class_='cooldown')
if cooldown_soup:
a.cooldown = list(cooldown_soup.stripped_strings)[1]
details_soup = ability_soup.find(class_='abilityFooterBoxRight')
details = list(details_soup.stripped_strings)
for name, detail in zip(details[::2], details[1::2]):
d = AbilityDetail(a.ability_id)
d.name = name[:-1]
d.detail = detail
a.details.append(d)
return a
def _scrape_hero_abilities(soup_, hero_id):
divs = soup_(class_='abilitiesInsetBoxContent')
return [_scrape_hero_ability(d, hero_id) for d in divs]
def _scrape_hero(soup_):
print(("scraping '{}'"
.format(soup_.find(id='centerColContent').find('h1').string)))
h = Hero()
h.img_url = soup_.find(id='heroTopPortraitIMG')['src']
h.portrait_img_url = soup_.find(id='heroPrimaryPortraitImg')['src']
h.name = soup_.find(id='centerColContent').find('h1').string
h.title = ''
h.lore = list(soup_.find(id='bioInner').stripped_strings)[0]
prim_style = soup_.find(id='overviewIcon_Primary')['style']
if prim_style == 'top:83px':
h.primary_attribute = 'str'
elif prim_style == 'top:43px':
h.primary_attribute = 'agi'
elif prim_style == 'top:1px':
h.primary_attribute = 'int'
h.atk_type = soup_.find(class_='bioTextAttack').string
roles_soup = soup_.find(id='heroBioRoles')
if len(roles_soup.contents) > 1:
h.roles = roles_soup.contents[1].strip(' - ')
h.int = soup_.find(id='overview_IntVal').string
h.agi = soup_.find(id='overview_AgiVal').string
h.str = soup_.find(id='overview_StrVal').string
h.dmg = soup_.find(id='overview_AttackVal').string
h.move_spd = soup_.find(id='overview_SpeedVal').string
h.armor = soup_.find(id='overview_DefenseVal').string
misc_stats = soup_(class_='statRowCol2W')
h.sight_range = misc_stats[0].string
h.atk_range = misc_stats[1].string
h.missile_spd = misc_stats[2].string
h.abilities = _scrape_hero_abilities(soup_, h.hero_id)
return h
def scrape_heroes(sample_size=None, hero_url=None):
with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as e:
future_heros = [e.submit(_scrape_hero, h) for h in get_hero_soups(sample_size, hero_url)]
for future in concurrent.futures.as_completed(future_heros):
yield future.result()
if __name__ == '__main__':
print('scraping heroes')
heroes = list(scrape_heroes())
print('encoding heroes to JSON')
json = json.dumps(heroes, separators=(',', ':'),
default=operator.attrgetter("__dict__"))
f = open('heroes_json.txt', 'w')
print('writing JSON to file "heroes_json.txt')
f.write(json)
f.close()
print('done')