网站开发与设计静态网页源代码,网站后台演示,美叶设计网站,ssc网站建设教程python内置的最基本的HTTP请求库#xff0c;有以下四个模块#xff1a; urllib.request 请求模块 urllib.error 异常处理模块 urllib.parse url解析模块 urllib.robotparser robots.txt解析模块 urllib.request请求模块#xff1a; urllib.request.urlopen(u…python内置的最基本的HTTP请求库有以下四个模块 urllib.request 请求模块 urllib.error 异常处理模块 urllib.parse url解析模块 urllib.robotparser robots.txt解析模块 urllib.request请求模块 urllib.request.urlopen(url,dataNone,[timeout,]*,cafileNone,capathNone,cadefaultFalse,contextNone) urlopen()函数 import urllib.requestresponse urllib.request.urlopen(http://www.baidu.com) print(response.read().decode(utf-8)) #response.read()是bytes类型的数据要转码。 import urllib.parsedata bytes(urllib.parse.urlencode({word:hello}),encodingutf-8)#该提交方式是postdata参数是bytes类型的键值对对象response urllib.request.urlopen(http://httpbin.org/post,datadata) #专门提供做http测试的网站print(response.read()) #timeout是超时响应参数response urllib.request.urlopen(http://httpbin.org/get,timeout1) print(response.read())import socketimport urllib.errortry: urllib.request.urlopen(http://httpbin.org/get, timeout0.1)except urllib.error.URLError as e: if isinstance(e.reason,socket.timeout): print(TIME OUT) #响应类型print(type(response))#响应头、状态码response urllib.request.urlopen(https://www.python.org)print(response.status) #得到响应的状态码print(response.getheaders()) #得到响应的Response Headersprint(response.getheader(Server)) #根据键得到Response Headers中指定键的值 Request()函数当urlopen()要传递headers等信息时候就要用到Request()函数返回一个request对象作为urlopen()函数的一个参数。import urllib.parseurl http://httpbin.org/postheaders { # User-Agent:Mozilla/4.0(compatible;MSIE 5.5;Windows NT), Host:httpbin.org}dict { name:Germey}data bytes(urllib.parse.urlencode(dict),encodingutf-8)req urllib.request.Request(urlurl,datadata,headersheaders,methodPOST)req.add_header(User-Agent,Mozilla/4.0(compatible;MSIE 5.5;Windows NT)) #可以单独添加headerresponse urllib.request.urlopen(req)print(response.read().decode(utf-8)) cookieimport http.cookiejar,urllib.requestcookie http.cookiejar.MozillaCookieJar()handler urllib.request.HTTPCookieProcessor(cookie)opener urllib.request.build_opener(handler)response opener.open(http://www.baidu.com)for item in cookie: print(item.name : item.value)#存储cookiefilename cookieLWP.txtcookie http.cookiejar.LWPCookieJar(filename)handler urllib.request.HTTPCookieProcessor(cookie)opener urllib.request.build_opener(handler)response opener.open(http://www.baidu.com)cookie.save(ignore_discardTrue,ignore_expiresTrue)#读取cookiecookie http.cookiejar.LWPCookieJar() #怎么存就怎么取cookie.load(cookieLWP.txt,ignore_discardTrue,ignore_expiresTrue)handler urllib.request.HTTPCookieProcessor(cookie)opener urllib.request.build_opener(handler)response opener.open(http://www.baidu.com)print(response.read().decode(utf-8))urllib.error异常处理模块 异常处理from urllib import errortry: response urllib.request.urlopen(https://www.cnblogs.com/wisir/index.html)except error.HTTPError as e: print(e.reason,e.code,e.headers,sep\n)except error.URLError as e: print(e.reason)else: print(Request Successfully)try: response urllib.request.urlopen(https://www.baidu.com,timeout0.01)except urllib.error.URLError as e: print(e.reason) if isinstance(e.reason,socket.timeout): print(TIME OUT) urllib.parse URL解析模块 urlparse# urllib.parse.urlparse(urlstring,scheme,allow_fragmentsTrue)from urllib.parse import urlparseresult urlparse(http://www.baidu.com/index.html;user?id5#comment)print(type(result),result)urlunparse作用与urlparse相反是将ParseResult类型的六个参数合成一个完整的url。from urllib.parse import urlunparsedata [http,www.baidu.com,index.html,user,a6,comment]print(urlunparse(data))urljoin以第二个参数为基准若第二个参数没有ParseResult类型六个参数中的某一个则用第一个参数作为补充。from urllib.parse import urljoinprint(urljoin(http://www.baidu.com,FAQ.html))print(urljoin(http://www.baidu.com,https://www.cnblogs.com/wisir/))urlencode字典对象转换为get请求参数from urllib.parse import urlencodeparams { name:germey, age:22}base_url http://www.baidu.com?url base_url urlencode(params)print(url) python3 urllib库官方文档https://docs.python.org/3/library/urllib.html转载于:https://www.cnblogs.com/wisir/p/9969833.html