为什么我可以在剧作家中获得cookie值?

发布于 2025-02-11 19:37:33 字数 3235 浏览 1 评论 0原文

首先,我的英语不好

我想使用剧作家来获得饼干,但我不能。 我尝试了三种方法,但一无所获。

  1. 使用page.on
page.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


  1. 使用browser_context.cookies已解决! Thx用于@Charchit
context = 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>>>

  1. 使用JS,
cookie = page.evaluate('console.log(document.cookie)')
print(cookie)

>>>
None

我从Chromium页面打开了网络选项卡,我想要的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','[email protected]')
    page.fill('#pwd', '[email protected]')

    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()

Firstly, sry for my poor English

I want to use playwright to get the cookie, but I can't.
I tried 3 ways I've found, and got nothing.

  1. Using page.on
page.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)

returned somthing, but no cookie here

  1. Using browser_context.cookies Resolved! Thx for @Charchit
context = 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>>>

  1. Using JS
cookie = page.evaluate('console.log(document.cookie)')
print(cookie)

>>>
None

I opened the network tab from the Chromium page,there was the cookie I want in Requests' header.

please help me, Thank you all!

Here's my code example. The site is in Chinese language, and hope you will not mind it. It's just a simple login page.

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','[email protected]')
    page.fill('#pwd', '[email protected]')

    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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

享受孤独 2025-02-18 19:37:33

在您的第二种方法中,将cookies = context.cookies更改为cookies = cockies = context.cookies()。这是一种方法,您需要称呼它。检查 documentation> documentation

context = browser.new_context();
page = context.new_page()
page.goto(url)
cookies = context.cookies()
print(cookies)

方法不建议。这是因为即使您从响应中获得cookie标题,除非您使用出厂功能或全局变量,否则您也无法真正存储并使用它。此外,为什么当browsercontext特别具有一种方法时,为什么要这样做:)

edit

第一个方法似乎不起作用的原因是因为它返回请求的标题和响应制成。 cookies也可以通过页面本身上的JavaScript创建,这些cookies可能根本不会出现在标题中。

其次,从您的问题中为第一个方法打印出的标题,似乎只是一个请求。运行代码后,收到了更多的请求和响应,这些请求和响应将其打印出更多的标题。特别是从响应中,您可以通过搜索标题'set-cookie'来检索服务器设置的cookie。

In your second method, change cookies = context.cookies to cookies = context.cookies(). It's a method, you need to call it. Check the documentation:

context = browser.new_context();
page = context.new_page()
page.goto(url)
cookies = context.cookies()
print(cookies)

Also, doing it like your first method is not advisable. This is because even if you get the Cookie header from the response, you can't really store and use it else where unless you use a factory function or a global variable. Besides, why do that when BrowserContext specifically has a method for it :)

Edit

The reason the first method seemingly does not work is because it returns the headers of the request and responses made. Cookies can also be created through javascript on the page itself, these may not show up in the headers at all.

Secondly, from the headers you have printed out for the first method in your question, it seems like it was only for a single request. After running your code, there were a lot more requests and responses received, which in place printed out a lot more headers. From the responses in particular, you can retrieve the cookies set by the server by searching for the header 'set-cookie'.

江湖正好 2025-02-18 19:37:33

它对我有用

browser = playwright.chromium.launch()
page = browser.new_page()
page.goto('https://stackoverflow.com')
cookies = page.context.cookies()
print(cookies)

it works for me

browser = playwright.chromium.launch()
page = browser.new_page()
page.goto('https://stackoverflow.com')
cookies = page.context.cookies()
print(cookies)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文