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.

KorniszoIszon
  • 111
  • 1
  • 6

2 Answers2

5

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

KorniszoIszon
  • 111
  • 1
  • 6
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.

rennerj2
  • 76
  • 8