Ошибка при парсинге(python)

import requests
from bs4 import BeautifulSoup
import lxml

url = 'https://www.gismeteo.ru/weather-sankt-peterburg-4079/'
res = requests.get(url)
print(res.status_code)
print(res.headers)

Я пробовал с другим url. Сработало. Видимо проблема в сайте. Что надо добавить?


Ответы (1 шт):

Автор решения: Namerek

pip install fake-useragent

import requests
from fake_useragent import UserAgent

ua = UserAgent()

url = 'https://www.gismeteo.ru/weather-sankt-peterburg-4079/'
res = requests.get(url,
                   headers={
                       'User-Agent': ua.firefox
                   }
                   )
print(res.status_code)
print(res.headers)
# 200
# {'Date': 'Sun, 03 Apr 2022 20:34:20 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Vary': 'Accept-Encoding, Accept-Encoding, User-Agent', 'Server-Timing': "type;desc='cache'", 'Server': 'gis', 'Strict-Transport-Security': 'max-age=604800', 'X-DC': 'nord.router-ru-nord02', 'X-XSS-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'X-Decepticon': '0', 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Content-Encoding': 'gzip'}

Запустите пару раз. fake-useragent при первом запуске выдает исключение

→ Ссылка