python 模拟浏览器登陆coursera
import requests import random import string def randomString(length): return ''.join(random.choice(string.ascii_letters + string.digits) for i in range(length)) XCSRF2Cookie = 'csrf2_token_%s' % ''.join(randomString(8)) XCSRF2Token = ''.join(randomString(24)) XCSRFToken = ''.join(randomString(24)) cookie = "csrftoken=%s; %s=%s" % (XCSRFToken, XCSRF2Cookie, XCSRF2Token) url = "https://accounts.coursera.org/api/v1/login" logininfo = { "email":"...", "password":"...", "webrequest":"true" } user_agent = ( 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36' ) post_headers = {"User-Agent": user_agent, "Referer": "https://accounts.coursera.org/", "X-Requested-With": "XMLHttpRequest", "X-CSRF2-Cookie": XCSRF2Cookie, "X-CSRF2-Token": XCSRF2Token, "X-CSRFToken": XCSRFToken, "Cookie": cookie } coursera_session = requests.Session() login_res = coursera_session.post(url, data=logininfo, headers=post_headers, ) if login_res.status_code == 200: print("Login Successfully!") else: print(login_res.text)