Python instance---Access joke api+ send mail + cloud host crontab regular execution

Source: Internet
Author: User
Tags email account

The idea of this example comes from the Internet, but the online implementation of the steps to explain the lack of clarity, especially to achieve this series of steps, there will be a lot of pits, today on a detailed talk about, to play a friend for a reference.
First, the first step is how to download the joke from the free joke API interface to the text, API everyone knows is the application interface, that is, we can send requests, to obtain data. I choose Iteys, there is a request method example,
The Python implementation is as follows: The package needs to be loaded,

import sys, urllib, urllib2, jsonimport random

Send the request and get the data with URLLIB2, because the data fetched from the interface is the JOSN format by default (how to parse the JSON please refer to my other blog), need to parse into a python-supported DIC format, by generating random numbers, to stitch the requested URL to achieve a random page, the code is as follows:

page_num = random.randint(1,500‘http://api.1-blog.com/biz/bizserver/xiaohua/list.do?page=%d‘%page_numreq = urllib2.Request(url)resp = urllib2.urlopen(req)content = resp.read()if (content):        print‘content ok‘        #print content#解析json串dic_str = json.loads(content)

Next, by parsing the JSON to the title of each joke (JSON string corresponding to the field asked author) and content, splicing together to generate random numbers to achieve random extraction of 10 jokes,
The code is as follows:

num = Random.randint (0 , Span class= "Hljs-number" >9 ) for  i in  Range (0 , 10 ): Joke_str + = " title: '  + dic_str[ ' detail ' ][num + i][ author ' ] +  ' \ n '  +  content: '  + Dic_str[ ' detail ' ][num + i][ ' content ' ] +  \ n '  +  '---'  *20  +  \ n '  joke_str + = u ' Source: Iteye '   

Secondly, the code to send the message, after the previous step has obtained the content to send (JOKE_STR), the script sent the message has a relatively fixed idea: Call Smtplib module, SMTP server connection, SMTP server login, set encrypted transport, SMTP server sent, The SMTP server disconnects, the first pit appears – the problem with the mailbox: I have tried QQ mailbox and 163 mailbox, QQ mailbox must remember to open smpt service, and will get a separate password, SMTP server login to use this independent password, 163 mailbox is the default open, and login password is the email account password. The code and tips are as follows:

Import Smtplibimportstringhost=' smtp.163.com 'subject=' Happy new day~~~ 'to=[' mail inbox 1 ',' Mail inbox 2 ',' mail inbox x '] #实现群发功能FROM =' [email protected] 'text=joke_strbody=string. Join (' From:%s ' %from,' to:%s ' %to,' Subject:%s ' %subject,"',text),' \ r \ n ') Try:server=smtplib. SMTP () Server.connect (HOST,' + ') Server.starttls () Server.login (' ***@163.com ',' Password ') forIteminchTO:server.sendmail (from,item,body) #sendmail send the content asstringType, body/msg,as_string () server.quit()Print ' Mail sent successfully ~ 'Except Exception,e:Print ' Failure '+str (e)

Finally, after sending the mail script to complete, it is one step, send the message this simple rough work, of course, can not manually come ... , this step requires you to have a cloud host server, so that we can put the script to hang under the server, timed execution, not beautiful?? ~ ~, I use is Ubuntu14.04, can turn on the Crontab service, the steps are as follows: 1, set the scheduled task, execute crontab-e command edit, 2, CRONTAB-E format refer to Linux Operation Engineer's technical blog. 3. Restart Service crontab Restart, view crontab service, perform crontab-l if it appears as shown in Setup success, second pit: time zone problem, Cloud host default is UTC (Time zone), so when timing, Beijing times to subtract 8 hours. ,
After the above steps, this example completes the ~

Python instance---Access joke api+ send mail + cloud host crontab regular execution

Related Article

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.