R
R
r4khic2019-09-02 14:31:41
Python
r4khic, 2019-09-02 14:31:41

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:

Mistake
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

Here is a line of request request code:
def get_html(url):
    r = requests.get(url)
    return r.text

Tried to do so
def get_html(url):
    r = requests.get(url,allow_redirects=True)
    return r.text

But it didn't help me.
I only understood from the error that 30 redirects were exceeded. How to fix this error? Where to begin ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2019-09-02
@vintello

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

R
r4khic, 2019-09-03
@r4khic

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:

The code
# < Получаем html код.
def get_html(url):
    try:
        r = requests.get(url)
        return r.text
    except requests.exceptions.TooManyRedirects:
        print('')

PS: I would like to hear your opinion on this issue.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question