python requests get cookies
Alternatively, you can use requests.Session
and observe cookies
before and after a request:
>>> import requests
>>> session = requests.Session()
>>> print(session.cookies.get_dict())
{}
>>> response = session.get('http://google.com')
>>> print(session.cookies.get_dict())
{'PREF': 'ID=5514c728c9215a9a:FF=0:TM=1406958091:LM=1406958091:S=KfAG0U9jYhrB0XNf', 'NID': '67=TVMYiq2wLMNvJi5SiaONeIQVNqxSc2RAwVrCnuYgTQYAHIZAGESHHPL0xsyM9EMpluLDQgaj3db_V37NjvshV-eoQdA8u43M8UwHMqZdL-S2gjho8j0-Fe1XuH5wYr9v'}
How to send cookies in a post request with the Python Requests library?
The latest release of Requests will build CookieJars for you from simple dictionaries.
import requests
cookies = {'enwiki_session': '17ab96bd8ffbe8ca58a78657a918558'}
r = requests.post('http://wikipedia.org', cookies=cookies)
Enjoy :)
using the requests.session object to set cookies in order to access page
Your header is wrong, try it like this:
import requests
import json
s=requests.Session()
url="https://seekingalpha.com/api/v3/symbols/hsy/press-releases"
s.headers={
"accept": "application/json, text/plain, */*",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache",
"pragma": "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
}
r=s.get(url)
print(r.text)
After you can parse response:
m=json.loads(r.text)
Python: How to use Chrome cookies in requests
I have a good script to read Chrome cookies directly on /Default/Cookies. I think you would work fine.
import sqlite3
import sys
from os import getenv, path
import os
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import PBKDF2
import keyring
def get_cookies(url, cookiesfile):
def chrome_decrypt(encrypted_value, key=None):
dec = AES.new(key, AES.MODE_CBC, IV=iv).decrypt(encrypted_value[3:])
decrypted = dec[:-dec[-1]].decode('utf8')
return decrypted
cookies = []
if sys.platform == 'win32':
import win32crypt
conn = sqlite3.connect(cookiesfile)
cursor = conn.cursor()
cursor.execute(
'SELECT name, value, encrypted_value FROM cookies WHERE host_key == "' + url + '"')
for name, value, encrypted_value in cursor.fetchall():
if value or (encrypted_value[:3] == b'v10'):
cookies.append((name, value))
else:
decrypted_value = win32crypt.CryptUnprotectData(
encrypted_value, None, None, None, 0)[1].decode('utf-8') or 'ERROR'
cookies.append((name, decrypted_value))
elif sys.platform == 'linux':
my_pass = 'peanuts'.encode('utf8')
iterations = 1
key = PBKDF2(my_pass, salt, length, iterations)
conn = sqlite3.connect(cookiesfile)
cursor = conn.cursor()
cursor.execute(
'SELECT name, value, encrypted_value FROM cookies WHERE host_key == "' + url + '"')
for name, value, encrypted_value in cursor.fetchall():
decrypted_tuple = (name, chrome_decrypt(encrypted_value, key=key))
cookies.append(decrypted_tuple)
else:
print('This tool is only supported by linux and Mac')
conn.close()
return cookies
if __name__ == '__main__':
pass
else:
salt = b'saltysalt'
iv = b' ' * 16
length = 16
#get_cookies('YOUR URL FROM THE COOKIES', 'YOUR PATH TO THE "/Default/Cookies" DATA')
Related Topics
Defining the Midpoint of a Colormap in Matplotlib
What Makes Sets Faster Than Lists
How to Search Sub-Folders Using Glob.Glob Module
Is Module _File_ Attribute Absolute or Relative
Typeerror: Objectid('') Is Not JSON Serializable
Pandas Get Column Average/Mean
Driving Excel from Python in Windows
How to Parse a Time String Containing Milliseconds in It with Python
What's the Difference Between "Pip Install" and "Python -M Pip Install"
Replacing Numpy Elements If Condition Is Met
Python Urllib2, Basic Http Authentication, and Tr.Im
Django, Creating a Custom 500/404 Error Page
Writing String to a File on a New Line Every Time
Python: Binding Socket: "Address Already in Use"
Error While Importing Tensorflow in Python 2.7 in Ubuntu 12.04. 'Glibc_2.17 Not Found'
MAC Osx Python Ssl.Sslerror: [Ssl: Certificate_Verify_Failed] Certificate Verify Failed (_Ssl.C:749)