Python 2.7 version:
import os
import cookielib
import requests
cj = cookielib.MozillaCookieJar()
cookiepath = os.getcwd()
cj.load(os.path.join(cookiepath, 'cookies.txt'))
r = requests.get('https://stackoverflow.com', cookies=cj)
Python 3.6 version(tested):
import os
import http.cookiejar
import requests
cj = http.cookiejar.MozillaCookieJar()
cookiepath = os.getcwd()
cj.load(os.path.join(cookiepath, 'cookies.txt'))
r = requests.get('https://stackoverflow.com', cookies=cj)
print(r.cookies) #<RequestsCookieJar[Cookie(version=0, name='prov', value='619.....1a9f2f'....rfc2109=False)]>
print(r.headers) #{'Content-Type': 'text/html; charset=utf-8', 'Content-Encoding': 'gzip', 'X-Frame-Options': 'SAMEORIGIN'...}
"cookies.txt" must be a Netscape format cookies file. There are Firefox add-ons available to export cookies in this format. Also, make sure that the file is in the current working directory path for the above code to work.
cookielib
has been renamed tohttp.cookiejar
in Python 3. Confirm the version of Python. – amanb Mar 14 '18 at 18:45