Help with urllib2 in python?

by on January 23, 2012

Question by Bob P: Help with urllib2 in python?
I am having trouble with this script:

#!/usr/bin/python

import urllib2
import urllib

print “Content-Type: Text/html\n\n”

print “Beginning of page”

url = “”"http://translate.google.com/translate_t?hl=en&ie=UTF-8&text=gato&sl=es&tl=en#en|es|my%20cat%20is%20red%0A”"”

data = None

txheaders = {
‘User-Agent’: ‘Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)’,
‘Accept-Language’: ‘en-us’,
‘Accept-Encoding’: ‘gzip, deflate, compress;q=0.9′,
‘Keep-Alive’: ’300′,
‘Connection’: ‘keep-alive’,
‘Cache-Control’: ‘max-age=0′,
}

req = urllib2.Request(url, data, txheaders)
response = urllib2.urlopen(req)
page = response.read()
info = response.info()

fileObj = open (“htmldata.txt”,”w”)
fileObj.write(page)
fileObj.close()

print page

print “End of page”

This is the link to the program on the web: http://silo.cs.indiana.edu:8334/cgi-bin/equilibrium

Why does it show all that gobbly gook?

Best answer:

Answer by mauvais écureuil
The “url” variable appears to be truncated; I take it you have a translate URL?

At any rate. You’re asking why it shows what, exactly?

Add your own answer in the comments!

Leave a Comment

Previous post:

Next post: