2 25urllib.py

那是個好男孩發表於2019-02-26
"""
urllib.request.urlopen(url,data,timeout)
"""
# from urllib.request import urlopen
# import urllib.parse
# from urllib.error import URLError
# import socket
# url = "http://httpbin.org/post"
# data = bytes(urllib.parse.urlencode({'name': 'dc'}), encoding="utf-8")
# try:
#     reponse = urlopen(url = url, data=data, timeout=5)
# except URLError as e:
#     if isinstance(e.reason,socket.timeout):
#         print("TIME OUT")
# else:
#     print(reponse.read().decode("utf-8"))
"""
urllib.request.Request(url,data,headers,method)
"""
# from urllib.request import Request,urlopen
# import urllib.parse
# url = "http://httpbin.org/post"
# data = bytes(urllib.parse.urlencode({'name': 'dc'}), encoding="utf-8")
# headers = {
#     'User-Agent': 'Mozilla/4.0(compatible;Msie5.5;Windows NT)'
# }
# req = Request(url=url, data=data, headers=headers, method="POST")
# reponse = urlopen(req)
# print(reponse.read().decode("utf-8"))
"""
Handler 驗證 代理 Cookies build_opener
"""
"""
驗證
"""
# from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener
# import urllib.error
# url = "https://www.zhihu.com/signup?next=%2F"
# name = "17380646919"
# value = "dc201637"
# p = HTTPPasswordMgrWithDefaultRealm()
# p.add_password(None, url, name, value)
# handler = HTTPBasicAuthHandler(p)
# opener = build_opener(handler)

# try:
#     reponse = opener.open(url)
# except urllib.error.URLError as e:
#     print(e.reason)
# else:
#     print(reponse.read().decode("utf-8"))
"""
代理
"""
# from urllib.error import URLError
# from urllib.request import ProxyHandler, build_opener
# url = "http://www.baidu.com"
# Proxy_Handler = ProxyHandler({
#     'http': 'http://127.0.0.1:9743',
#     'https':'https://127.0.0.1:9743'
# })
# opener = build_opener(Proxy_Handler)
# try:
#     reponse = opener.open(url)
#     print(reponse.read().decode("utf-8"))
# except URLError as e:
#     print(e.reason)
"""
cookies
"""
"""
列印cookies的值
"""
# from urllib.request import HTTPCookieProcessor,build_opener
# import http.cookiejar
# url = "http://www.baidu.com"
# cookie = http.cookiejar.CookieJar()
# handler = HTTPCookieProcessor(cookie)
# opener = build_opener(handler)
# reponse = opener.open(url)
# for items in cookie:
#     print(items.name + "=" + items.value)
"""
儲存cookies的值
"""
# from urllib.request import HTTPCookieProcessor,build_opener
# import http.cookiejar
# url = "http://www.baidu.com"
# filename = "cookies.txt"
# # cookie = http.cookiejar.MozillaCookieJar(filename)
# cookie = http.cookiejar.LWPCookieJar(filename)
# handler = HTTPCookieProcessor(cookie)
# opener = build_opener(handler)
# reponse = opener.open(url)
# cookie.save(ignore_discard=True, ignore_expires=True)
"""
讀取cookies的值並應用
"""
# from urllib.request import HTTPCookieProcessor,build_opener
# import http.cookiejar
# url = "http://www.baidu.com"
# cookie = http.cookiejar.LWPCookieJar()
# cookie.load('cookies.txt',ignore_discard=True,ignore_expires=True)
# handler = HTTPCookieProcessor(cookie)
# opener = build_opener(handler)
# reponse = opener.open(url)
# print(reponse.read().decode("utf-8"))
"""
異常處理 URLError、HTTPError
"""
# from urllib import error, request
# try:
#     reponse = request.urlopen("http://cuiqingcai.com/index.htm")
# except error.HTTPError as e:
#     print(e.reason,e.code,e.headers,sep= '\n')
# except error.URLError as e:
#     print(e.reason)
# else:
#     print('no worry')
"""
reson屬性返回一個物件
"""
# import urllib.request
# import socket
# from urllib.error import HTTPError, URLError
# try:
#     reponse = urllib.request.urlopen("https://www.baidu.com", timeout=0.1)
# except URLError as e:
#     print(type(e.reason))
#     if isinstance(e.reason, socket.timeout):
#         print("TIMEOUT")
"""
解析連結
"""