Python - open a website and send cookies
Asked Answered
A

2

5

How to send a cookie to the webbrowser using Python (I am using version 3.7)?

I know how to open a website:

import webbrowser
webbrowser.open("http://www.example.com", new=2)

But I have no idea, how to open that site with some cookies saved somewhere.

Aphyllous answered 6/8, 2018 at 18:44 Comment(3)
I'm not sure but I think you need to send a GET or POST request obj and put those cookies in it.Prompter
What have you tried so far?Sayles
Now, I am looking into selenium webdriver. I hope it will give me a solution.Aphyllous
A
6

I solved the problem using selenium and a webdriver.

from selenium import webdriver

browser = webdriver.Chrome()

browser.get("http://www.example.com")
browser.add_cookie({
    'name' : 'myLovelyCookie',
    'value' : 'myLovelyValue'
})

And the result: Cookie

Aphyllous answered 7/8, 2018 at 19:18 Comment(0)
E
0

Not sure how to do it using the webbrowser library, but this can be easily done with the requests library. For example:

import requests
cookie = {
    'uid': 'example_user_id', 
}
url = "https://example.com"
req = requests.get(url, cookies=cookie)

From there you can read the content of the server’s response, contained in req.

Estipulate answered 7/8, 2018 at 12:16 Comment(2)
But how to open browser now?Aphyllous
Ah, I see what you mean. Yeah not sure how to open it in a browser, this would just get all the info from that URL with the cookies.Estipulate

© 2022 - 2024 — McMap. All rights reserved.