Python请求-抓取网站Cookie

时间:2020-07-21 22:03:25

标签: python-3.x python-requests

我有以下简单的脚本试图从状态URL抓取并打印会话cookie:

import sys
import os
import requests
from requests.adapters import HTTPAdapter


url = ''.join(('http://www.canada.com/entertainment/television/tv-listings/index.html#f=grid'))
                                     
session = requests.Session()
session.mount(url, HTTPAdapter(max_retries=5))
                                 
headers ={
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
                     
req = session.get(url, headers=headers)

print(str(req.cookies.get_dict()))

代码运行没有错误,但是我只是返回一个空字典。谁能解释我在做什么错?

谢谢

1 个答案:

答案 0 :(得分:0)

当您看到网站响应标题时,响应标题中没有设置cookie。因此,您将获得空字典。

enter image description here

您也可以在响应中看到这一点

ST_ASGEOJSON()