User:Birgit Bachler/td problems

From XPUB & Lens-Based wiki

Comic

Using two different image sources from google image search. python nazibami.py > nazibami.html

User Birgit Bachler td problems nazibami.png
import urllib2, urlparse, html5lib

def absolutizeURL (href, base):
    if not href.lower().startswith("http://"):
        return urlparse.urljoin(base, href)
    return href

def openURL (url):
    """
    returns (page, actualurl)
    sets user_agent and resolves possible redirection
    realurl maybe different than url in the case of a redirect
    """    
    request = urllib2.Request(url)
    user_agent = "Mozilla/5.0 (X11; U; Linux x86_64; fr; rv:1.9.1.5) Gecko/20091109 Ubuntu/9.10 (karmic) Firefox/3.5.5"
    request.add_header("User-Agent", user_agent)
    pagefile=urllib2.urlopen(request)
    realurl = pagefile.geturl()
    return (pagefile, realurl)

parser = html5lib.HTMLParser(tree=html5lib.treebuilders.getTreeBuilder("dom"))

(f,bamiurl)=openURL("http://images.google.com/images?hl=en&source=hp&q=bami&gbv=2&aq=f&aqi=g10&aql=&oq=")
tree1 = parser.parse(f)
f.close()
tree1.normalize()
bamiimgs = tree1.getElementsByTagName("img")
# print tags

(f,naziurl)=openURL("http://images.google.com/images?hl=en&gbv=2&tbs=isch%3A1&sa=1&q=nazi&aq=f&aqi=g10&aql=&oq=&start=0")
tree2 = parser.parse(f)
f.close()
tree2.normalize()
naziimgs = tree2.getElementsByTagName("img")
# print tags

print """<html><body>"""


for (bami, nazi) in zip (bamiimgs, naziimgs):
    var=bami.getAttribute("src")
    var=absolutizeURL(var,bamiurl)
    if var != "http://images.google.com/images/nav_logo7.png":
        print "<img src='" + var + "' height='200' />"
    var=nazi.getAttribute("src")
    var=absolutizeURL(var,naziurl)
    if var != "http://images.google.com/images/nav_logo7.png":
        print "<img src='" + var + "' height='200' />"
	
print """</html></body>"""


Palindrome

Roethlisberger Case Cops Comb Bar for Clues for Bar Comb Cops Case Roethlisberger
Roethlisberger Accuser Drops Out Goes Home Goes Out Drops Accuser Roethlisberger
Rozlyn Papa Alleged Sex Tape Grand Opening Grand Tape Sex Alleged Papa Rozlyn
Quentin Tarantino Sued Over 'Kill Bill' 'Kill Over Sued Tarantino Quentin
MJ Tribute Band The Battle Over Neverland Over Battle The Band Tribute MJ
Dr. Phil I Don't Wanna Be on the Internet the on Be Wanna Don't I Phil Dr.
Court Rejects KKKrazy Lawsuit Against Dr. Drew Dr. Against Lawsuit KKKrazy Rejects Court
Tiger's Alleged Mistresses Who's on Top on Who's Mistresses Alleged Tiger's
Michael Lohan Among the Living the Among Lohan Michael
Matthias Jabs of the Scorpions: 'Memba Him 'Memba Scorpions: the of Jabs Matthias
Paps Sue Over 'Bachelor' Wedding Beat Down Beat Wedding 'Bachelor' Over Sue Paps
Levi Johnston Paid in Full in Paid Johnston Levi
Ryan O'Neal on Oscar Snub 'Poor Farrah' 'Poor Snub Oscar on O'Neal Ryan
Roethlisberger Attorney: Ben Is 'Innocent' Is Ben Attorney: Roethlisberger
Lil Wayne Arrives at Rikers Island Rikers at Arrives Wayne Lil
Sean Penn Close Encounter at Academy Awards Academy at Encounter Close Penn Sean
Cops: Ben Roethlisberger to be Interviewed Again Interviewed be to Roethlisberger Ben Cops:
New "Real Housewives" Who'd You Rather You Who'd Housewives" "Real New
Adam Lambert Cup Is Half Full Half Is Cup Lambert Adam


import feedparser
sugar = feedparser.parse("http://www.tmz.com/rss.xml")
    

for i in sugar.entries:
#    print i["title"]

#    text ="we did our homework"
    text = i["title"].replace("," , "").replace("!" , "").replace("-" , "").replace("?", "")
    words=text.split()


    backup = words[0:-1]
    words.reverse()
    print " ".join(backup + words)


Regular Expressions

Download romeo_and_juliet.txt and run regex.py

You say the cheek - I should the cheek
You will the airy - I pay the airy
You kiss the manner - I hit the manner
You have the contrary - I look'd the contrary
You gave the children - I beseech the children
You take the field - I use the field
You know the sweetest - I then the sweetest
You lie the tables - I pray the tables
You tallow the curtains - I doubt the curtains
You shall the join - I faith the join
You are the worshipp'd - I with the worshipp'd
You must the furious - I desire the furious


import re

text = open("romeo_and_juliet.txt").read()

You = re.findall(r"\bYou [a-z']+\b", text)
Me = re.findall(r"\bI [a-z']+\b", text)
The = re.findall(r"\bthe [a-z']+", text)

You = list(set(You))
Me = list(set(Me))

import random
random.shuffle(You)
random.shuffle(Me)
random.shuffle(The)

for (x,y,z) in zip(You,Me,The):
    print x,z,"-",y,z



Attachments