Script has stopped working - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Script has stopped working (/thread-2149.html) |
Script has stopped working - NickBurrett - Feb-22-2017 Hello Can anyone tell me why my script has stopped working?
RE: Script has stopped working - micseydel - Feb-22-2017 It's definitely tough without seeing the script. From the tiny bit you've posted, I could guess that you're scraping a site and they're blocking you now. RE: Script has stopped working - NickBurrett - Feb-22-2017 import requests from lxml import html import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart def send_email(message_html, message_text): # Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart('alternative') msg['Subject'] = config["email_subject"] msg['From'] = config["email_from"] msg['To'] = config["email_to"] # Record the MIME types of both parts - text/plain and text/html. part1 = MIMEText(message_text, 'plain') part2 = MIMEText(message_html, 'html') # Attach parts into message container. # According to RFC 2046, the last part of a multipart message, in this case # the HTML message, is best and preferred. msg.attach(part1) msg.attach(part2) # Send the message via local SMTP server. s = smtplib.SMTP( config["smpt_host"] + ":" + config["smtp_port"] ) # sendmail function takes 3 arguments: sender's address, recipient's address # and message to send - here it is sent as one string. s.login(config["smtp_user"], config["smtp_password"]) s.sendmail(config["email_from"], config["email_to"], msg.as_string()) s.quit() def format_html(job): job_html = """ <div class="jobs-item" style="border: 1px solid rgb(204,204,204); width: 100%; margin-top: 20px;"> <h3><a href="{0}">{1}</a></h3> <p class="location">Location:<strong>{2}</strong></p> <p>{3}<a href="{0}" class="next">Full Details</a></p> </div> """.format( job["url"], job["title"], job["location"], job["description"].encode('utf-8') ) return job_html def format_text(job): job_text = """ {1} {2} {3} {0} """.format( job["url"], job["title"], job["location"], job["description"].encode('utf-8') ) return job_text config = { "url" : "https://www.entsweb.co.uk/jobs-auditions/", "email_from" : "xxx", "email_to" : "xxx", "smpt_host" : "xxx", "smtp_port" : "xxx", "smtp_user" : "xxx", "smtp_password" : "xxx", "email_subject" : "New Jobs", } r = requests.get(config["url"]) message_html = "" message_text = "" tree = html.fromstring( r.text.encode('utf-8') ) for job_node in tree.xpath('//div[@class="jobs-item"]'): job = {} job["title"] = job_node.xpath('.//h3/a/text()')[0] job["url"] = job_node.xpath('.//h3/a/@href')[0] job["url"] = config["url"] + job["url"] job["location"] = job_node.xpath('.//p[@class="location"]/strong/text()')[0] job["description"] = job_node.xpath('.//p[@class="location"]/following-sibling::p[1]/text()')[0] message_html += format_html(job) message_text += format_text(job) send_email(message_html, message_text) |