Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pycurl
- from StringIO import StringIO
- import re
- from urllib import urlencode
- import requests
- from lxml import html
- import cookielib
- params = {}
- HEADERS = [
- 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
- 'Content-Type: application/x-www-form-urlencoded',
- 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'
- ]
- login = 'login'
- password = 'pass'
- def curl_post(url=None, params=None):
- storage = StringIO()
- ch = pycurl.Curl()
- ch.setopt(ch.URL, url)
- ch.setopt(ch.HEADER, 1)
- ch.setopt(ch.CONNECTTIMEOUT, 30)
- ch.setopt(ch.WRITEFUNCTION, storage.write)
- if params is not None:
- if params.get('params'):
- ch.setopt(ch.POST, 1)
- ch.setopt(ch.POSTFIELDS, params['params'])
- if params.get('headers'):
- ch.setopt(ch.HTTPHEADER, params['headers'])
- if params.get('cookies'):
- ch.setopt(ch.COOKIE, params['cookies'])
- ch.perform()
- ch.close()
- result = storage.getvalue()
- headers, content = result.split('\r\n\r\n')
- cookies_list = re.findall(r'Set-Cookie: (.*);', headers, re.MULTILINE)
- cookies = ';'.join([c.split(';')[0] for c in cookies_list])
- with open('test.cookies', 'w') as cookies_file:
- cookies_file.write(cookies)
- return {'headers': headers, 'cookies': cookies, 'content': content}
- init_get = curl_post('https://vk.com', {'headers': HEADERS})
- tree = html.fromstring(init_get['content'])
- ip_h = tree.xpath("//form[@id='quick_login_form']/input[@name='ip_h']")[0].value
- lg_h = tree.xpath("//form[@id='quick_login_form']/input[@name='lg_h']")[0].value
- post_params = 'act=login&role=al_frame&%s&%s&%s&%s&%s' % (urlencode({'_origin': 'http://vk.com'}),
- urlencode({'ip_h': ip_h}),
- urlencode({'lg_h': lg_h}),
- urlencode({'email': login}),
- urlencode({'pass': password}))
- auth_post = curl_post('https://login.vk.com/?act=login', {'headers': HEADERS,
- 'params': post_params,
- 'cookies': init_get['cookies']})
- location = re.search(r'Location: (.*)', auth_post['headers'])
- if not location:
- print 'Can not auth'
- exit(1)
- authed = curl_post(location.group(1), {'headers': HEADERS,
- 'cookies': init_get['cookies']})
- remixsid = re.search(r'remixsid=\w*;', authed['cookies'])
- remixsid = remixsid.group()[:-1]
- im = curl_post('https://vk.com/im', {'headers': HEADERS,
- 'cookies': remixsid})
- with open('vk.html', 'w') as vk_file:
- vk_file.write(im['content'])
Advertisement
Add Comment
Please, Sign In to add comment