美文网首页
Python之urllib学习Cookie

Python之urllib学习Cookie

作者: 见字如晤一 | 来源:发表于2019-03-08 18:23 被阅读0次

Cookies的处理需要相关的Handler
下面演示一下,如何将网站的Cookies获取下来,相关代码入下:

# urllib Cookie设置的高级用法,主要是通过Handler实现
import urllib.request
import http.cookiejar
cookies=http.cookiejar.CookieJar()
handler= urllib.request.HTTPCookieProcessor(cookies)
opener=urllib.request.build_opener(handler)
response = opener.open("http://www.zhihu.com")
for item in cookies:
    print(item.name+'='+item.value)

打印输出:

_xsrf=AlZV0zUKwpdESr0a5NiskMdbQQLSnWiJ
_zap=2cbcb5ad-126a-480c-90d5-13a8c28b04a5
tgw_l7_route=4860b599c6644634a0abcd4d10d37251

上面代码是将cookies信息直接打印输出,那如何将cookies信息保存到文件中呢,实现保存有两种格式,一种是Mozilla格式、另一种是LWP格式

下面演示保存为Mozilla格式,生成的文件名为cookies.txt

# urllib Cookie设置的高级用法,保存为具体文件,Mozilla格式
import urllib.request
import http.cookiejar

cookies = http.cookiejar.MozillaCookieJar('cookies.txt')
handler = urllib.request.HTTPCookieProcessor(cookies)
opener = urllib.request.build_opener(handler)
response = opener.open("http://www.zhihu.com")

cookies.save(ignore_discard=True, ignore_expires=True)
# 使用要点,http.cookiejar/urllib.request.HTTPCookieProcessor/urllib.request.build_opener

最终运行生成了文件


image.png

内容格式为:


image.png

另外一种格式为LWP格式

# urllib Cookie设置的高级用法,保存为具体文件,LWP格式
import urllib.request
import http.cookiejar

cookies = http.cookiejar.LWPCookieJar('cookies1.txt')
handler = urllib.request.HTTPCookieProcessor(cookies)
opener = urllib.request.build_opener(handler)
response = opener.open("http://www.zhihu.com")

cookies.save(ignore_discard=True, ignore_expires=True)
# 使用要点,http.cookiejar/urllib.request.HTTPCookieProcessor/urllib.request.build_opener

生成的文件格式为:


image.png

好了,以上演示完保存cookies到具体文件!!下面演示如果载入保存好的cookies文件

载入已保存的cookies文件

保存为文件格式后,如何加载这个cookie文件呢:
cookies.load('cookies1.txt',ignore_expires=True,ignore_discard=True)

# urllib Cookie设置的高级用法,将具体文件LWP格式的cookie载入后发起请求
import urllib.request
import http.cookiejar

cookies = http.cookiejar.LWPCookieJar()
cookies.load('cookies1.txt',ignore_expires=True,ignore_discard=True)
handler = urllib.request.HTTPCookieProcessor(cookies)
opener = urllib.request.build_opener(handler)
response = opener.open("http://www.zhihu.com")
print(response.read().decode('utf-8'))
# 使用要点,http.cookiejar/urllib.request.HTTPCookieProcessor/urllib.request.build_opener
######################################################

相关文章

  • Python之urllib学习Cookie

    Cookies的处理需要相关的Handler下面演示一下,如何将网站的Cookies获取下来,相关代码入下: 打印...

  • Python爬虫学习(十六)初窥Scrapy

    Python爬虫学习(一)概述Python爬虫学习(二)urllib基础使用Python爬虫学习(三)urllib...

  • 归纳下Python爬虫的点

    要学习Python爬虫,我们要学习的共有以下几点: Python基础知识 Python中urllib和urllib...

  • urllib

    python模块(包)之urllib urllib:官方文档是最好的模块表达说明。 urllib is a pac...

  • Python ☞ day 14

    Python学习笔记之 爬虫 urllib 模块提供了一系列用于操作URL的功能 urllib 爬取网页 将爬取的...

  • 2019-01-09 python 库之 requests

    python 库之 requests Requests 是用Python语言编写,基于 urllib,采用 Ap...

  • Python之urllib库学习

    Python请求网络时,已经提供了很多库可供使用,最基础的http库有urllib、httplib2、reques...

  • Python爬虫常用库之requests详解

    在使用了urllib库之后,感觉很麻烦,比如获取个cookie都需要分几步,代码又多,这和python的风格好像有...

  • Python基础库使用(一)

    python 基本库的使用(一) 使用 urllib 在 Python 2 中,有 urllib 和 urllib...

  • 爬虫常用库介绍

    urllib Urllib是 python 内置的库,在 Python 这个内置的 Urllib 库中有这么 4 ...

网友评论

      本文标题:Python之urllib学习Cookie

      本文链接:https://www.haomeiwen.com/subject/jskdpqtx.html