简体   繁体   English

如何使用 pyppeteer 设置 cookie

[英]How to set cookies with pyppeteer

I know barely anything about cookies, but I need to set them in order to make my program work.我对 cookie 几乎一无所知,但我需要设置它们以使我的程序正常工作。 Let's say I have these cookies:假设我有这些饼干:

 "fl-test-cookie-exist=Exist; fl-notice-cookie=true; country_notify=true; _svtri=6b01b3be-4fe8-4b91-8282-1613818f3329; _ga=GA1.2.842004217.1579404024; _fbp=fb.1.1579404024541.600339844; scarab.visitor=%226461D28971E3E0BA%22; _hjid=b601b76b-f122-4ba9-9a8c-d6fcdf5f368e; _crbx=b3b2745b-8fb4-4cb8-bd34-c69f479d1cfa; __zlcmid=wKiqd1GkJlTkIi; fita.sid.footlocker_eu=6k4vL-qyM8JRJG2CFSe2sm6brf9WYjz3; OptanonAlertBoxClosed=2020-01-19T03:48:01.217Z; _abck=C64424015AC0B0105676118215BC44C2~0~YAAQdAIkF11dnoJuAQAALofruwNEbc5wL+ZOWzzIFS8amV/qlm6lkf29ulMcoZEzJfnOdRYu0CrNf3+ja0SbwdkPslNfvAcIITMBBGg77CNWOPLcQqIUWKOvLU3EmFbhANnkXICl0Ch3Lbu2pD1e3rJtf8Q/QzbJmAt/gX8PrqrovJfn9wLtyGR+vDE1sEXOB/yYuOeik5M+0oyJUrQRPm698nUWdZ1oVqJd88OGwVpOmE2rmN+kFJhb1qOPMJBZlsMvAxDj1niiMfBWEJvrb4mIpf7vDD4uaz/TkJ7z7OB/uaOqNo7x7o1P1EwLjPizMmgvujjwL5i5xQ==~-1~-1~-1; sto-id-47873=ALPOBFKMPOCA; check=true; mboxEdgeCluster=28; mbox=PC#e6cd0cbb3290401490c41788f9eacc1d.28_0#1643685841|session#b5eae18e8bbd49fba332e7b053220170#1580442897; OptanonConsent=landingPath=NotLandingPage&datestamp=Thu+Jan+30+2020+19%3A24%3A00+GMT-0800+(Pacific+Standard+Time)&version=3.6.25&groups=1%3A1%2C2%3A1%2C3%3A1%2C4%3A1%2C101%3A1%2C102%3A1%2C103%3A1&AwaitingReconsent=false; _svlet=1580441040982; _gid=GA1.2.670261469.1580441041; scarab.mayAdd=%5B%7B%22i%22%3A%22314100494104%22%7D%5D; _svs=%7B%22p%22%3A%7B%220%22%3A1580441037477%2C%227%22%3A1580441037478%7D%2C%22c%22%3A%7B%221%22%3Atrue%2C%222%22%3Atrue%2C%223%22%3Atrue%2C%224%22%3Atrue%7D%2C%22ct%22%3A1580441044719%2C%22e%22%3A%7B%2225%22%3A%7B%22group%22%3A27%2C%22sent%22%3Atrue%7D%7D%7D"

and I have to set them with pyppeteer ( https://miyakogi.github.io/pyppeteer/reference.html#page-class ), now I already split the cookies up with SimpleCookie and I converted the cookies into a dictionary.我必须用 pyppeteer ( https://miyakogi.github.io/pyppeteer/reference.html#page-class ) 设置它们,现在我已经用 SimpleCookie 拆分了 cookie,并将 cookie 转换为字典。

My question is: what is supposed to go in all those field (name and value) which of those would be the name and which would be the value?我的问题是:所有这些字段(名称和值)中应该包含哪些内容,哪些是名称,哪些是值? I have multiple cookies, so how do I set them all?我有多个 cookie,那么我该如何设置它们呢?

Thank you in advance <3提前致谢 <3

You can set cookie for page with:您可以为页面设置 cookie:

await page.setCookie(*cookie)

Where cookie is a list containing python dict with the cookie you have there.其中 cookie 是一个包含 python dict 和你在那里的 cookie 的列表。 You have to turn it into python dict.你必须把它变成python dict。 Result should be list like this:结果应该是这样的:

cookie = [{'fl-test-cookie-exist':'Exist',
         'fl-notice-cookie'= True,
         'country_notif': True,
         ... }]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM