1. 程式人生 > >urllib庫認證,代理,cookie

urllib庫認證,代理,cookie

認證,代理,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
5
import 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()