Behdad Ahmadi Behdad Ahmadi - 5 days ago 5
Python Question

Login to Vk.com in python

I want to login to Vk.com in python. I dont want to use mechanize, twill or anything like that. I want to only use requests or urllib.

This is my code that I use to login but failed:

import requests
login_url = 'https://login.vk.com/?act=login'
payload = {'email':'EMAIL','pass':'PASSWORD'}
result = requests.get(login_url,data=payload).content


I also try with this payload:

payload = {'role':'al_frame','expire':'1','captcha_sid':'','captcha_key':'','_origin':'https%3A%2F%2Fvk.com','ip_h':'1bab73f98c2a0d4319','lg_h':'b2765a98e04fead77c','email':'EMAIL','pass':'PASSWORD'}


But both of them failed to login to Vk.com . How can I login to the website?

Answer

After some hours working on it, at the end I found the solutions for it.

import requests
from bs4 import BeautifulSoup as bs
headers={"Referer":"https://m.vk.com/login?role=fast&to=&s=1&m=1&email=YOUR_EMAIL"
,'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:50.0) Gecko/20100101 Firefox/50.0'}
payload = {'email':'YOUR EMAIL','pass':'PASSWORD'}

with requests.Session() as S:
    page = S.get('https://m.vk.com/login')
    soup = bs(page.content,'lxml')
    url = soup.find('form')['action']
    p = S.post(url,data=payload,headers=headers)
    #NOW YOU ARE SUCCESSFULLY LOGGED IN
Comments