我给你贴一个我写的browser吧,希望你能用上。
import gzip
import socket
import urllib2
import cookielib
from StringIO import StringIO
class Browser(object):
def __init__(self, filecookie=None, PROXY=None):
VERSION = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/534.35 (KHTML, like Gecko) Chrome/13.0.761.0 Safari/534.35'
self.version = VERSION
self.headers = []
self.headers.append(('User-agent', self.version))
self.headers.append(('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'))
self.headers.append(('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'))
self.headers.append(('Accept-Encoding', 'gzip'))
self.headers.append(('Accept-Language', 'en-US,en;q=0.8'))
self.headers.append(('Connection', 'keep-alive'))
if filecookie:
self.cj = cookielib.MozillaCookieJar(filecookie)
else:
self.cj = cookielib.CookieJar()
if PROXY and 'http' in PROXY:
proxy_handler = urllib2.ProxyHandler(PROXY)
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj), proxy_handler)
else:
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
self.opener.addheaders = self.headers
def addheaders(self, headers):
self.opener.addheaders = self.headers + headers
def open(self, url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
if data:
pg = self.opener.open(url, data, timeout=timeout)
else:
pg = self.opener.open(url, timeout=timeout)
if
pg.info().get('Content-Encoding') == 'gzip':
buf = StringIO(pg.read())
f = gzip.GzipFile(fileobj=buf)
return f.read()
else:
return pg.read()