Python simple procedure crawling weather information, time sent by mail to a friend

Some time ago saw this blog.
https://blog.csdn.net/weixin_45081575/article/details/102886718.
He spent request module, which is unfortunately what, I just just used.
He had smtp module, which is unfortunately what, I just just used.
Sending e-mail, which unfortunately what, I will not .

the whole idea:

Python language module encapsulates a lot to help you communicate, it is relatively simple to write. ,

. 1 , https://dev.heweather.com/docs/api/weather wind weather provides weather information acquired interface json format data obtained by the request url. Specific usage can be viewed api development documentation links.
For example, I want to get weather information at this time in Zhengzhou, request as follows:

https://free-api.heweather.net/s6/weather/now?location=zhengzhou&key=8a92371afde5490c9ecf3e9346ff189a

# 得到数据
def get_sky():
    url='https://free-api.heweather.net/s6/weather/now?location=zhengzhou&key=8a92371afde5490c9ecf3e9346ff189a'
    r=requests.get(url)
    now=json.loads(r.text)
    # print(now)

    a=now['HeWeather6'][0]['basic']
    b=now['HeWeather6'][0]['now']
    stat=''
    stat+="省份:%s<br>"%a['admin_area']
    stat+='城市:%s<br>'%(a['location'])
    stat+='云量: %s<br>'%(b['cloud'])
    stat+='能见度: %s℃<br>'%(b['vis'])
    stat+='体感温度: %s℃<br>'%(b['fl'])
    stat+='温度: %s<br>'%(b['tmp'])
    stat+='风力: %s<br>'%(b['wind_sc'])
    stat+='风向: %s<br>'%(b['wind_dir'])

    return stat

After obtaining the requested information through the text page, each information corresponding to the parsed according to the format json returned.

2 , using the smtp module sends a message, get here is relatively simple, there is no blog above that used in the csv file, he added accessories, and other function calls to add attachments to e-mail package. Here only a simple pass strings.

#  邮件传输
def smtp_tran(data):
    print(data)
    msg=MIMEText(data,'html','utf-8')
    HOST='smtp.qq.com'
    SUBJECT='大哥你的天气情况到了'
    FROM='你的[email protected]'
    TO='你的[email protected]'
    msg['Subject']=SUBJECT
    msg['From']=FROM
    msg['To']=TO
    server=smtplib.SMTP(HOST,25)
    server.set_debuglevel(1)  #打印出传递过程
    server.login(FROM,'授权码') #这里填写你邮箱的授权码
    server.sendmail(FROM,[TO],msg.as_string())
    server.quit()

Smtp packaged module using the function on the line, a direct call. A point worth noting is, 163, qq, etc. smtp service requires authorization code instead of a password, the authorization code is nothing to Baidu.

3, complete code

#  用提供好的api爬取天气数据,然后发送给邮箱
import requests
import json
from email.mime.text import MIMEText
import smtplib
# 得到数据
def get_sky():
    url='https://free-api.heweather.net/s6/weather/now?location=zhengzhou&key=8a92371afde5490c9ecf3e9346ff189a'
    r=requests.get(url)
    now=json.loads(r.text)
    # print(now)
    a=now['HeWeather6'][0]['basic']
    b=now['HeWeather6'][0]['now']
    stat=''
    stat+="省份:%s<br>"%a['admin_area']
    stat+='城市:%s<br>'%(a['location'])
    stat+='云量: %s<br>'%(b['cloud'])
    stat+='能见度: %s<br>'%(b['vis'])
    stat+='体感温度: %s℃<br>'%(b['fl'])
    stat+='温度: %s℃<br>'%(b['tmp'])
    stat+='风力: %s<br>'%(b['wind_sc'])
    stat+='风向: %s<br>'%(b['wind_dir'])

    return stat

#  邮件传输
def smtp_tran(data):
    print(data)
    msg=MIMEText(data,'html','utf-8')
    HOST='smtp.qq.com'
    SUBJECT='大哥你的天气情况到了'
    FROM='[email protected]'
    TO='[email protected]'   
    msg['Subject']=SUBJECT
    msg['From']=FROM
    msg['To']=TO
    server=smtplib.SMTP(HOST,25)
    server.set_debuglevel(1)
    server.login(FROM,'yqqrkjmvsiffeaha')
    server.sendmail(FROM,[TO],msg.as_string())
    server.quit()
smtp_tran(get_sky())
# print(get_sky(),end='')

[The To] which can fill a plurality of mailboxes, a plurality of simultaneously transmitted messages.

4, then you can send a mail run, the timing of sending the idea is to create a batch file to run this code statement into them, each time you run the batch file is equivalent to running this file.

Here Insert Picture DescriptionBatch file is a file saved a bunch of commands.
Open Task Scheduler and then create tasks, defining operation is run the batch file, the flip-flop is 8 o'clock every morning, of course, to ensure that the computer is on. This method is for Windows.
as the picture shows:
Here Insert Picture Description

It follows the established:
Here Insert Picture Description

5, Finally, 8 o'clock every morning to get up, then, will be issued.

Published 50 original articles · won praise 67 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_41033366/article/details/104202137