MOA

Security & Administration

Thursday, June 24, 2010

Customize your Google Search

Tuesday, June 22, 2010

Screenscraping with Python 3

import urllib.request

# reads CNN and decodes
page = urllib.request.urlopen('http://www.cnn.com/')
text = page.read().decode("utf8")

# where to find the text, change this to wherever ( would recommend using Firebug )
where = text.find('div id="cnn_mtt1lftarea"')
stopwhere = text.find('years')

# Add + how far after to print from the where and where to stop line
start= where + 28
ending = stopwhere + 7

price = text[start:ending]

print(price)

A simple Python 3 Email with google's smtp.



The code:

import smtplib
def promt(promt):
    return input(promt).strip()

# inputs the addresses
fromaddr=promt("From: ")
toaddrs=promt("To: ").strip()

print ("Enter message end with ^D (Unix) or ^Z (Winfail): ")

msg=("From: %s\r\nTo: %s\r\n\r\n"
     % (fromaddr, ", ".join(toaddrs)))

while True:       # loop
    try:
        line=input()
        except EOFError:
            break
        if not line:
            break
        msg=msg+line
print ("Message Lenght is ", len(msg))    # prints the lenght of the message
server=smtplib.SMTP("smt.gmail.com",587)   # server used to send smtp mail

server.starttls()  # tls
server.set_debuglevel(1) # remove this if you dont want to use debuglevel
server.login('emailaddr@gmail.com','Password')  # enter your smtp address and password

server.sendmail(fromaddr, toaddrs, msg)  # sends the email
server.quit()