Python 3.x 如何查看发出GET请求时设置的所有cookie

Python 3.x 如何查看发出GET请求时设置的所有cookie,python-3.x,cookies,python-requests,Python 3.x,Cookies,Python Requests,参考 OP在Chrome上看到了许多cookie,但在他的Python请求代码中没有看到大多数cookie。给出的原因是设置的cookie来自其他页面/资源,可能是由JavaScript代码加载的 这是我用来尝试获取访问URL时加载的Cookie的函数: from requests import get from requests.exceptions import RequestException from contextlib import closing def get_cookies(

参考

OP在Chrome上看到了许多cookie,但在他的Python请求代码中没有看到大多数cookie。给出的原因是设置的cookie来自其他页面/资源,可能是由JavaScript代码加载的

这是我用来尝试获取访问URL时加载的Cookie的函数:

from requests import get from requests.exceptions import RequestException from contextlib import closing def get_cookies(url): """ Returns the cookies from the response of `url` when making a HTTP GET request. """ try: s = Session() with closing(get(url, stream=True)) as resp: return resp.cookies except RequestException as e: print('Error during requests to {0} : {1}'.format(url, str(e))) return None
但是使用这个函数,我只看到由URL设置的cookie,而没有其他像广告cookie这样的cookie。鉴于这种设置,我如何查看其他cookie,就像Chrome如何查看它们一样?也就是说,当发出GET请求时,我如何查看所有cookie的设置,包括来自其他页面/资源的cookie?

花了一点功夫,但我成功地使其工作。 基本上需要硒和铬实际加载网站和所有第三方的东西。其中一个输出是./chrome\u dir/Default/cookies中的一个sqlite3 cookies数据库,您可以获取该数据库供自己使用

from selenium import webdriver import sqlite3 def get_cookies(url): """ Returns the cookies from the response of `url` when making a HTTP GET request. """ co = webdriver.ChromeOptions() co.add_argument("--user-data-dir=chrome_dir") # creates a directory to store all the chrome data driver = webdriver.Chrome(chrome_options=co) driver.get(url) driver.quit() conn = sqlite3.connect(r'./chrome_stuff/Default/Cookies') c = conn.cursor() c.execute("SELECT * FROM 'cookies'") return c.fetchall()