认证,代理,cookie

 1from urllib.request import HTTPBasicAuthHandler, HTTPPasswordMgrWithDefaultRealm, build_opener
2from urllib.error import URLError
3from urllib import request,parse
4from urllib.request import ProxyHandler, build_opener
5import ssl
6import http.cookiejar, urllib.request
7
8ssl._create_default_https_context = ssl._create_unverified_context
9
10
11'''验证'''
12def studyAuth():
13    username = 'username'
14    password = 'password'
15    url = 'http://localhost:5000/'
16    p = HTTPPasswordMgrWithDefaultRealm()
17    p.add_password(None, url, username, password)
18    auth_handle = HTTPBasicAuthHandler(p)
19    opener = build_opener(auth_handle)
20
21    try:
22        result = opener.open(url)
23        html = result.read().decode('utf-8')
24        print(html)
25    except URLError as  e:
26        print(e.reason)
27
28studyAuth()
29
30'''代理'''
31def studyProxy():
32    proxy_handle = ProxyHandler({
33        'http': 'http:127.0.0.1:9743',
34        'https': 'https:127.0.0.1:9743'
35    })
36    opener = build_opener(proxy_handle)
37    try:
38        response = opener.open('https://www.baidu.com')
39        print(response.read().decode('utf-8'))
40    except URLError as e:
41        print(e.reason)
42
43studyProxy()
44
45'''cookie'''
46def studyCookie():
47    cookie = http.cookiejar.CookieJar()
48    handle = urllib.request.HTTPCookieProcessor(cookie)
49    opener = urllib.request.build_opener(handle)
50    response = opener.open('https://www.baidu.com')
51    for item in cookie:
52        print(item.name)
53        print(item.value)
54        print(item.name + '=' + item.value)
55
56def studyCookie1():
57    filename = 'cookie.txt'
58    cookie = http.cookiejar.LWPCookieJar(filename)
59#   cookie = http.cookiejar.MozillaCookieJar(filename)
60    handle = urllib.request.HTTPCookieProcessor(cookie)
61    opener = urllib.request.build_opener(handle)
62    response = opener.open('http://www.baidu.com')
63    cookie.save(ignore_discard=True, ignore_expires=True)
64
65'''读取利用生成的cookie文件'''
66def studyCookie2():
67    cookie = http.cookiejar.LWPCookieJar()
68    cookie.load('cookie.txt', ignore_expires=True, ignore_discard=True)
69    handle = urllib.request.HTTPCookieProcessor(cookie)
70    opener = urllib.request.build_opener(handle)
71    response = opener.open('http://www.baidu.com')
72    print(response.read().decode('utf-8'))
73
74studyCookie2()

urllib库认证,代理,cookie的更多相关文章

  1. Python3 urllib库和requests库

    1. Python3 使用urllib库请求网络 1.1 基于urllib库的GET请求 请求百度首页www.baidu.com ,不添加请求头信息: import urllib.requests d ...

  2. 4-urllib库添加代理,添加请求头格式 模板

    urllib 库设置代理的方法 案例如下:

  3. python爬虫 - Urllib库及cookie的使用

    http://blog.csdn.net/pipisorry/article/details/47905781 lz提示一点,python3中urllib包括了py2中的urllib+urllib2. ...

  4. 第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理—用户代理和ip代理结合应用

    第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理 使用IP代理 ProxyHandler()格式化IP,第一个参数,请求目标可能是http或者https,对应设置build_opener ...

  5. urllib库利用cookie实现模拟登录慕课网

    思路 1.首先在网页中使用账户和密码名登录慕课网 2.其次再分析请求头,如下图所示,获取到请求URL,并提取出cookie信息,保存到本地 3.最后在代码中构造请求头,使用urllib.request ...

  6. 爬虫之urllib库

    一.urllib库简介 简介 Urllib是Python内置的HTTP请求库.其主要作用就是可以通过代码模拟浏览器发送请求.它包含四个模块: urllib.request :请求模块 urllib.e ...

  7. python3爬虫之Urllib库(二)

    在上一篇文章中,我们大概讲了一下urllib库中最重要的两个请求方法:urlopen()  和  Request() 但是仅仅凭借那两个方法无法执行一些更高级的请求,如Cookies处理,代理设置等等 ...

  8. Python3爬虫一之(urllib库)

    urllib库是python3的内置HTTP请求库. ython2中urllib分为 urllib2.urllib两个库来发送请求,但是在python3中只有一个urllib库,方便了许多. urll ...

  9. python3中urllib库的request模块详解

    刚刚接触爬虫,基础的东西得时时回顾才行,这么全面的帖子无论如何也得厚着脸皮转过来啊! 原帖地址:https://www.2cto.com/kf/201801/714859.html 什么是 Urlli ...

随机推荐

  1. 【NOIP2016提高A组五校联考1】挖金矿

    题目 分析 我们二分答案 设\(sum_{i,j}\)表示的i列前个数的和, 假设当前出的二分答案为x,第i列挖了\(h_j\)层,则 \[\dfrac{\sum_{i=1}^{n}sum_{i,h_ ...

  2. Python Number(数字) Ⅱ

    Python math 模块.cmath 模块 Python 中数学运算常用的函数基本都在 math 模块.cmath 模块中. Python math 模块提供了许多对浮点数的数学运算函数. Pyt ...

  3. linux 下u盘只读

    使用linux不管是centos还是ubuntu的小伙伴都难免遇到插入U盘的时候,不能对U盘进行操作.提示权限不足或者是只读文件系统. 现在教你三行命令教你解决U盘只读文件系统的问题. 1.插入U盘并 ...

  4. mfc static控件 视频播放 闪屏问题 解决方案

    方案1: 我昨天刚在csdn上解决了这个问题,不是双缓冲和WS_CLIPCHILDREN还有背景擦出什么的问题,就是在你重画的时候要去掉这些控件(按钮什么的)区域, 闪屏是因为窗口大小发生改变时,由于 ...

  5. nginx 日志文件分隔

    Nginx命令 Nginx命令帮助如下 nginx -h nginx version: nginx/0.8.45 Usage: nginx [-?hvVt] [-s signal] [-c filen ...

  6. Jedis下的ShardedJedis

    jedis客户端操作redis主要三种模式:单台模式.分片模式(ShardedJedis).集群模式(BinaryJedisCluster) ShardedJedis是通过一致性哈希来实现分布式缓存的 ...

  7. springboot(六) Maven打包引入本地jar包

       springboot Maven打包引入本地jar包 最近在做项目的时候,有一些jar包不存在maven的依赖库中,所以需要自己引入本地jar包来达到需求,那么我们该如何去将本地的jar包引入s ...

  8. 基于 Golang 完整获取百度地图POI数据的方案

    百度地图为web开发者提供了基于HTTP/HTTPS协议的丰富接口,其中包括地点检索服务,web开发者通过此接口可以检索区域内的POI数据.百度地图处于数据保护对接口做了限制,每次访问服务,最多只能检 ...

  9. es索引基本操作(2)之 索引映射(mappings)管理和索引库配置管理(settings)

    1:索引的映射管理 elasticsearch中的文档等价于java中的对象 , 那么在java对象中有字段(比如string.int.long等): 同理在elasticsearch索引中的具体字段 ...

  10. 一个蒟蒻的解题过程记录——洛谷P1003 铺地毯

    这到题算是我“火线回归”后码的第一道题,病好了心情不错,发篇博客分享一下 目录: ·题目描述 ·题目分析 ·解题思路 ·代码实现 ·总结 ·题目描述: 为了准备一场特殊的颁奖典礼,组织者在会场的一片矩 ...