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

上海网站建设天锐科技网站开发所使用的浏览器

上海网站建设天锐科技,网站开发所使用的浏览器,新乡网站建设,梅州市住房与城乡建设局网站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.hkea.cn/news/14334427/

相关文章:

  • 河南建设安全协会网站新能源汽车车型及报价
  • 公司网站建设 费用高端网站建设服务商
  • 网站建设代码好难啊wordpress双站
  • 怎么建网站视频米兰网的网络营销是什么
  • 阿克苏网站建设优化有网站了怎么设计网页
  • 做外贸网站需要注意什么免费开源企业网站程序
  • 融媒体建设网站怎么搞科讯cms怎么做网站地图
  • 手机软件制作和做网站相同网站建设印花税
  • 温州建设做seo的网站
  • 简洁大气企业网站直播网站开发方案ppt
  • 旅游攻略网站官方网站车联网是谁做
  • 泰安网站建设价格旅游网站开发说明书
  • 网站开发中英文版如何写网站特殊字体
  • 河北邯郸网站建设公司乐清做网站公司哪家好
  • 公司网站改版多少钱太原网站的优化
  • 广东建设报网站创新的企业网站建设
  • php网站验证码厦门正规网站建设企业
  • 网站建设要多少费用网站关键词排名seo
  • 如何做网站的后台管理学校网站模板设计
  • 社区网站开发进度表重庆域名注册官网
  • 东莞360推广的网站是谁做的厦门集美网站建设
  • 山东网站建设好不好软件工程与项目管理
  • 网站外链的作用wordpress站点地图无法读取
  • 婚纱摄影平台佛山seo优化外包
  • 苏州园区做网站手机个人网站制作教程
  • wordpress恢复数据库菜单不见了镇江积分优化
  • phpcms做双语网站做公司网站需要制作内容
  • 网站功能模块什么意思seo外包
  • 郑州网站建设郑州网站建设h5商城网站怎么做的
  • 成都企业网站制作珠海网站建设维护