import os import urllib import urllib2 import cookielib COOKIEFILE coo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
import urllib
import urllib2
import cookielib
COOKIEFILE = 'cookies.lwp'
URL = 'http://pda.vkontakte.ru/index?fromlogin=1&'
txdata = urllib.urlencode({
'email' : '',
'pass' : '',
})
txheaders = {
'User-Agent' : 'Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9.0.4) Gecko/2008112309 Iceweasel/3.0.4 (Debian-3.0.4-1)',
'Accept' : 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
'Accept-Language' : 'en-us,en;q=0.5',
'Accept-Encoding' : 'gzip,deflate',
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Keep-Alive' : '300',
'Connection' : 'keep-alive',
'Referer' : 'http://pda.vkontakte.ru/login?pda=index'
}
urlopen = urllib2.urlopen
Request = urllib2.Request
cj = cookielib.LWPCookieJar()
#if the file exists --> reading cookies
if os.path.isfile(COOKIEFILE):
cj.load(COOKIEFILE)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
req = Request(URL, txdata, txheaders)
handle = urlopen(req)
#saving cookies
cj.save(COOKIEFILE)