Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - open a website and send cookies

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.

like image 323
KorniszoIszon Avatar asked Aug 06 '18 18:08

KorniszoIszon


2 Answers

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

like image 165
KorniszoIszon Avatar answered Sep 20 '22 19:09

KorniszoIszon


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.

like image 34
rennerj2 Avatar answered Sep 20 '22 19:09

rennerj2