python 采用selenium+cookies 获取登录后的网页

百度网页由于需要登陆+手机短信验证。比较麻烦

这里我采用先人工登录百度账号,然后将百度账号的相关cookies保存下来

然后采用selenium动态登录网页

整体代码如下

from selenium import webdriver
    import time
    options = webdriver.ChromeOptions()
    options.add_argument('--start-maximized')  # 浏览器最大化
    options.add_argument('--disable-infobars')
    browser = webdriver.Chrome(options=options)
    browser.get('http://www.baidu.com')
    cookie_1 = {"name":"BAIDUID","value":"83D79E79B353728AA1824DACF6D670DC"}
    cookie_2 = {"name":"BDUSS","value":"pSUFZPT1ctbXlJeDJVZlZ1VWItWk9qYkVtNE0tZlNqWnZpRUNveHVuVUVSeTVsRVFBQUFBJCQAAAAAAAAAAAEAAABE1ecvwffQx9PqstDDzgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAS6BmUEugZlU3"}
    time.sleep(3)
    browser.add_cookie(cookie_1)
    browser.add_cookie(cookie_2)
    time.sleep(3)
    browser.get('http://www.baidu.com')
    time.sleep(10)

1、登录百度网页,查看源代码

找到2所示的两个关键字段 BAIDUID和BDUSS,并人工构造两个cookie

cookie_1 = {"name":"BAIDUID","value":"83D79E79B353728AA1824DACF6D670DC"}
cookie_2 = {"name":"BDUSS","value":"pSUFZPT1ctbXlJeDJVZlZ1VWItWk9qYkVtNE0tZlNqWnZpRUNveHVuVUVSeTVsRVFBQUFBJCQAAAAAAAAAAAEAAABE1ecvwffQx9PqstDDzgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAS6BmUEugZlU3"}

然后采用selenium 添加构造的两个cookie

browser.add_cookie(cookie_1)
browser.add_cookie(cookie_2)

接下来大功告成

这里有个更快捷的办法,直接把Cookies全部复制

再人工根据规则构造cookies

规则类似于如下代码所示

cookie_1 = {"name": "BAIDUID", "value": "83D79E79B353728AA1824DACF6D670DC"}

以下为总代码

def dongtai_BAIDU():
    """
    :return: 获取登录后的cookies 然后携带这些cookie
    """
    from selenium import webdriver
    import time
    options = webdriver.ChromeOptions()
    options.add_argument('--start-maximized')  # 浏览器最大化
    options.add_argument('--disable-infobars')
    browser = webdriver.Chrome(options=options)
    browser.get('http://www.baidu.com')
    # cookie_1 = {"name": "BAIDUID", "value": "83D79E79B353728AA1824DACF6D670DC"}
    # cookie_2 = {"name": "BDUSS",
    #             "value": "pSUFZPT1ctbXlJeDJVZlZ1VWItWk9qYkVtNE0tZlNqWnZpRUNveHVuVUVSeTVsRVFBQUFBJCQAAAAAAAAAAAEAAABE1ecvwffQx9PqstDDzgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAS6BmUEugZlU3"}
    cookies = "BIDUPSID=83D79E79B353728A8EC4C62E933EEF8A; PSTM=1694932781; BD_UPN=12314753; BA_HECTOR=8hak0k8gah81808ka4aha52l1igd7pd1p; ZFY=FFDC03Zc:Bp2wVP15g5U4cKd12L:B4UP88tb5D6i6ZhME:C; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BD_CK_SAM=1; PSINO=7; delPer=0; shifen[1858839_91638]=1694935272; shifen[1858839_87962]=1694935272; BCLID=11202995316399066065; BCLID_BFESS=11202995316399066065; BDSFRCVID=cOKOJexroG0Aahbq3iXuesms7eKK0gOTDYLEOwXPsp3LGJLVcRc7EG0PtjJ5HU4bLrA9ogKKLmOTHpuF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; BDSFRCVID_BFESS=cOKOJexroG0Aahbq3iXuesms7eKK0gOTDYLEOwXPsp3LGJLVcRc7EG0PtjJ5HU4bLrA9ogKKLmOTHpuF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=tJAj_D-btK03H48k-4QEbbQH-UnLq-J9W2OZ04n-ah02EJjd-RL5Mqk0bqbLb5b-W20j0h7m3UTdsq76Wh35K5tTQP6rLtJNKbv4KKJxbnckMqnaj-5dKxo-hUJiBM7LBan7QP5IXKohJh7FM4tW3J0ZyxomtfQxtNRJ0DnjtpChbRO4-TFaj6bLef5; H_BDCLCKID_SF_BFESS=tJAj_D-btK03H48k-4QEbbQH-UnLq-J9W2OZ04n-ah02EJjd-RL5Mqk0bqbLb5b-W20j0h7m3UTdsq76Wh35K5tTQP6rLtJNKbv4KKJxbnckMqnaj-5dKxo-hUJiBM7LBan7QP5IXKohJh7FM4tW3J0ZyxomtfQxtNRJ0DnjtpChbRO4-TFaj6bLef5; COOKIE_SESSION=0_0_0_1_0_1_1_0_0_1_0_0_0_0_0_0_0_0_1694935272%7C1%230_0_1694935272%7C1; BDUSS=pSUFZPT1ctbXlJeDJVZlZ1VWItWk9qYkVtNE0tZlNqWnZpRUNveHVuVUVSeTVsRVFBQUFBJCQAAAAAAAAAAAEAAABE1ecvwffQx9PqstDDzgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAS6BmUEugZlU3; BDUSS_BFESS=pSUFZPT1ctbXlJeDJVZlZ1VWItWk9qYkVtNE0tZlNqWnZpRUNveHVuVUVSeTVsRVFBQUFBJCQAAAAAAAAAAAEAAABE1ecvwffQx9PqstDDzgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAS6BmUEugZlU3; BDRCVFR[S4-dAuiWMmn]=I67x6TjHwwYf0; H_PS_PSSID=39310_39363_39279_39349_39097_39198_39261_39359_39233_26350; BAIDUID=83D79E79B353728AA1824DACF6D670DC:SL=0:NR=10:FG=1; sug=3; sugstore=1; ORIGIN=0; bdime=0; H_PS_645EC=429eEe9gpR3wfujbqACMgrQQ0Qa0BzvEMw9PZbFseOM5%2FslGgIVC3wEIxeUdoBbKjw; BAIDUID_BFESS=83D79E79B353728AA1824DACF6D670DC:SL=0:NR=10:FG=1"
    cookies = {i.split("=")[0]: i.split("=")[1] for i in cookies.split(";") if len(i.split("=")) > 0}
    cookies_ = {}
    for i in cookies:
        cookies_['name'] = i.replace(" ","")
        cookies_['value'] = cookies[i].replace(" ","")
        browser.add_cookie(cookies_)
    time.sleep(3)
    # browser.add_cookie(ret)
    # browser.add_cookie(cookie_1)
    # browser.add_cookie(cookie_2)
    time.sleep(3)
    # browser.add_cookie(cookies)
    browser.get('http://www.baidu.com')
    time.sleep(10)
相关推荐
awonw6 小时前
[java][框架]springMVC(1/2)
测试工具·postman
迃幵chen11 小时前
wireshark-网络分析工具
网络·测试工具·wireshark
孤蓬&听雨12 小时前
RabbitMQ自动发送消息工具(自动化测试RabbitMQ)
分布式·测试工具·自动化·rabbitmq·自动发送消息
土小帽软件测试12 小时前
jmeter基础01-2_环境准备-Mac系统安装jdk
java·测试工具·jmeter·macos·软件测试学习
qq_4337169515 小时前
测试分层:减少对全链路回归依赖的探索!
自动化测试·软件测试·功能测试·测试工具·回归·pytest·postman
qq_4337169516 小时前
Postman断言与依赖接口测试详解!
自动化测试·软件测试·功能测试·测试工具·mysql·接口测试·postman
如光照16 小时前
Linux与Windows中的流量抓取工具:wireshark与tcpdump
linux·windows·测试工具·网络安全
土小帽软件测试17 小时前
jmeter基础03_汉化jmeter界面
测试工具·jmeter·软件测试学习
小白~小黑20 小时前
软件测试基础十二(python变量进阶)
python·功能测试·测试工具·自动化
程序员小雷1 天前
软件测试基础:单元测试与集成测试
python·功能测试·selenium·测试工具·单元测试·集成测试·压力测试