您的位置:首页 > 编程语言 > Python开发

通过Python获取Cookie,保存到本地

2018-11-12 09:18 696 查看

1.通过Python获取Cookie,保存到本地

代码如下:

#!/usr/bin/python

import http.cookiejar, urllib.request


'''

获取Cookie保存到本地

'''

def get_baidu_cookies(url):

filename = 'E:\\baiducookies.txt';

cookie = http.cookiejar.LWPCookieJar(filename);

handler = urllib.request.HTTPCookieProcessor(cookie);

opener = urllib.request.build_opener(handler);

response = opener.open(url);

cookie.save(ignore_discard=True, ignore_expires=True);

if response.status == 200:

return 1;

return 0;


# 调用获取百度的cookie

res = get_baidu_cookies('https://www.baidu.com');

print(res);

代码截图:

运行截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息