Answer the question
In order to leave comments, you need to log in
Requests.exceptions.TooManyRedirects: Exceeded 30 redirects how to solve error?
I am parsing this portal . The title, date and content of the news. And when parsing, I use the python 3.7 BS4 library
. And when parsing this portal , I get the following error:
Traceback (most recent call last):
File "C:/Users/Администратор/PycharmProjects/Task/parser.py", line 124, in <module>
call_all_func(resources)
File "C:/Users/Администратор/PycharmProjects/Task/parser.py", line 104, in call_all_func
item_page = get_html(resource_link)
File "C:/Users/Администратор/PycharmProjects/Task/parser.py", line 14, in get_html
r = requests.get(url)
File "C:\Users\Администратор\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\Администратор\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Администратор\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Администратор\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 668, in send
history = [resp for resp in gen] if allow_redirects else []
File "C:\Users\Администратор\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 668, in <listcomp>
history = [resp for resp in gen] if allow_redirects else []
File "C:\Users\Администратор\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 165, in resolve_redirects
raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects, response=resp)
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.
Process finished with exit code 1
def get_html(url):
r = requests.get(url)
return r.text
def get_html(url):
r = requests.get(url,allow_redirects=True)
return r.text
Answer the question
In order to leave comments, you need to log in
you need to start by checking the url
, I think it lacks parameters and it redirects to itself with the hope that everything will be entered through the gui. and request protects from an eternal loop and falls off after 30 redirects
Found what was the reason: Cyclic redirects. That is, the page refers to itself, and requests exceeds the request limit when trying to reach the final page. The solution to the problem is to ignore the error. Capture her.
Here is the code:
# < Получаем html код.
def get_html(url):
try:
r = requests.get(url)
return r.text
except requests.exceptions.TooManyRedirects:
print('')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question