How to display or extract all "cookie" information from a python page?

Asked

Viewed 120 times

0

I’m trying to extract the cookie from a page but I can only get information from the NID:

import requests

session = requests.Session()
response = session.get('https://www.google.com/')
print(session.cookies.get_dict())
{'NID': '204=nKJSokAbELYYO54Rj-qPjZ_ES0MckXrTdA_1dHceNFIOEU6S9qxT1Tn8ROe5o2lB2JxToKXw1o48sn_a1c5KvY8H60U7ZjDVheg-Dl1ahy31HydxgOfUXeGeSUGvdwEmNd_kMk3CWcl3d2SQpw5Iu8flaA_yROToJfJDo0WshBE'}

How to view more options like "DV", how do I do this? inserir a descrição da imagem aqui

1 answer

1

Attention, the cookie DV indicated in your question is contained in the request (Request), and not in the answer (Response)!

Some cookies can be generated dynamically. Probably, some code JavaScript was executed by the browser after the download of the content of the page, which led to the generation of these cookies.

The Python library requests does not interpret the content of the page, that is, if there is any code JavaScript in that content, it will not be executed.

An alternative solution is to use the library selenium, who is capable of enslave a browser (driver) to interpret the content of a given page, see only:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('https://www.google.com/')
driver.implicitly_wait(3)
cookies = driver.get_cookies()
print(cookies)
driver.close()

Exit:

[
  {'name': 'NID', 'value': '204=sJ0qzKNZMg1bjKafu3ogygb9K9pjfRPhlhNZoJbJ_CRHN0ISwwcjgl47xBjcl3YSOyC0szfBI_XLbkvFlLOkUtzfpCqiD-NYHrOuwQFeKjgc18uNrDfZLby2FpWUuN29Shot10tQA7WBpY_TqWGQjejycOSsvl3mdILX3lM6Ydw', 'path': '/', 'domain': '.google.com', 'secure': True, 'httpOnly': True, 'expiry': 1607022048},
  {'name': '1P_JAR', 'value': '2020-06-03-19', 'path': '/', 'domain': '.google.com', 'secure': True, 'httpOnly': False, 'expiry': 1593802849}
]
  • This way actually generates all the content, but to use without the presence of a browser(drive) would not be possible?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.