I am searching for a method to get my cookies from my chrome browser (default), and use it using requests.
I have ofcourse already searched around, and found for example
Toms answer has worked very well for me and was even the only way for me to work in Windows 7 to crawl through sites that require a login. However, with Windows 10 and the Chrome 80 cookie handling (SameSite Cookies) there seems to be a new encryption - the cookie delivered by the "get_cookies" method had all empty values.
What worked for me now was browser_cookie3 (fork of browsercookie, was updated just some days ago to work with Chrome 80). I used this with request and selenium.
Install in an elevated prompt with
pip3 install browser-cookie3
Usage with request:
import browser_cookie3
cookies = browser_cookie3.chrome(domain_name='.google.com')
response = requests.get('http://www.google.com', verify=False, headers=headers, cookies=cookies, timeout=3)
Exchange google.com with the domain of the cookie you need. And be sure to include the timeout parameter or your script may freeze. headers is just an object with all your headers, e.g.
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36",
"Accept-Encoding":"gzip, deflate",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"DNT":"1",
"Connection":"close",
"Upgrade-Insecure-Requests":"1"
}
or something like that.
Usage with selenium:
import browser_cookie3
driver = webdriver.Chrome('./chromedriver')
cookies = browser_cookie3.chrome(domain_name='.google.com')
for c in cookies:
cookie = {'domain': c.domain, 'name': c.name, 'value': c.value, 'secure': c.secure and True or False}
driver.add_cookie(cookie)
driver.get('http://www.google.com')
./chromedriver is where my chromedriver.exe lies.