Answer the question
In order to leave comments, you need to log in
Why is Connection aborted when connecting to an existing Yandex Weather URL?
import requests
city_id = 27612
url = 'https://export.yandex.ru/weather-ng/forecasts/%s.xml' % city_id
response = requests.get(url)
Answer the question
In order to leave comments, you need to log in
Apparently, YandexPogoda takes your program for a bot and resets the connection. To avoid this, you need to pretend to be a browser, i.e. make sure that the http request sent by the program has a User-agent header. Plus, it may be necessary to request data several times for the same reason (a RemoteDisconnected error pops up). I used sessions, it turned out.
import requests
headers = {'User-agent':'Mozilla/5.0','Referer':'http://www.python.org/'}
session = requests.Session()
adapter = requests.adapters.HTTPAdapter(max_retries=10)
session.mount('http://', adapter)
# запрашиваем нужные урлы
for url in urls:
response = session.request('GET', url, headers = headers)
# делаем что-то с response...
reponse.close()
session.close()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question