TimeoutError when sending email in the app

35 Views Asked by At

I have written the code to send an email when a new tour arrives from a specific website using web scraping. The program is functioning properly, but it encounters a TimeoutError when it needs to send the email.

The error:

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

Process finished with exit code 1

The code:

import ssl
import time
import requests
import selectorlib
import smtplib,ssl
import os

url="https://programmer100.pythonanywhere.com/tours/"
HEADERS = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
def scrape(url):
    """Scrape the page source from url"""
    response= requests.get(url,headers= HEADERS)
    source=response.text
    return source

def extract(source):
    extractor= selectorlib.Extractor.from_yaml_file("extract.yaml")
    value=extractor.extract(source)["tours"]
    return value

def send_email(message):
    host="smtp.google.com"
    port =465
    username = "[email protected]"
    pswrd = "biyh sayq xiqi yvan"
    reciever= "[email protected]"

    context=ssl.create_default_context()

    with smtplib.SMTP_SSL(host, port, context=context)as server:
        server.login(username,pswrd)
        server.sendmail(username, reciever, message)
    print("email sent")

def store(extracted):
    with open ("data.txt","a") as file:
        file.write(extracted+"\n")

def read(extracted):
    with open ("data.txt","r") as file:
        return file.read()

if __name__=="__main__":
    while True:
            scraped= (scrape(url))
            extracted= extract(scraped)
            print(extracted)
            content=read(extracted)

            if extracted!="No upcoming tours":
                if extracted not in content:
                    store(extracted)
                    send_email(message="Hey, New Event Found")
            time.sleep(1)
0

There are 0 best solutions below