0

Python 経由で Twitch.tv Web サイトにログインしようとしています。すべてのパラメーターを指定しても、ログインできません。以下はコードです。

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()

理想的には、ログイン後にホームページに戻り、ログインしたユーザーの名前を表示する必要があります。私にとっては、ログインしていないことを示すhtmlが表示されます。助けてください

ここで指定されたユーザー/パスワードは本物であり、テストに使用できます

4

2 に答える 2