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

软件网站建设基本流程图网站建设毕业设计报告书

软件网站建设基本流程图,网站建设毕业设计报告书,iis7发布php网站,不要网站域名Python urllib Python urllib 库用于操作网页 URL#xff0c;并对网页的内容进行抓取处理。 本文主要介绍 Python3 的 urllib。 urllib 包 包含以下几个模块#xff1a; urllib.request - 打开和读取 URL。urllib.error - 包含 urllib.request 抛出的异常。urllib.parse …Python urllib Python urllib 库用于操作网页 URL并对网页的内容进行抓取处理。 本文主要介绍 Python3 的 urllib。 urllib 包 包含以下几个模块 urllib.request - 打开和读取 URL。urllib.error - 包含 urllib.request 抛出的异常。urllib.parse - 解析 URL。urllib.robotparser - 解析 robots.txt 文件。 urlopen 语法 urllib.request.urlopen(url,dataNone,[timeout,]*,cafileNone,capathNone,cadefaultFalse,contextNone) #url:访问的网址 #data:额外的数据如headerform data用法 # request:GET import urllib.request response urllib.request.urlopen(http://www.baidu.com) print(response.read().decode(utf-8))# request: POST # http测试http://httpbin.org/ import urllib.parse import urllib.request data bytes(urllib.parse.urlencode({word:hello}),encodingutf8) response urllib.request.urlopen(http://httpbin.org/post,datadata) print(response.read())# 超时设置 import urllib.request response urllib.request.urlopen(http://httpbin.org/get,timeout1) print(response.read())import socket import urllib.request import urllib.errortry:response urllib.request.urlopen(http://httpbin.org/get,timeout0.1) except urllib.error.URLError as e:if isinstance(e.reason,socket.timeout):print(TIME OUT)响应 # 响应类型 import urllib.open response urllib.request.urlopen(https:///www.python.org) print(type(response)) # 状态码 响应头 import urllib.request response urllib.request.urlopen(https://www.python.org) print(response.status) print(response.getheaders()) print(response.getheader(Server))Request 声明一个request对象该对象可以包括header等信息然后用urlopen打开。 # 简单例子 import urllib.request request urllib.request.Requests(https://python.org) response urllib.request.urlopen(request) print(response.read().decode(utf-8))# 增加header from urllib import request, parse url http://httpbin.org/post headers {User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36Host:httpbin.org } # 构造POST表格 dict {name:Germey } data bytes(parse.urlencode(dict),encodingutf8) req request.Request(urlurl,datadata,headersheaders,methodPOST) response request.urlopen(req) print(response.read()).decode(utf-8) # 或者随后增加header from urllib import request, parse url http://httpbin.org/post dict {name:Germey } req request.Request(urlurl,datadata,methodPOST) req.add_hader(User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36) response request.urlopen(req) print(response.read().decode(utf-8))设置请求超时 有些请求可能因为网络原因无法得到响应。因此我们可以手动设置超时时间。当请求超时我们可以采取进一步措施例如选择直接丢弃该请求或者再请求一次。 import urllib.requesturl http://tieba.baidu.com response urllib.request.urlopen(url, timeout1) print(response.read().decode(utf-8))使用 data 参数提交数据 在请求某些网页时需要携带一些数据我们就需要使用到 data 参数。 import urllib.parse import urllib.requesturl http://127.0.0.1:8000/book params {name:浮生六记,author:沈复 }data bytes(urllib.parse.urlencode(params), encodingutf8) response urllib.request.urlopen(url, datadata) print(response.read().decode(utf-8))params 需要被转码成字节流。而 params 是一个字典。我们需要使用 urllib.parse.urlencode() 将字典转化为字符串。再使用 bytes() 转为字节流。最后使用 urlopen() 发起请求请求是模拟用 POST 方式提交表单数据。 异常处理 捕获异常保证程序稳定运行 # 访问不存在的页面 from urllib import request, error try:response request.urlopen(http://cuiqingcai.com/index.htm) except error.URLError as e:print(e.reason)# 先捕获子类错误 from urllib imort request, error try:response 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(Request Successfully) # 判断原因 import socket import urllib.request import urllib.errortry:response urllib.request.urlopen(http://httpbin.org/get,timeout0.1) except urllib.error.URLError as e:if isinstance(e.reason,socket.timeout):print(TIME OUT)
http://www.tj-hxxt.cn/news/136143.html

相关文章:

  • 梯子seo网络优化师招聘
  • 网站后台英语财务软件哪个好用
  • 网站推广公司汉狮网络江干区网站建设
  • 北京网站推广优化上海网站建设推荐案例
  • 网站搜索推广方案论文上海网站备案审核
  • 哪种类型的网站比较难做制作英文网站多少钱
  • 360永久免费建网站专业柳州网站建设多少钱
  • 济南外贸网站中国建设app官方下载
  • 免费网站模板怎么做网站做的网站怎么上传到网上
  • 中国手机最好的网站排名电商系统app开发
  • 中国建设招标网 官方网站下载wordpress数据库重置密码
  • 平板电脑可以做网站吗有哪些做的很漂亮的网站
  • 晋中住房与城乡建设厅网站wordpress链接失效
  • 网站关键词库是怎么做的企业网站建设运营方案
  • 银川网站建设公司哪家不错Php外贸网站建设新浪博客
  • 两学一做学习网站温州平阳县网站建设兼职
  • 黑白的网站网站如何做tag
  • 自己做图网站北京建设工程招标信息网站
  • 网站logo设计创意网站建设与管理项目1项目规划
  • 企业网站备案流程湖北省市政工程建设官方网站
  • 北京天奕时代创意设计有限公司seo运营招聘
  • 高端网站改版内部网站做域名解析到端口
  • 成都美食网站设计论文wordpress ftp下载
  • 优客工场 网站开发深圳电商页面设计那家好
  • 网站建设拟采用的技术路线福步外贸论坛登录
  • 全球网站域名做网站项目如何实现支付
  • 响应式企业网站cms安卓app软件公司
  • 株洲seo网站优化wordpress 完整模板
  • 校园网站建设方案网站分辨率做96是否会更好
  • 有口碑的做网站随州市网站建设