Last active
June 6, 2021 16:37
-
-
Save dimitryzub/adfc10730eb8a60e2f7f8cfc50d25a00 to your computer and use it in GitHub Desktop.
HLTV CS:GO Scrape Match Stats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Website was dynamically updated so requests-html was used instead of bs4 | |
from requests_html import HTMLSession | |
import csv | |
session = HTMLSession() | |
with open('csgo_match_stats.csv', mode='w', newline='', encoding='utf8') as csv_file: | |
# fieldnames needs to be the same as while doing .appned() | |
fieldnames = ['Left Team', 'Left Team Score', 'Right Team Score', 'Right Team', 'Event Name'] | |
writer = csv.DictWriter(csv_file, fieldnames=fieldnames) | |
writer.writeheader() | |
# Fast but not reliable solution. It only be scraped ones. | |
for url in range(0, 7300, 100): | |
response = session.get(f'https://www.hltv.org/results?offset={url}&startDate=2020-06-05&endDate=2021-06-05') | |
data = [] | |
for result in response.html.find('.result'): | |
# Team on the left | |
team_name_left = result.find('.line-align.team1', first=True).text | |
# Team on the right | |
team_name_right = result.find('.line-align.team2', first=True).text | |
event_name = result.find('.event', first=True).text | |
# Combined scores before splitting | |
result_score_combined = result.find('.result-score', first=True).text | |
# Score on the left | |
left_score = result_score_combined.split(' - ')[0] | |
# Score on the right | |
right_score = result_score_combined.split(' - ')[1] | |
# print(team_name_left) | |
# print(team_name_right) | |
# print(event_name) | |
# print(result_score_combined) | |
# print(left_score) | |
# print(right_score) | |
data.append({ | |
'Left Team': team_name_left, | |
'Left Team Score': left_score, | |
'Right Team Score': right_score, | |
'Right Team': team_name_right, | |
'Event Name': event_name, | |
}) | |
print(f'Scraping page: {url}') | |
# Writes rows from the data dict() after makes an .append() | |
for cs_go_data in data: | |
writer.writerow(cs_go_data) | |
print('Done scraping.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment