Hello Python

import urllib, urllib2, time, cookielib

def renrenLogin(username, password):
    cookiejar = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
    urllib2.install_opener(opener)
    request = urllib2.Request("http://www.renren.com")

    request.add_header('Accept', 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5')
    request.add_header('Accept-Language', 'zh-CN,zh;q=0.8')
    request.add_header('Accept-Charset', 'GBK,utf-8;q=0.7,*;q=0.3')
    request.add_header('Keep-Alive', '300')
    request.add_header('Connection', 'keep-alive')
    request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; hu-HU; rv:1.7.8) Gecko/20050511 FireFox/1.0.4')
    request.add_header('Cache-Control', 'max-age=0')# if not contains, cache will be got 

    params = urllib.urlencode(
        {
            'email': username,
            'password': password,
            'origURL': 'http://www.renren.com/Home.do',
        }
    )

    data = urllib2.urlopen(request, params)
    
    logfile = open('log.html', 'w')
    logfile.write(data.read())
    logfile.close()

if __name__=='__main__':
    renrenLogin('[email protected]', '********')

你可能感兴趣的:(windows,cache,python,header,email,import)