python selenium 設置cookie

# 1.用於將cookie字符串轉換爲對象,因爲後面add_cookie需要傳字典進去
def ParseCookiestr(cookie_str):
    cookielist = []
    for item in cookie_str.split(';'):
        cookie={}
        itemname=item.split('=')[0]
        iremvalue=item.split('=')[1]
        cookie['name']=itemname
        cookie['value']=urllib.parse.unquote(iremvalue)
        cookielist.append(cookie)
    return cookielist
#加載谷歌驅動
    browser = webdriver.Chrome(executable_path=r"C:\Users\jing\Desktop\suningpy\chromedriver.exe")
    url="https://pin.suning.com/pgs/product/0071097856/11684494456.html?safp=f73ee1cf.MSFS_10292478.15093111.10&safc=prd.0.0"
    #打開url
    browser.get(url)
    cookielist=ParseCookiestr("_cp_dt=e1fe075c-e289-4843-a20d-308910f36092-43138;_device_session_id=p_1583115823846_1278033580342619;_df_ud=woshijingchunde;_snma=1%7C158268495152741387%7C1582684951527%7C1583132520048%7C1583132522427%7C108%7C6;_snmb=158313252005553763%7C1583132522444%7C1583132522432%7C2;_snmc=1;_snmp=158313252242067118;_snsr=direct%7Cdirect%7C%7C%7C;_snvd=1581992040883Ln8P6fT8M7A;_snzwt=TH1wo0170990e6b89gD388a27;authId=siB20ABFB9AC65F50C928DCA7FB955C540;city=1000106;cityCode=371;cityId=9106;custLevel=161000000110;custno=7113871039;districtCode=01;hm_guid=a79bf782-d06d-4875-a502-cc5a75fa2d7a;idsLoginUserIdLastTime=;logonStatus=2;nick=%E6%97%B7%E6%97%A5%E7%A7%AF%E6%99%B7...;nick2=%E6%97%B7%E6%97%A5%E7%A7%AF%E6%99%B7...;province=180;provinceCode=180;secureToken=F45D156C0CDEE953D6F68BB2A10E7735;sncnstr=sHwWSwkBxvS0ljRmp6ucRg%3D%3D;streetCode=3710199;tradeMA=191;_fp_t_=123,1583132521346;_portoData=6cc27f76-d2e6-4b65-99ae-49e5db137da9;route=eae9bfd253702942b0d3986bfc7e5aa7;SN_SESSION_ID=edaf327d-a199-477a-89a5-c03d0d9146d3")
    for item in cookielist: browser.add_cookie({
        'domain': '.suning.com',
        'name': item['name'],
        'value': item['value'],
        'path': '/',
        'expires': None
    })

這樣cookie就設置完成了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章