Last month I came across an idea to check my email inboxes using python. I did some research on the web and try to imitate the great solutions from other coders. Because I am currently using IMAP service in my main email accounts, I chose to use the “imaplib” module to achieve my expectation. Here are the codes:
#-*- encoding: utf-8 -*-
def gmail_checker(mailhost, username, password, flag = 0):
x, y=i.status('INBOX', '(MESSAGES UNSEEN)')
print "%s : total %i messages, %i unseen." % (username, messages, unseen)
if __name__ == '__main__':
gmail_checker('imap.163.com', 'firstname.lastname@example.org', 'xxxx', 1)
gmail_checker('imap.gmail.com', 'email@example.com', 'xxx',1)
gmail_checker('imap-mail.outlook.com', 'firstname.lastname@example.org','xxx', 1)
I needed this to help me because in China is kind of difficult to check my Gmail account and I want a simpler way to check my inboxes without opening bulkier email clients.
It’s been two months since I started learning python on my own. It all starts from the bottom of my heart, about what I really want to learn. To be honest, I genuinely feel that I’ve enjoyed this journey.
After reading related books and videos for quite a long time, I decided to get hands on the practice, which is more difficult as expected.
What drives me to write this small application is that I love watching sports games and I often checked a website, which is “www.zhibo8.cc”, to see what games are on live today. However, I found this action repetitive and a waste of time opening my website, selecting the bookmark and then scrolling down to the part which I want. I have tried several modules for web scraping and this time I chose to use BeautifulSoup to get the web page. Therefore, this module’s code is as follows:
from bs4 import BeautifulSoup
from datetime import *
url = 'http://www.zhibo8.cc/'
q = urllib2.urlopen(url)
soup = BeautifulSoup(q)
i = date.today()
today = soup.find(title=i)
#today1 = soup.find_all(class_='content')
#for i in today1:
# print i.li
today1 = today.next.next.next.next
for i in today1.ul:
if i.next.string == None:
print i.next.string + '\n' + i.next.next.string
It works pretty well and shows me what I need in the console. But after I came to the U.S., it started to raise an error saying the variable “today” is Nonetype. After debugging, I found it kind of funny because it is due to the time zone difference. Variable “i” here represents today in the U.S. while sometimes in China it is one day later. Obviously, the website “zhibo8” will delete all the live games information at the end of the day. So if I am lucky enough to run this application at night in Los Angeles, when it is tomorrow morning in China, I will encounter this error.
I plan to figure out a way to solve this problem later, and I am quite satisfied with my first executable python file after two months.