A
A
arhipT9992022-03-31 19:33:59
Python
arhipT999, 2022-03-31 19:33:59

How to fix error [Errno 11001] getaddrinfo failed?

The code:

import telebot
import requests
from bs4 import BeautifulSoup as bs

bot = telebot.TeleBot('*Токен*')
new = 'Первым замом Жириновского в центральном аппарате ЛДПР назначен депутат Алексей Диденко'
n = []
num = 0

def rass(message):
    while True:
        import random
        global new
        global n
        global num
        url = f'http://{random.choice(n)}'
        response = requests.get(url)
        soup = bs(response.text, 'lxml')
        teg = soup.find_all('div', class_='tgme_widget_message_wrap')
        div = soup.find_all('div', class_='tgme_widget_message')
        next = soup.find_all('div', class_='tgme_widget_message_bubble')
        text_ = soup.find_all('div', class_='tgme_widget_message_text')

        s = []
        for i in text_:
            s.append(i)
        try:
            out = str(s[-1])
        except IndexError:
            bot.send_message(message.chat.id, 'Вы неправильно ввели какал!')
            num = 0
            n = []
            kanal(message)
        out = out[65:]
        out = out[:-6]
        for _ in range(out.count('<')):
            a = out.find('<')
            b = out.find('>')
            s = out[a:b+1]
            out = out.replace(s, '')
        if out == new:
            pass
        else:
            new = out
            bot.send_message(message.chat.id, f'{out}\n-----\nНовость от {url}')
            print(out)

def proverka(message):
    if message.text.lower() == 'стоп' and n != []:
        num = 1
        print(n)
        rass(message)
    else:
        n.append(message.text)
        bot.send_message(message.chat.id, 'Чтобы заполнить больше каналов пини "/next"')

def kanal(message):
    msg = bot.send_message(message.chat.id, 'Введите название канала например: @rian_ru. Если больше не хотите вводить названия напишите "Стоп"')
    bot.register_next_step_handler(msg, proverka)

@bot.message_handler(commands='next')
def pr(message):
    if num == 0:
        kanal(message)
    else:
        rass(message)

@bot.message_handler(commands='start')
def nav(message):
    pr(message)

bot.polling()

Console:
Traceback (most recent call last):
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\connection.py", line 72, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\socket.py", line 955, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 398, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 239, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1037, in _send_output
    self.send(msg)
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 975, in send
    self.connect()
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 205, in connect
    conn = self._new_conn()
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001AFDEF130D0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\adapters.py", line 440, in send
    resp = conn.urlopen(
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='rian_ru', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001AFDEF130D0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\Student\Documents\GitHub\arhipT999.github.io\tbot.py", line 73, in <module>
    bot.polling()
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\__init__.py", line 664, in polling
    self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\__init__.py", line 726, in __threaded_polling
    raise e
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\__init__.py", line 686, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\util.py", line 136, in raise_exceptions
    raise self.exception_info
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\util.py", line 88, in run      
    task(*args, **kwargs)
  File "c:\Users\Student\Documents\GitHub\arhipT999.github.io\tbot.py", line 53, in proverka
    rass(message)
  File "c:\Users\Student\Documents\GitHub\arhipT999.github.io\tbot.py", line 18, in rass
    response = requests.get(url)
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 75, in get      
    return request('get', url, params=params, **kwargs)
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 61, in request  
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\Student\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\adapters.py", line 519, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='rian_ru', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001AFDEF130D0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danil Gorev, 2022-03-31
@ketovv

Let's take a closer look at your error. It is located here - response = requests.get(url)
An error of type ConnectionError tells us that the requests.get function could not wait for a response from the resource (URL) passed to it. How did it happen?
You give the url parameter f'http://{random.choice(n)}', and random.choice(n) should choose a suitable host from n, but returns rian_ru?
It is unlikely that you will be able to open the site rian_ru right?
In addition, you should definitely listen to THIS song and fix all the styling errors in your code, which are terrifying, sorry.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question