目录
  • urllib库作用
  • urllib 库下的几种模块的基本使用
    • 一、urllib.request模块
      • 1.功能
      • 2.常用方法
    • 参数说明:
    • 总结

      urllib库作用

      urllib 库 是python内置的 http 请求库。urllib 模块提供的上层接口,使访问 www 和 ftp 上的数据就像访问本地文件一样。我们爬取网页的时候,经常需要用到这个库。

      urllib 库下的几种模块的基本使用

      一、urllib.request模块

      1.功能

      urllib.request 模块提供了最基本的构造 http (或其他协议如 ftp)请求的方法,利用它可以模拟浏览器的一个请求发起过程。利用不同的协议去获取 url 信息。它的某些接口能够处理基础认证 ( basic authenticaton) 、redirections (http 重定向)、 cookies (浏览器 cookies)等情况。而这些接口是由 handlers 和 openers 对象提供的。

      2.常用方法

      2.1 urlopen()方法

      语法格式:

      urllib.request.urlopen(url, data=none, [timeout, ]*, cafile=none, capath=none, cadefault=false, context=none)
      

      参数说明:url:需要打开的网址; data: post 提交的数据, 默认为 none ,当 data 不为 none 时, urlopen() 提交方式为 post; timeout:设置网站访问超时时间

      用例:

      import urllib.request# 等价于from urllib import request
      response = urllib.request.urlopen('https://www.baidu.com')
      print("查看 response 响应信息类型: ",type(response))
      page = response.read()
      print(page.decode('utf-8'))
      

      说明: 直接使用 urllib.request 模块中的 urlopen方法获取页面,其中 page 数据类型为 bytes 类型,经过 decode 解码 转换成 string 类型。通过输出结果可以 urlopen 返回对象是httpresposne 类型对象。

      urlopen 返回一个类文件对象,并提供了如下方法:

      read() , readline() , readlines() , fileno() , close() :这些方法的使用方式与文件对象完全一样;

      info():返回一个httplib.httpmessage对象,表示远程服务器返回的头信息;可以通过quick reference to http headers查看 http header 列表。

      getcode():返回http状态码。如果是http请求,200表示请求成功完成;404表示网址未找到;

      geturl():返回获取页面的真实 url。在 urlopen(或 opener 对象)可能带一个重定向时,此方法很有帮助。获取的页面 url 不一定跟真实请求的 url 相同。

      示例:

      import urllib.request
      response = urllib.request.urlopen('https://python.org/')
      print("查看 response 的返回类型:",type(response))
      print("查看反应地址信息: ",response)
      print("查看头部信息1(http header):\n",response.info())
      print("查看头部信息2(http header):\n",response.getheaders())
      print("输出头部属性信息:",response.getheader("server"))
      print("查看响应状态信息1(http status):\n",response.status)
      print("查看响应状态信息2(http status):\n",response.getcode())
      print("查看响应 url 地址:\n",response.geturl())
      page = response.read()
      print("输出网页源码:",page.decode('utf-8'))
      

      2.2 request()方法

      使用request()来包装请求,再通过urlopen()获取页面。

      语法格式:

      urllib.request.request(url, data=none, headers={}, origin_req_host=none, unverifiable=false, method=none)
      

      示例:

      import urllib.request
      url = "https://www.lagou.com/zhaopin/python/?labelwords=label"
      headers = {
      'user-agent':'mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/63.0.3239.108 safari/537.36',
      'referer': 'https://www.lagou.com/zhaopin/python/?labelwords=label',
      'connection': 'keep-alive'
      }
      req = request.request(url, headers=headers)
      page = request.urlopen(req).read()
      page = page.decode('utf-8')
      print(page)
      

      参数说明:

      user-agent :这个头部可以携带如下几条信息:浏览器名和版本号、操作系统名和版本号、默认语言。这个数据可以从 网页开发工具上的请求反应信息中获取(浏览器上一般按 f12 打开开发工具)。作用是用于伪装浏览器。

      referer:可以用来防止盗链,有一些网站图片显示来源 https://***.com ,就是检查 referer 来鉴定的。

      connection:表示连接状态,记录 session 的状态。

      origin_req_host:请求方的 host 名称或者 ip 地址。

      unverifiable:指请求无法验证,默认为 false。用户并没有足够的权限来选择接收这个请求结果,例如请求一个 html 文档中的图片,但没有自动抓取图像的权限,这时 unverifiable 为 true。

      method:指定请求使用的方法,例如 get、post、put 等。

      参考:

      总结

      本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注www.887551.com的更多内容!