Unable to login Twitch TV via script

末鹿安然 提交于 2019-12-12 10:23:06

问题


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':'&#x2713;','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


回答1:


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:

pip install -U selenium 

Here is a Python solution using Selenium.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from bs4 import BeautifulSoup

my_username = "volatil3_"
my_password = "thisispassword"

driver = webdriver.Firefox()
driver.get("http://www.twitch.tv/user/login")
elem_user = driver.find_element_by_id("login_user_login")
elem_passwd = driver.find_element_by_id("user[password]")
elem_user.send_keys(my_username)
elem_passwd.send_keys(my_password + Keys.RETURN)
# In case it need some time to populate the content.
#time.sleep(5)

html = driver.page_source
soup = BeautifulSoup(html)
logginTag = soup.find("a", {"id" : "user_display_name"})
print(logginTag)
driver.close()

And here is the output:

<a class="header_nick button drop" href="#" id="user_display_name">volatil3_</a>



回答2:


PhantomJS for Twitch Login, see my question here

var page = require('webpage').create();

page.open('http://www.twitch.tv/login', function() {
    page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
        page.evaluate(function() {
            $("#login_user_login").val("username");
            $("[id='user[password]']").val("password");
            $(".button.primary:first").click(); // click login button
        });
        setTimeout(function(){
            page.render("e.png"); // see if anything happens
            phantom.exit();
        }, 5000); // 5 seconds
    });
});


来源:https://stackoverflow.com/questions/25703924/unable-to-login-twitch-tv-via-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!