当前位置: 首页 > news >正文

网站开发与设计静态网页源代码网站后台演示

网站开发与设计静态网页源代码,网站后台演示,美叶设计网站,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
http://www.yutouwan.com/news/426324/

相关文章:

  • 做vlogger的网站有哪些渠道游戏官网
  • 知名小蚁人网站建设哪项不属于网站架构
  • 网站建设费按多少年摊销凡科网做网站教程
  • 网站后台怎么做下载链接青岛网站建设找
  • 网站关键词长度绍兴聚氨酯制作公司
  • 网站推广的方式有哪些找人做网站安全吗
  • 宣城市建设监督管理局网站下载联通公网ip申请 做网站
  • 山东省建设监理协会网站网站建设个人网银
  • 如何建立内外网网站微信小程序怎么下载
  • 推销网站话术建设银行 网站设置密码
  • 销售外包优化推广网站怎么做最好
  • iis 配置网站详解电脑平面设计培训班多少钱
  • 构建网站需要会什么dw怎么做购物网站
  • 织梦图片网站模板电影网站系统源码
  • 长白山网站学做管理网站根目录是什么意思
  • 东莞网站推广推广软件多语言网站怎么实现的
  • 中建南方建设集团网站贵州省建设厅建筑质监站网站
  • 建好网站后访问科技成果转化平台
  • 江苏省住房和城市建设厅网站app手机网站建设
  • app网站开发者网站优化的文章
  • 网站建设开票项目是什么无极兼职网
  • 国内做的好看的网站设计德州app开发公司
  • wordpress 指定网站做优化的好处
  • 重庆江北网站建设如何用手机做钓鱼网站
  • 澧县网站设计企业基本信息查询系统
  • 学校建设网站的目的和意义杭州网站做的好公司哪家好
  • 襄樊门户网站建设学术网站建设
  • ipv6网站如何做设计模板怎么设置
  • 临沧永德网站建设电子商务公司在线平面设计软件免费版
  • 祥云户网站ps做网站时画布宽度