Как сохранить результат парсинга в json файл
import csv
import requests
from bs4 import BeautifulSoup
FILE = 'games.csv'
HOST = 'https://store.steampowered.com'
URL = 'https://store.steampowered.com/tags/ru/Гонки/?l=russian'
HEADERS = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 YaBrowser/20.11.3.183 Yowser/2.5 Safari/537.36'
}
def get_html(url, params=None):
req = requests.get(url, headers=HEADERS, params=params)
return req
def get_conten(html):
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('a', class_='tab_item')
games = []
for item in items:
ru_price = item.find('div', class_='discount_final_price')
if ru_price:
ru_price = ru_price.get_text()
else:
ru_price = 'Цена неизвестна'
games.append(
{
'title': item.find('div', class_='tab_item_name').get_text(strip=True),
'tag': item.find('div', class_='tab_item_top_tags').get_text(strip=True),
'ru_price': ru_price,
}
)
return games
def parser():
PAGENATION= input('Укажите количество страниц для парсинга: ')
PAGENATION= int(PAGENATION.strip())
html = get_html(URL)
if html.status_code == 200:
games = []
for page in range(0, PAGENATION):
print(f'Парсинг страницы {page}')
html = get_html(URL, params={'p': page})
games.extend(get_content(html.text))
print(games)
else:
print('Error')
parser()