Не могу понять, почему не все файлы записываються, как html-страницы
Пишу парсер для сбора данных с metacritic, и сохраняю каждую страницу, как файл в папку дата. Но часть файлов записываются пустыми.
url = f'https://www.metacritic.com/browse/games/score/metascore/all/all/filtered=2?page=0'
user_agent = {
'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'}
response = requests.get(url,headers=user_agent)
src = response.text
with open('index.html',encoding= 'utf-8') as file:
src = file.read()
soup = BeautifulSoup(src, 'html.parser')
all_games_hrefs = soup.find_all('a',class_ = 'title')
all_games_dict = {}
for href in all_games_hrefs:
href_text = href.text
hrefs = 'https://www.metacritic.com'+href.get('href')
print(f'{href_text}: {hrefs}')
all_games_dict[href_text] = hrefs
with open('all_games_hrefs', 'w') as file:
json.dump(all_games_dict, file, indent=4, ensure_ascii=False)
with open('all_games_hrefs') as file:
all_games = json.load(file)
for game_name, game_href in all_games.items():
response = requests.get(url=game_href, headers=user_agent)
src = response.text
time.sleep(rand.randint(3, 30))
with open(f'data/{game_name}.html', 'w', encoding='utf-8') as file:
file.write(src)
with open(f'data/{game_name}.html', encoding='utf-8') as file:
src = file.read()
Хотелось бы узнать, с чем это связано и как это можно исправить