简体   繁体   中英

Basic get request, not returning cookies

I am doing a simple get request to get a website cookies:

import requests

with requests.Session() as s:

    session = requests.Session()

    response = session.get("http://www.dailymail.co.uk/home/index.html")
    ncooks = session.cookies.get_dict()
    print(ncooks)

But, when the ncooks gets returned it is empty {}

Why is this? How can I solve this problem to get the cookies for the website?

Your code works, the website you're using just doesn't set a cookie. Try with http://nytimes.com , the dict won't be empty.

Unrelated, but you're creating an unnecessary extra Session object; it should look like this:

import requests

with requests.Session() as s:
    response = s.get("http://nytimes.com")
    cooks = s.cookies.get_dict()
    print(cooks)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM