簡體   English   中英

無法使用請求登錄網站

[英]Can't log in to a site using requests

我正在嘗試使用請求模塊登錄該站點,但每次嘗試以下嘗試時都會收到403狀態代碼。 盡管我試圖模仿監控開發工具發送請求的方式,但我無法讓它工作。 我在這里使用的憑據(用戶名: simpndev@gmail.com ,密碼: +agb5E2?w2pQJ3z )僅用於測試目的,因此您可以免費使用。

要獲取表單,您需要做的就是單擊login按鈕,然后單擊Fantasy按鈕。

我試過:

import re
import requests

link = 'https://www.fanduel.com/contests'
url = 'https://api.fanduel.com/sessions'

payload = {"email":"simpndev@gmail.com","password":"+agb5E2?w2pQJ3z","product":"DFS"}

def log_in(s):
    r = s.get(link)
    client_id = re.findall(r"clientId\":\"(.*?)\",",r.text)[0]
    s.headers['authorization'] = f'Basic {client_id}'
    s.headers['Referer'] = 'https://www.fanduel.com/login'
    s.headers['accept'] = 'application/json'
    r = s.post(url,json=payload)
    print(r.status_code)

if __name__ == '__main__':
    with requests.Session() as s:
        s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36'
        log_in(s)

我發現使用 selenium 成功了,所以我不希望 go 這條路線。

如何使用請求登錄到該站點?

此請求涉及 2 個潛在的 403 錯誤。 來自link = 'https://www.fanduel.com/contests'機器人保護的 403 和來自對 /sessions/ 的請求的 403

來自link的 403 使用瀏覽器的一些高級功能來檢查重復嘗試登錄。 這將是一個更復雜的主題,涉及將 User-Agent 字符串與 HTTP2 流量進行比較,用 ML 擊敗驗證碼等等。 我的建議是不要沿着這條路走 go。

相反,請降低您的用戶代理字符串的版本,並確保您提供了正確的標頭。 您沒有為初始請求提供正確的標頭,因此您正在生成 403,然后您在機器人黑名單上並試圖對其進行管理。

以下對我有用,如下面的調試器屏幕截圖所示:

import re
import requests

link = 'https://www.fanduel.com/contests'
url = 'https://api.fanduel.com/sessions'

payload = {"email":"simpndev@gmail.com","password":"+agb5E2?w2pQJ3z","product":"DFS"}

def log_in(s):
    r = s.get(link)
    client_id = re.findall(r"clientId\":\"(.*?)\",",r.text)[0]
    s.headers['Authorization'] = f'Basic {client_id}'
    s.headers['Referer'] = 'https://www.fanduel.com/login?cc_success_url=%2Fcontests'
    s.headers['Accept'] = 'application/json'
    s.headers['Accept-Encoding'] = 'gzip, deflate, br'
    s.headers['Accept-Language'] = 'en-US,en;q=0.5'
    s.headers['Origin'] = "https://www.fanduel.com"
    r = s.post(url,json=payload)
    print(r.status_code)

if __name__ == '__main__':
    with requests.Session() as s:
        s.headers['User-Agent'] = 'Mozilla/5.0 (en-us) AppleWebKit/534.14 (KHTML, like Gecko; Google Wireless Transcoder) Chrome/9.0.597 Safari/534.14 wimb_monitor.py/1.0'
        log_in(s)

請注意,我修改了您的引用者,修改了 header 鍵的大小寫,並提供了可能被認為是多余的標題。 在檢查 r.request.headers 時,我看到了請求發送的內容與例如 Firefox 之間的差異,所以我只是添加了任何不同的內容。

另請注意,您的不記名令牌和帳戶憑據現在分布廣泛,如果您仍在使用它們進行測試,可能會導致額外的 403。 您需要一個干凈的帳戶,因為現在很多人可能擁有這些信用。

在此處輸入圖像描述

去過那里,過去使用 Selenium 進行 web 抓取時遇到了很多問題。

我更深入的一種替代方法是使用mitmproxy將導航腳本轉儲到文件並使用requests重放它,如下所示:

import mitmproxy.io
import requests

def main():
    sess = requests.Session()
    with open('flows', 'rb') as f: data = [row for row in mitmproxy.io.FlowReader(f).stream()]
    for d in data:
        url = d.request.url
        raw_content = d.request.raw_content
        headers = dict([(k, v,) for k, v in d.request.headers.items() if not k.startswith(':') and k.lower() != 'cookie'])
        print(url, raw_content, headers)
        if d.request.method == 'GET':
            r = sess.get(url, headers=headers, data=raw_content)
        if d.request.method == 'POST':
            r = sess.post(url, headers=headers, data=raw_content)
        print(r.status_code)
        if d.request.url == 'https://api.fanduel.com/sessions':
            break

if __name__ == '__main__':
    main()

上面的代碼(使用我自己從 mitmproxy 轉儲的流)導致下面的 output:

http://www.fanduel.com/ b'' {'Host': 'www.fanduel.com', 'Upgrade-Insecure-Requests': '1', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Mobile/15E148 Safari/604.1', 'Accept-Language': 'en-gb', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'keep-alive'}
200
https://www.fanduel.com/ b'' {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Mobile/15E148 Safari/604.1', 'accept-language': 'en-gb', 'accept-encoding': 'gzip, deflate'}
200
https://www.fanduel.com/JMCVuBG8/init.js b'' {'accept': '*/*', 'accept-encoding': 'gzip, deflate, br', 'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Mobile/15E148 Safari/604.1', 'accept-language': 'en-gb', 'referer': 'https://www.fanduel.com/'}
200
https://www.fanduel.com/login?source=Header%20Login b'' {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'accept-encoding': 'gzip, deflate, br', 'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Mobile/15E148 Safari/604.1', 'accept-language': 'en-gb', 'referer': 'https://www.fanduel.com/'}
200 
https://api.fanduel.com/sessions b'{"email":"simpndev@gmail.com","password":"+agb5E2?w2pQJ3z","product":"DFS"}' {'accept': 'application/json', 'origin': 'https://www.fanduel.com', 'content-type': 'application/json', 'authorization': 'Basic ZWFmNzdmMTI3ZWEwMDNkNGUyNzVhM2VkMDdkNmY1Mjc6', 'referer': 'https://www.fanduel.com/login', 'content-length': '75', 'accept-language': 'en-gb', 'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Mobile/15E148 Safari/604.1', 'accept-encoding': 'gzip, deflate, br'}
201

您所需要的只是一個設置了代理的設備,以通過您自己的 mitmproxy 安裝(我通常使用我的手機通過我的 PC 和 SOCKS5 路由請求)。 如果網站導航發生變化,您只需構建一個新流程並轉儲腳本以提供 python 程序。

這不會解決更復雜的 javascript 安全實現,但對於像本網站這樣的簡單實現來說應該足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM