Python Gets the weather condition and sends it to the destination mailbox as a mail message

Source: Internet
Author: User
Tags email account

Python crawl weather conditions

The following is the sample code:

 fromUrllib.requestImportUrlopen fromBs4ImportBeautifulSoup fromUrllib.errorImportHttperrorImportSmtplib fromEmail.mime.textImportMimetext fromEmail.utilsImportformataddrImport Time"""
Crawler is a need to invest a great degree of maintenance, such as Web developers in the future at some time to refactor the HTML code, which is likely to lead to the failure of the crawler, so write a crawler to try to be proactive, so that the crawler more robust, so you can sleep a stable sleep
This program is very simple, roughly divided into two parts, the crawler part and the mail part, the code also has a lot of optimization space, such as some constants can get outside, so that the code looks more neat, the message content is sent in HTML format, so you can change to your favorite style.
I do not recommend that you frequently send e-mails, mailboxes may be blocked, letters returned, resulting in a failure to send
Email mailbox to turn on the SMTP client feature, mailbox--and set-up SMTP service, get authorization code (that is, the password required to login in the program)
"""
#获取当天的天气情况defget_weather (URL):Try: HTML=urlopen (URL). Read ()exceptHttperror as E:returnNoneTry: Weather_list=[] BS0BJ=beautifulsoup (HTML,"Html.parser") Time.sleep (5) Weather=bs0bj.find ("Div",{"class":"Condition-icon Wx-weather-icon Vector"}). next_siblings title=Bs0bj.body.h1.get_text () weather_list.append (title) forNextinchWeather:weather_list.append (Next.get_text ())exceptAttributeerror as E:returnNonereturnweather_list
#获取未来5天的天气情况defget_5weathers (URL):Try: HTML=urlopen (URL). Read ()exceptHttperror as E:returnNoneTry: Weather5_list=[] BS0BJ=beautifulsoup (HTML,"Html.parser") Weathers=bs0bj.find ("Table",{"class":"twc-table"}). tbody forChildinchWeathers.children:list1=[] forIinchChild.children:list1.append (I.get_text ()) List1.remove ("") weather5_list.append (list1)exceptAttributeerror as E:returnNonereturnweather5_list
#等到的数据形如一下数据格式#weather=[' Beijing, China ', ' 3° ', ' sunny ', ' body sense temperature -1° ', ' high temperature-low temperature -7° UV index 0 (max.) ']#weathers=[[' Tonight \n12 month 18th ', ' Big Sunny ', '---7° ', ' 0% ', ' North 15 km/h ', ' 40% '], [' Tuesday December 19 ', ' Sunny ', ' 5°-5° ', ' 0% ', ' Southwest 21 ' km/h ', ' 32% '], [' Wednesday December 20 ', ' Sunny ', ' 7°-6° ', ' 0% ', ' northwest 22 km/h ', ' 33% '], [' Thursday December 21 ', ' Sunny ', ' 6°-6° ', ' 0% ', ' Southwest West 1 ' 1 km/h ', ' 41% '], [' Friday December 22 ', ' Sunny ', ' 8°-6° ', ' 0% ', ' North 16 km/h ', ' 30% '], [' Saturday December 23 ', ' Sunny ', ' 8°-3° ', ' 0% ', ' Northwest West 1 ' 4 km/h ', ' 29% ']
##*********************** send e-mail *******************************#third-party SMTP serversdefsendEmail (): Msg=mimetext (Mail_msg,"HTML","Utf-8") msg[" from"]=FORMATADDR (["Crotch man", Mail_user]) msg[" to"]=FORMATADDR (["Small crotch man s", receive_address]) msg["Subject"]="Beijing Weather Forecast" Try: SMTP0BJ=smtplib. Smtp_ssl (mail_host,465) Smtp0bj.login (mail_user,mail_pass) Smtp0bj.sendmail (mail_user,receive_address,msg.as_string ()) Smtp0bj.quit ()Print("Mail has been send to%s successfully."%receive_address)exceptSmtplib. Smtpexception as E:Print(e)if __name__=="__main__": Weather=get_weather ("http://www.weather.com") Weathers=get_5weathers ("Https://weather.com/zh-CN/weather/5day/l/CHXX0008:1:CH") ifweather==none:exit ()ifweathers==none:exit () mail_host="smtp.163.com"Mail_user="[email protected]" #Sender Email Accountmail_pass="JXXxX199XXxX" #Sender Mailbox Passwordreceives=[ "4352XXxX@qq. com", "3426XXxX@qq. com", "5437XXxX[email protected]", "6353XXxX[email protected]", ] #Recipient's email accountmail_msg=""" """% (weather[0],weather[1],weather[2],weather[3],weather[4],weathers[0][0],weathers[0][1],weathers[0][2],weathers [0] [3],weathers[0][4],weathers[0][5],weathers[1][0],weathers[1][1],weathers[1][2],weathers[1][3],weathers[1][4], WEATHERS[1][5],WEATHERS[2][0],WEATHERS[2][1],WEATHERS[2][2],WEATHERS[2][3],WEATHERS[2][4],WEATHERS[2][5], Weathers[3][0],weathers[3][1],weathers[3][2],weathers[3][3],weathers[3][4],weathers[3][5],weathers[4][0], Weathers[4][1],weathers[4][2],weathers[4][3],weathers[4][4],weathers[4][5]) forReceive_addressinchReceives:sendemail () time.sleep (120) exit ()

Python Gets the weather condition and sends it to the destination mailbox as a message

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.