我想通過Python登錄到Twitch.tv網站。儘管給予所有參數它仍然沒有讓我登錄下面是代碼:無法通過腳本登錄Twitch電視
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()
理想的情況下登錄之後應該回到主頁,顯示登錄的用戶名。對我來說這顯示HTML,表示它沒有登錄。請幫助我
這裏給出的用戶名/密碼是真實的,可用於測試
首先是..看起來像變量'html'只包含舊的HTML ..我不認爲你登錄後,新的HTML將存儲在那裏。即使你的文章有效,並且你登錄了,你也可以通過調用'result = s.post(..)''newhtml = request._content'來訪問新的html。 – 2014-09-07 20:12:43