标签:url email sendmail com scrapy == lib smtplib login
import scrapy import smtplib from email.mime.text import MIMEText class WeatherSpider(scrapy.Spider): name = "weather" start_urls = ["http://weather.com.cn/weather/101190502.shtml",] def parse(self,response): date = response.xpath("//div[@id=‘7d‘]/ul/li[1]/h1/text()").extract() data1 = response.xpath("//div[@id=‘7d‘]/ul/li[1]/p[@class=‘wea‘]/text()").extract() data2 = response.xpath("//div[@id=‘7d‘]/ul/li[1]/p[@class=‘tem‘]/span/text()").extract() data3 = response.xpath("//div[@id=‘7d‘]/ul/li[1]/p[@class=‘tem‘]/i/text()").extract() data4 = response.xpath("//div[@id=‘7d‘]/ul/li[1]/p[@class=‘win‘]/i/text()").extract() if data2 == []: lst =[‘今天:‘+date[0]+‘\n天气:‘+data1[0]+‘\n气温:‘+data3[0]+‘\n风力:‘+data4[0]] else: lst =[‘今天:‘+date[0]+‘\n天气:‘+data1[0]+‘\n气温:‘+data2[0]+‘~‘+data3[0]+‘\n风力:‘+data4[0]] #email部分有点小改动,原来的方法发不了 user = ‘470811168@qq.com‘ pwd = to = msg = MIMEText(‘‘.join(lst)) msg[‘Subject‘] = ‘Weather‘ msg[‘From‘] = user msg[‘To‘] = to s = smtplib.SMTP() s.connect(‘smtp.qq.com‘,25) s.login(user,pwd) s.sendmail(user,to,msg.as_string()) s.quit()
标签:url email sendmail com scrapy == lib smtplib login
原文地址:https://www.cnblogs.com/puddingsmall/p/12285334.html