【发布时间】:2022-08-24 14:16:25
【问题描述】:
首先,对不起我的英语不好
我想用剧作家来弄饼干,但我不能。 我尝试了 3 种我找到的方法,但一无所获。
- 使用
page.onpage.on(\'request\',get_cookie) page.on(\'response\',get_cookie) def get_cookie(request): allheaders = request.all_headers() print(allheaders) >>> {\'accept-ranges\': \'bytes\', \'age\': \'9576\', \'cache-control\': \'max-age=600\', \'content-length\': \'6745\', \'content-type\': \'image/png\', \'date\': \'Thu, 30 Jun 2022 01:09:20 GMT\', \'etag\': \'\"206578bcab2ad71:0\"\', \'expires\': \'Thu, 30 Jun 2022 01:19:20 GMT\', \'last-modified\': \'Tue, 06 Apr 2021 06:11:52 GMT\', \'server\': \'NWS_SPMid\', \'x-cache-lookup\': \'Cache Hit\', \'x-daa-tunnel\': \'hop_count=1\', \'x-nws-log-uuid\': \'16892018456232999193\', \'x-powered-by\': \'ASP.NET\'} {\'accept-ranges\': \'bytes\', \'age\': \'9576\', \'cache-control\': \'max-age=600\', \'content-length\': \'6745\', \'content-type\': \'image/png\', \'date\': \'Thu, 30 Jun 2022 01:09:20 GMT\', \'etag\': \'\"206578bcab2ad71:0\"\', \'expires\': \'Thu, 30 Jun 2022 01:19:20 GMT\', \'last-modified\': \'Tue, 06 Apr 2021 06:11:52 GMT\', \'server\': \'NWS_SPMid\', \'x-cache-lookup\': \'Cache Hit\', \'x-daa-tunnel\': \'hop_count=1\', \'x-nws-log-uuid\': \'16892018456232999193\', \'x-powered-by\': \'ASP.NET\'} ...(and more like this)返回了一些东西,但这里没有 cookie
- 使用
browser_context.cookies已解决!感谢@Charchitcontext = browser.new_context(); page = context.new_page() page.goto(url) cookies = context.cookies print(cookies) >>> <bound method BrowserContext.cookies of <BrowserContext browser=<Browser type=<BrowserType name=chromium executable_path=/Users/swong/Library/Caches/ms-playwright/chromium-1005/chrome-mac/Chromium.app/Contents/MacOS/Chromium> version=102.0.5005.40>>>
- 使用 JS
cookie = page.evaluate(\'console.log(document.cookie)\') print(cookie) >>> None我从 Chromium 页面打开了网络选项卡,在 Requests\' 标头中有我想要的 cookie。
请帮助我,谢谢大家!
这是我的代码示例。该网站是中文的,希望您不要介意。这只是一个简单的登录页面。
from playwright.sync_api import sync_playwright url = \'https://so.gushiwen.cn/user/login.aspx\' def get_cookie(request_or_reqponse): headersArray = request_or_reqponse.headers_array() print(\'「headersArray」:\', headersArray) with sync_playwright() as p: browser = p.chromium.launch(headless=False) context = browser.new_context(); page = context.new_page() page.goto(url) page.fill(\'#email\',\'6j3y4ecy@spymail.one\') page.fill(\'#pwd\', \'6j3y4ecy@spymail.one\') page.wait_for_timeout(5000) # imput the captcha code manually page.on(\'request\',get_cookie) page.on(\'response\',get_cookie) print(\'loging in...\') page.click(\'#denglu\') page.wait_for_timeout(50000) # wait for nothing browser.close()
-
您可以使用 URL 创建minimal reproducible example,以便我们可以复制和测试代码。
-
解决它。对不起,我是新人。
标签: python cookies scrapy playwright