2 25urllib.py

"""
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")
"""
解析链接
"""
    

  

posted @ 2019-02-26 19:45  那是个好男孩  阅读(108)  评论(0编辑  收藏  举报