I am trying to login to Twitch.tv website via Python. Despite of giving all parameters it's still not letting me to log in. Below is the code:
import requests
from bs4 import BeautifulSoup
from time import sleep
# #user[login]:volatil3_
# user[password]:thisispassword
#https://secure.twitch.tv/user/login
# <a href="#" class="header_nick button drop" id="user_display_name"> volatil3_ </a>
def connect():
user = {'Username':'volatil3_','Password':'thisispassword'}
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36','Referer':'http://www.twitch.tv/user/login'}
with requests.Session() as s:
html = s.get("http://www.twitch.tv/user/login", headers=headers, verify=False, timeout=5)
soup = BeautifulSoup(html.text)
tokenTag = soup.find("input", {"name" : "authenticity_token"})
token = tokenTag["value"].strip()
#print(html.text)
print("-----------------------------------------------")
credentials = {"user[login]":'volatil3_', "user[password]":'thisispassword',"authenticity_token":token,'redirect_on_login':'https://secure.twitch.tv/user/login','embed_form':'false','utf8':'✓','mp_source_action':'','follow':''}
print(credentials)
s.post("https://secure.twitch.tv/user/login", data = credentials, headers=headers, verify=False, timeout=10,allow_redirects=True)
#html = s.get("http://www.twitch.tv", headers=headers, verify=False, timeout=5)
soup = BeautifulSoup(html.text)
logginTag = soup.find("a", {"id" : "user_display_name"})
print(logginTag)
if "Log In" in html.text:
print("cound not log in")
connect()
Ideally after login it should get back to home page and show name of Logged IN user. For me it's shows html that indicates it is not logged in. Help me please
The user/password given here are Real and can be used for testing
I took a quick look at the site you want and found it to be really heavy javascript. After the login post request, it will follow a redirect, and in the new page, most of the contents are generated by Javascript which is really a hassle to work with either using request, urllib2, ..etc... Seems like you are only at stage1: login, after that, probably tons of work really cannot not be guaranteed without using a Javascript engine like PhantomJS, Selenium. Here is a POC that I wrote using Selenium in Python. Hope will be helpful.
To install Selenium:
Here is a Python solution using Selenium.
And here is the output:
PhantomJS for Twitch Login, see my question here