запрос дает ошибку времени ожидания в Python 3.7 - PullRequest
0 голосов
/ 30 мая 2019

Я настраиваю монитор для Adidas, чтобы получать уведомления о любых изменениях на сайте, который будет уведомлять меня по электронной почте.

Я использую Python 3.7

Этодовольно простой скриптСценарий загружает домашнюю страницу Adidas и, если он находит какой-либо текст, отправляет мне электронное письмо.

Если он не находит какой-либо текст, он ждет 60 секунд и снова загружает домашнюю страницу.однако я продолжаю получать сообщение об ошибке тайм-аута при запуске сценария

# Import requests (to download the page)
import requests

# Import BeautifulSoup (to parse what we download)
from bs4 import BeautifulSoup

# Import Time (to add a delay between the times the scape runs)
import time

# Import smtplib (to allow us to email)
import smtplib


# This is a pretty simple script. The script downloads the homepage of VentureBeat, and if it finds some text, emails me.
# If it does not find some text, it waits 60 seconds and downloads the homepage again.

# while this is true (it is true by default),
while True:
    # set the url as the page i want to monitor,
    url = "https://www.adidas.co.uk/yeezy"
    # set the headers like we are a browser,
    headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
    # download the homepage
    response = requests.get(url, headers=headers)
    # parse the downloaded homepage and grab all text, then,
    soup = BeautifulSoup(response.text, "lxml")

    # if the number of times the word "SELECT SIZE" occurs on the page is less than 1,
    if str(soup).find("YEEZY BOOST 350 V2 ADULTS") == -1:
        # wait 60 seconds,
        time.sleep(60)
        # continue with the script,
        continue

    # but if the word  occurs any other number of times,
    else:
        gmail_user = 'example@gmail.com'  
        gmail_password = 'Password'

        sent_from = gmail_user  
        to = ['example1@gmail.com']  
        subject = 'OMG Super Important Message'  
        body = 'Hey, what up?\n\n- You'

        email_text = """\  
        From: %s  
        To: %s  
        Subject: %s

        %s
        """ % (sent_from, ", ".join(to), subject, body)

        try:  
            server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
            server.ehlo()
            server.login(gmail_user, gmail_password)
            server.sendmail(sent_from, to, email_text)
            server.close()

            print ('Email sent!')
        except:  
            print ('Something went wrong...')

        break


Traceback (most recent call last):
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connectionpool.py", line 603, in urlopen
    chunked=chunked)
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connectionpool.py", line 387, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1321, in getresponse
    response.begin()
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 296, in begin
    version, status, reason = self._read_status()
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 257, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1052, in recv_into
    return self.read(nbytes, buffer)
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 911, in read
    return self._sslobj.read(len, buffer)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Во время обработки вышеупомянутого исключения произошло другое исключение:

Трассировка (последний последний вызов):

  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests-2.22.0-py3.7.egg\requests\adapters.py", line 449, in send
    timeout=timeout
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connectionpool.py", line 641, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\util\retry.py", line 368, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\packages\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connectionpool.py", line 603, in urlopen
    chunked=chunked)
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connectionpool.py", line 387, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1321, in getresponse
    response.begin()
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 296, in begin
    version, status, reason = self._read_status()
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 257, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1052, in recv_into
    return self.read(nbytes, buffer)
  File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 911, in read
    return self._sslobj.read(len, buffer)
urllib3.exceptions.ProtocolError: ('Connection aborted.', TimeoutError(10060, 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond', None, 10060, None))

Во время обработки вышеуказанного исключения произошло другое исключение:

       Traceback (most recent call last):

          File "C:\Users\TEST\Desktop\adidas.py", line 24, in <module>
            response = requests.get(url, headers=headers)
          File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests-2.22.0-py3.7.egg\requests\api.py", line 75, in get
            return request('get', url, params=params, **kwargs)
          File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests-2.22.0-py3.7.egg\requests\api.py", line 60, in request
            return session.request(method=method, url=url, **kwargs)
          File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests-2.22.0-py3.7.egg\requests\sessions.py", line 533, in request
            resp = self.send(prep, **send_kwargs)
          File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests-2.22.0-py3.7.egg\requests\sessions.py", line 646, in send
            r = adapter.send(request, **kwargs)
          File "C:\Users\TEST\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests-2.22.0-py3.7.egg\requests\adapters.py", line 498, in send
            raise ConnectionError(err, request=request)
        requests.exceptions.ConnectionError: ('Connection aborted.', TimeoutError(10060, 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond', None, 10060, None))
        >>>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...