Created
March 6, 2023 21:56
-
-
Save solareon/45a6f254e0b835f8c4ddcc6de950b317 to your computer and use it in GitHub Desktop.
Arcade Highscore Scraper
This file contains hidden or 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
| import argparse | |
| import json | |
| import re | |
| import requests | |
| from bs4 import BeautifulSoup | |
| parser = argparse.ArgumentParser(description='Extract data from arcadehighscores.com tables and save as JSON.') | |
| parser.add_argument('url', help='URL to extract data from') | |
| parser.add_argument('output_file', help='File name to save JSON output to') | |
| args = parser.parse_args() | |
| response = requests.get(args.url) | |
| html = response.content | |
| soup = BeautifulSoup(html, 'html.parser') | |
| tables = soup.find_all('table') | |
| results = [] | |
| for i, table in enumerate(tables): | |
| inner_tables = table.find_all('table') | |
| if not inner_tables: | |
| header = '' | |
| img = table.find_previous('img', {'src': re.compile(r'^pic\.php\?.*n=.*')}) | |
| if img: | |
| header = img['src'].split('n=')[-1].split('&')[0] | |
| table_data = [] | |
| rows = table.find_all('tr') | |
| for row in rows: | |
| cols = row.find_all('td') | |
| row_data = [col.text.strip() for col in cols] | |
| if len(row_data) == 4: | |
| table_data.append(row_data) | |
| if table_data: | |
| table_json = { | |
| 'header': header, | |
| 'rows': table_data | |
| } | |
| results.append(table_json) | |
| output = { | |
| 'tables': results | |
| } | |
| with open(args.output_file, 'w') as f: | |
| json.dump(output, f, indent=2) | |
| print('JSON output saved to', args.output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment