【问题标题】:I'm not able to use python requests session cookies in selenium我无法在 selenium 中使用 python 请求会话 cookie
【发布时间】:2019-12-30 13:20:27
【问题描述】:

我正在尝试在网络浏览器中打开一个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 时(使用检查元素)__cfduid 看起来应该是这样,但由于未知原因,我看到两个 PHPSESSID而正确的域属性设置为.wwww.sotf.com 而不是www.sotf.com

非常感谢。

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver python-requests


    【解决方案1】:

    PHPSESSID cookie 被存储了两次,因为您打开了两次页面 - 第一次使用空 cookie jar 打开页面,而服务器设置了第一个不安全的 PHPSESSID cookie,然后您从 @ 复制第二个987654323@。登陆主机后清除 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'}
    

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 2013-03-24
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      • 2019-07-29
      • 2023-03-11
      • 2017-05-29
      • 1970-01-01
      相关资源
      最近更新 更多