上一篇写道关于存储cookie文件,本篇介绍怎样读取cookie文件html
# 读取cookie文件
from urllib import request,parse
from http import cookiejar
# 建立cookiejar的实例
cookie = cookiejar.MozillaCookieJar()
cookie.load('py15renrenCookie.txt', ignore_discard=True, ignore_expires=True)
# 常见cookie的管理器
cookie_handler = request.HTTPCookieProcessor(cookie)
# 建立http请求的管理器
http_handler = request.HTTPHandler()
# 生成https管理器
https_handler = request.HTTPSHandler()
# 建立请求管理器
opener = request.build_opener(http_handler,https_handler,cookie_handler)
def getHomePage():
# 地址是用在浏览器登陆后的我的信息页地址
url = "http://www.renren.com/967487029/profile"
# 若是已经执行login函数,则opener自动已经包含cookie
rsp = opener.open(url)
html = rsp.read().decode()
with open("py13rsp.html", "w", encoding="utf-8")as f:
# 将爬取的页面
print(html)
f.write(html)
if __name__ == '__main__':
getHomePage()
一样是当返回页面有我的信息,才算成功!
python
'''
使用ssl
1.直接访问https://www.12306.cn/mormhweb/会没法访问,报错以下
----------------------------------
您的链接不是私密链接
攻击者可能会试图从 www.12306.cn 窃取您的信息
(例如:密码、通信内容或信用卡信息)
-----------------------------------
2.不使用https使用http解能够访问
3.由于12306的证书是本身作的,而不是第三方机构
4.因此说http不安全会泄露我的信息
'''
from urllib import request
import ssl
# 利用非认证上下文环境替换认证的上下文环境
ssl._create_default_https_context = ssl._create_unverified_context url = "https://www.12306.cn/mormhweb/"
rsp = request.urlopen(url)
html = rsp.read().decode()
print(html)
不是报错页面,表示使用成功git