What? You don't have a girlfriend yet? Teach you how to capture a girl's heart with Python!

The weather is cooling, but the feelings are heating up?

Just arriving at the company in the morning, I received the soul torture from xiaoq:

"Q boy! Or take a vacation this afternoon! I'll take you to the psychiatry department! " I can't stand it. I blurted out.

Voice did not fall, the front row of the operation of small flowers look back to small Q, smile, beautiful Hengsheng.

When did this sullen young man hook up to run a business? I was about to ask, small Q saw through my intention, pretending to be serious: "cough!"! Work, work! "

Feed me such a bowl of dog food early in the morning and let me concentrate on my work? You think I can't find a clue without saying it?

Aristotle, the great philosopher, once said that "the beginning of a relationship is often reflected through a circle of friends". I ordered to open the small flower's circle of friends, brush and brush, and finally found the clue in a circle of friends half a month ago.

Little Q, this stuffy guy, actually sends a weather care email to capture her heart before work every day! The brain circuit is so clear!

No wonder I said that Python is like a month old when I went to work today, which leads their feelings! Because these things are implemented and automated in Python, it can be said that they are in minutes. I decided to repeat it and do something by the way.

The whole process only involves crawling the weather and sending emails, which can be said to be completed in one go.

In the process of knocking code, I realized that the hardest part was to combine the two and edit the local flavor to the target user. After all, it's 9102. Although the weather forecast is easy to read, how many people pay attention to it every day?

To get weather data, www.tianqi.com This website is suitable. All data can be obtained directly in the source code. The weather is small, lovely and long

Just pretending to be headers, crawling can happily locate all the data you want. Some codes are as follows:

import requests
from lxml import etree

def parse(url = 'https://www.tianqi.com/hangzhou'):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
    html = requests.get(url,headers = headers)
    bs = etree.HTML(html.text)

    #Today's weather related data: date, day of the week, weather, minimum temperature, maximum temperature
    today_date = bs.xpath('//ul[@class = "week"]/li[1]/b/text()')[0]
    today_week = bs.xpath('//ul[@class = "week"]/li[1]/span/text()')[0]
    today_weather = bs.xpath('//ul[@class = "txt txt2"]/li[1]/text()')[0]
    today_low = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[1]/b/text()')[0]
    today_high = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[1]/span/text()')[0]

    #Tomorrow's weather related data, with dimensions consistent with the above
    tomorrow_date = bs.xpath('//ul[@class = "week"]/li[2]/b/text()')[0]
    tomorrow_week = bs.xpath('//ul[@class = "week"]/li[2]/span/text()')[0]
    tomorrow_weather = bs.xpath('//ul[@class = "txt txt2"]/li[2]/text()')[0]
    tomorrow_low = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[2]/b/text()')[0]
    tomorrow_high = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[2]/span/text()')[0]

    tomorrow = ('Tomorrow is%s,%s,%s,%s-%s Temperature difference%d degree')% \
          (tomorrow_date,tomorrow_week,tomorrow_weather,tomorrow_low,tomorrow_high,int(int(tomorrow_high)-int(tomorrow_low)))

    print(('Tomorrow is%s,%s,%s,%s-%s Temperature difference%d degree')% \
          (tomorrow_date,tomorrow_week,tomorrow_weather,tomorrow_low,tomorrow_high,int(int(tomorrow_high)-int(tomorrow_low))))

    #Calculate the temperature difference between today and tomorrow. The highest temperature is used here
    temperature_distance = int(tomorrow_high) - int(today_high)

    if temperature_distance > 0:
        a = 'Warming up tomorrow%d' % temperature_distance
        print('Warming up tomorrow%d' % temperature_distance)
    if temperature_distance < 0:
        a = 'Cool down tomorrow%d' % temperature_distance
        print('Cool down tomorrow%d' % temperature_distance)
    else:
        a = 'Maximum temperature unchanged'
        print('Maximum temperature unchanged')
    content = tomorrow,a
    return content

After that, a return car not only gets the weather tomorrow (in fact, it can get it in the next few days), but also calculates the temperature difference.

Simply edit the weather data you climb to, and you can send it directly to the target user by email.

When it comes to e-mail, the strong Amway yagmail library takes minimalism to the extreme. Two lines of code can complete the operation of logging in and sending e-mail. The code is as follows:

#Python learning group 827513319  
import yagmail

def send_email(contents,send_to = 'receiver_email@xx.com'):
    #Log in email, set the login account, password, port and other information
    yag = yagmail.SMTP(user = 'youremail@sohu.com',password = 'yourpass',
                       host = 'smtp.sohu.com',port = '465')

    #After logging in, you can send one message. Set who to send it to, and the subject and content of the message
    yag.send(to = send_to,
             subject = 'Weather care',
             contents = contents)
    print('Sending succeeded!~')

After encapsulation, it's a piece of cake to regularly pick up the weather and send emails to the people who need to be cared for.

With Python getting older this month, chicken thief's little Q is easy to work on and can walk away, killing two birds with one stone.

I decided to do something humanitarian with this code.

You don't send the email to Xiaohua at 17:50 every day. I'll send it in front of you anonymously at 17:45. I'll give you a Suprise and say "arrange it!"!

Anxiously, he waited until 17:45, but there was no movement on Xiaohua's side. He didn't even raise his head.

Wrong? Is there something wrong with the email code? I quickly looked at the history of the sent email. At 17:45, the weather care email said "sent".

The target is quiet. It must be a demon. I'm waiting, waiting, with a lot of anxiety.

20: 15. I didn't expect xiaoq's wechat message:

The last sentence, let people, speechless and choking and pouring

Until now, the mind is full of the famous words of the emotional philosopher xiaoq:

"Everyone's concern is harassment except me!"

"Everyone's concern is harassment except me!"

"Everyone's concern is harassment except me!"

Note: the complete weather + email code is not uploaded in the computer! If you want something, you can say it~

Published 22 original articles, won praise 7, visited 10000+
Private letter follow

Tags: Python Windows Programming

Posted on Fri, 10 Jan 2020 10:05:14 -0500 by camdenite