Today, let's talk about how to use python to send weather forecasts and love words to your girlfriend, so that she can understand that programmers are also romantic
Overall idea:
- Climb the Internet to get weather information
- Crawling on the Internet for love words
- send emails
- Realize regular email
Modules to be used:
- Requests: used to send requests to the website
- yagmail: Usage: send mail
- schedule: used to implement scheduled tasks
- Beautiful soup: handle web page tags
- re: regular filtering
url to get weather: https://tianqi.2345.com/ . This is the default. In order to save the trouble of writing, I directly click the corresponding city on the home page. The final link is as follows: https://tianqi.2345.com/cixian1d/70177.htm , you can change it into a link to your girlfriend's city.
The url address where I get love words is: https://www.guaze.com/juzi/23389.html . Of course, you can also customize some love words and send them randomly using random
First, we need to import the corresponding module:
First, climb the Internet to get weather information
import requests from bs4 import BeautifulSoup url = 'https://tianqi.2345.com/cixian1d/70177.htm'
After analysis, we found the location of the label content we want to take
Next we start crawling:
web = requests.get(url) # get request for web page # print(web.text) page = BeautifulSoup(web.text,'html.parser') # Submit the obtained web page content to beautiful Soup for processing, and specify the interpreter as html weather = page.find('div',class_='real-today') # Search the div tag above the span tag we want to get, and specify its class name as real today. You can see in the figure above that div is followed by class, and the content in quotation marks is its class name print(weather.text)
The results are as follows:
Today: 24-31 ° shower turns overcast
The first step is to climb the weather. Isn't it very simple
Then we start the second part, crawling for love words
Another re module needs to be imported in this step
import re loveurl = 'https://www.guaze.com/juzi/23389.html' web2 = requests.get(loveurl) web2.encoding='gb2312' # You need to specify an encoding format for the website here # print(web2.text) page = BeautifulSoup(web2.text, "html.parser") div = page.find('div', class_="content") print(div.text) # Same routine as above
The execution results are as follows: (too many, so only part is shown)
1. People are always greedy, just like at first I just want to know your name.
2. The moon on the sea is the moon in the sky, and the man in front of him is his sweetheart—— Zhang Ailing
3. Do you know what my weakness is? It's your fault
4. I dreamed of you last night. In the dream, I love you very much. For me, the dream is false, but loving you is true.
5. Want to be the one who only likes you and is only loved by you
6. So many roads, which one can reach your heart
7. When I met you, I became very low, very low, all the way to the dust But my heart is glad, and there opens a flower—— Zhang Ailing
It can be seen that the obvious defect is that there are empty lines, so we need to deal with it a little
div = str(div.text) # Convert the content format obtained above to a string grep = re.compile(r"\d+,(.*)") # re filter format is numbers and arbitrary content, excluding blank lines content = grep.findall(div) # Specify the content to be filtered as div and return a list print(content[1]) # Print the contents with subscript 1 in the list
The results are as follows:
The moon on the sea is the moon in the sky, and the man in front of him is his sweetheart—— Zhang Ailing
The second step has been completed, and the rest is to send emails regularly
Import module:
import yagmail # This module is used to send mail import schedule # This module is used to schedule tasks yag = yagmail.SMTP( host='smtp.qq.com', user='Your mailbox', # If you used QQ email, write SMTP qq. COM, if it is 163, write SMTP 163.com password='Authorization code', smtp_ssl=True # If the authorization code is opened in the qq mailbox, smtp will generate an authorization code ) weather = [weather.text,"Daily love talk:",content[ran], # Define what to send yagmail.inline(r"./love.jpg") # Attached pictures can be deleted without sending pictures ] yag.send( to=['Email address to send to'], subject='weather forecast', # Mail subject contents=weather # The content sent is the weather defined above, where weather Text is the weather forecast and content[ran] is the love word )
Full code:
import requests import yagmail # This module is used to send mail import schedule # This module is used to schedule tasks from bs4 import BeautifulSoup import re ran = 0 url = 'https://tianqi.2345.com/cixian1d/70177.htm '# define the url of the weather forecast loveurl = 'https://www.guaze.com/juzi/23389.html '# defines the url of a love word def email(): global ran # Declare the ran variable as a global variable web = requests.get(url) # print(web.text) page = BeautifulSoup(web.text,"html.parser") # print(ran) # print(love[ran]) weather = page.find("div",class_="real-today") # print(weather.text) web2 = requests.get(loveurl) web2.encoding = 'gb2312' page = BeautifulSoup(web2.text, "html.parser") div = page.find('div', class_="content") div = str(div.text) # print(div) grep = re.compile(r"\d+,(.*)") content = grep.findall(div) # print(content) # The content in the email function is to crawl the weather and love words. You can change the url for the specific address yag = yagmail.SMTP( host='smtp.qq.com', user='Your mailbox', # If you used QQ email, write SMTP qq. COM, if it is 163, write SMTP 163.com password='Authorization code', smtp_ssl=True # If the authorization code is opened in the qq mailbox, smtp will generate an authorization code ) weather = [weather.text,"Daily love talk:",content[ran], # Define what to send yagmail.inline(r"./love.jpg") # Attached pictures can be deleted without sending pictures ] yag.send( to=['Email address to send to'], subject='weather forecast', # Mail subject contents=weather # The content sent is the weather defined above, where weather Text is the weather forecast and content[ran] is the love word ) print("Send complete") ran += 1 schedule.every().day.at("05:21").do(email) # Execute the function email0 at 5:21 every day # schedule.every(10).seconds.do(email) #Execute the function email every 10 seconds, which I use here for testing while True: schedule.run_pending()
The ran variable is used to get the contents in the list of love words, and add 1 for each sending, so as to make the love words sent different each time
The effect is as follows: (I set it here to send it at 5:21 a.m. every day, you can change it according to your needs, and the picture is also) get up for your girlfriend Amway!