Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HLTV CS:GO Scrape Match Stats
# 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