-
Notifications
You must be signed in to change notification settings - Fork 0
/
itchscrape.py
48 lines (38 loc) · 1.73 KB
/
itchscrape.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
import csv
import requests
from bs4 import BeautifulSoup
from tqdm import tqdm
def scrape_game_data(url):
game_list = []
page_num = 1
while True:
page_url = f"{url}?page={page_num}"
response = requests.get(page_url)
if response.status_code != 200:
break
soup = BeautifulSoup(response.content, 'html.parser')
games_on_page = soup.find_all('div', class_='game_cell')
if not games_on_page:
break
for game in tqdm(games_on_page, desc=f"Scraping page {page_num}"):
game_name = game.find('a', class_='title game_link').text.strip()
game_price_element = game.find('div', class_='price_value')
game_price = game_price_element.text.strip() if game_price_element else 'N/A'
game_genre_element = game.find('div', class_='game_genre')
game_genre = game_genre_element.text.strip() if game_genre_element else 'N/A'
game_url = game.find('a', class_='title game_link')['href']
game_list.append((game_name, game_price, game_genre, game_url))
page_num += 1
return game_list
def save_to_csv(data, filename):
with open(filename, 'w', newline='', encoding='utf-8-sig') as csvfile:
csv_writer = csv.writer(csvfile)
csv_writer.writerow(["Game_Name", "Game_Price", "Game_Genre", "Game_URL"])
csv_writer.writerows(data)
if __name__ == "__main__":
url = "https://itch.io/games/made-with-pico-8"
game_data = scrape_game_data(url)
if game_data:
csv_filename = "Pico8_games_data.csv"
save_to_csv(game_data, csv_filename)
print(f"Data scraped successfully and saved to {csv_filename}.")