簡體   English   中英

我無法在selenium中使用python請求會話cookie

[英]I'm not able to use python requests session cookies in selenium

我試圖通過它的外觀在Web瀏覽器中打開requests會話,似乎使用selenium是最有效/最佳的方式。

我的代碼:

import requests
from selenium import webdriver
from time import sleep

s = requests.Session()
s.get('https://www.sotf.com/en/nike/man/footwear/nike--joyride--cc3--setter--sneakers--at6395.html?RwDet=true&articoli_ID=17911')

driver = webdriver.Safari()

driver.get("https://www.sotf.com/")

for cookie in s.cookies:
    driver.add_cookie({
        'name': cookie.name, 
        'value': cookie.value,
        'path': '/',
        'domain': cookie.domain,
    })

driver.refresh()
sleep(1000)

當打印s.cookies.get_dict()我得到以下cookie:

{'__cfduid': 'dc81dd94c218523ce8161e4254d2652a01566815239', 'PHPSESSID': 'qhm7109shdrhu9uv3t38ani9df'}

問題是瀏覽器沒有使用這些cookie,當檢查safari中的cookie時(使用inspect元素) __cfduid看起來就像它應該但是由於未知原因我看到兩個PHPSESSID而且正確的有域名屬性設置為.wwww.sotf.com而不是www.sotf.com

在此輸入圖像描述

提前謝謝了。

PHPSESSID cookie存儲兩次,因為您打開頁面兩次 - 第一次打開帶有空cookie jar的頁面,而服務器設置第一個不安全的PHPSESSID cookie,然后從requests.Session復制第二個。 登陸主機后清除cookie; 在下面的示例中,我導航到https://www.sotf.com/404因為加載404頁面通常更快,清除默認cookie,然后從requests的cookie jar復制cookie:

import contextlib
import requests
from selenium import webdriver
from time import sleep


@contextlib.contextmanager
def init_driver():
    d = webdriver.Chrome()
    yield d
    d.quit()


if __name__ == '__main__':
    headers = {
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'en-US,en;q=0.9,de;q=0.8',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'none',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
    }

    params = {
        'RwDet': 'true',
        'articoli_ID': '17911',
    }

    s = requests.Session()
    s.get('https://www.sotf.com/en/nike/man/footwear/nike--joyride--cc3--setter--sneakers--at6395.html', headers=headers, params=params)
    print('cookies in requests jar:')
    for c in s.cookies:
        print(c)


    with init_driver() as driver:
        # 404 pages are usually faster to load
        driver.get("https://www.sotf.com/404")
        driver.delete_all_cookies()

        for cookie in s.cookies:
            driver.add_cookie({
                'name': cookie.name,
                'value': cookie.value,
                'path': '/',
                'domain': cookie.domain,
            })

        driver.get("https://www.sotf.com/")
        print('cookies in selenium jar:')
        for c in driver.get_cookies():
            print(c)

輸出:

cookies in requests jar:
<Cookie __cfduid=d54b8f9098af12dee16136e4dc641f74e1567012133 for .sotf.com/>
<Cookie PHPSESSID=mn28k5ta3ghfc77qb4nl23tga6 for www.sotf.com/>
cookies in selenium jar:
{'domain': 'www.sotf.com', 'expiry': 1598548157, 'httpOnly': False, 'name': 'cb-enabled', 'path': '/', 'secure': False, 'value': 'enabled'}
{'domain': 'www.sotf.com', 'httpOnly': False, 'name': 'PHPSESSID', 'path': '/', 'secure': True, 'value': 'mn28k5ta3ghfc77qb4nl23tga6'}
{'domain': 'sotf.com', 'httpOnly': False, 'name': '__cfduid', 'path': '/', 'secure': True, 'value': 'd54b8f9098af12dee16136e4dc641f74e1567012133'}

暫無
暫無

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

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