美文网首页程序员python入门基础学习
Python最强大的第三方库,你有必要了解一下!

Python最强大的第三方库,你有必要了解一下!

作者: 编程新视野 | 来源:发表于2019-01-17 15:17 被阅读1次

    Python最强大的第三方库,你有必要了解一下!

    写在之前

    标准库的内容已经非常多了,我在前面的文章中已经介绍了七八个,没看的可以回头找着去看看。虽然介绍了不少,但是 Python 给编程者的支持不仅仅在于标准库,它还有不可胜数的第三方库。(注释:加群943752371获取python入门20天完整学习笔记和100道基础练习题及答案以及入门书籍视频源码等资料)

    Python最强大的第三方库,你有必要了解一下!

    作为一个 Pythoner,即使你的技术已经达到大佬的级别,在做某件事情之前最好还是 Google 一下是否有标准库或者第三方库来帮你完成,不是有那么一句话么:“如果我比别人看的更远,那是因为我站在巨人的肩膀上”。编程就是要站在巨人的肩膀上,标准库 & 第三方库及其提供者就是这样的巨人,我们应心怀感念,虚心学习。

    安装第三方库

    安装第三方库的方法有很多,不同的方法有不同的优缺点,大家可以根据自己的喜好或者实际中的工作需要来选择。

    使用 pip 安装,这个是我首推的安装方法,用第三方库的管理工具安装。有一个网站是专门用来存储第三方库的,网站地址为https://pypi.python.org/pypi,所有在这个网站上,可以用 pip 或者 easy_install 这种安装工具来安装。

    pip 是一个以 Python 计算机程序语言写成的软件包管理系统,它可以安装和管理软件包。另外,不少的软件包也可以在 “Python 软件包索引” 中找到。

    首先,要安装 pip,大家可以先检查一下在自己的操作系统中是否已经有了 pip,因为有的操作系统,或者已经预先安装了,或者在安装 Python 的时候就安装了。如果确定没有安装的话,就要针对自己的操作系统进行安装,例如在 Ubuntu 系统中:

    pip apt-get install python3-pip

    当然也可以下载文件get-pip.pyhttps://bootstrap.pypa.io/get-pip.py),然后执行 pythonget-pip.py来安装,这个方法同样适用于 Windows 系统。

    这样 pip3 就安装好了,然后你就可以“为所欲为”的安装第三方库了。之所以这样,是因为只需要执行 pip3 install **** (**** 代表第三方库的名字,如果你做了特别的设置,或者不再考虑 python2 的相关安装了,那么可以直接使用 pip install **** 的方式进行安装)即可。当然这个前提是那个库已经在 PyPI 里面了。

    当第三方库安装完成以后,接下来的使用就如同前面的标准库一样。

    requests 库

    在这里我们以很重要的 requests 库为例,来说明一下第三方库的安装和使用。requests 是一个用于在程序中进行 http 协议下的 get 和 post 请求的模块,可以说是好用到哭。

    1.安装

    pip3 install requests

    安装好之后进入交互模式,在交互模式下进行如下操作:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> import requests

    dir(requests)

    ['ConnectTimeout', 'ConnectionError', 'DependencyWarning', 'FileModeWarning', 'HTTPError', 'NullHandler', 'PreparedRequest', 'ReadTimeout', 'Request', 'RequestException', 'RequestsDependencyWarning', 'Response', 'Session', 'T

    imeout', 'TooManyRedirects', 'URLRequired', 'author', 'author_email', 'build', 'builtins', 'cached', 'cake', 'copyright', 'description', 'doc', 'file', 'license', 'loader', 'n

    ame

    ', 'package', 'path', 'spec', 'title', 'url', 'version', '_check_cryptography', '_internal_utils', 'adapters', 'api', 'auth', 'certs', 'chardet', 'check_compatibility', 'codes', 'compat', 'cookies

    ', 'delete', 'exceptions', 'get', 'head', 'hooks', 'logging', 'models', 'options', 'packages', 'patch', 'post', 'put', 'request', 'session', 'sessions', 'status_codes', 'structures', 'urllib3', 'utils', 'warnings']

    从上面的代码可以看出,在 http 中常用到的 get,cookies,post 都赫然在目。

    2.get 请求

    >>> r = requests.get('http://www.baidu.com')

    上面得到某度的一个请求实例,然后:

    >>> r.cookies

    434404, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>

    仔细观察上面,是不是看到了 cookie 的 name 和 value ?结合我们以前学过的网络的相关知识,有没有一种豁然开朗的感觉?下面让我们继续来查看一下其它的属性:

    >>> r.headers

    {'Server': 'bfe/1.0.8.18', 'Date': 'Wed, 19 Sep 2018 09:06:42 GMT', 'Content-Type': 'text/html', 'Last-Modified': 'Mon, 23 Jan 2017 13:28:36 GMT', 'Connection': 'Keep-Alive', 'Cache-Control': 'private, no-cache, no-store, pro

    xy-revalidate, no-transform', 'Pragma': 'no-cache', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Transfer-Encoding': 'chunked'}

    r.encoding

    'ISO-8859-1'

    r.status_code

    200

    上面这些都是在客户端看到的网页的基本属性,下面这个则是网页的内容,比较长,我只截取部分内容显示:

    >>> print(r.text)

    http://s1.

    bdstatic.com/r/www/cache/bdorz/baidu.min.css>

    ....

    请求发出后,requests 会基于 http 头部对相应的编码做出有根据的推测,当你访问 r.text 时,requests 会使用其推测的文本编码。你可以找出 requests 使用了什么编码,并且能够使用 r.coding 属性来改变它。

    >>> r.content

    b'

    t/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css>

    3.post 请求

    假如你要向某个服务器发送一些数据,可能会使用 post 的方式,用 requests 模块实现这种请求比较简单,只需要传递一个字典给 data 参数即可,在这我使用一个在别处看到的例子:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> import requests

    payload = {'key1':'value1','key2':'value2'}

    r =requests.post('http://httpbin.org/post')

    r1 =requests.post('http://httpbin.org/post',data= payload)

    上面代码中 r 没有加 data 的请求,得到的效果如下:

    >>> print(r.text)

    {

    "args": {},

    "data": "",

    "files": {},

    "form": {},

    "headers": {

    "Accept": "/",

    "Accept-Encoding": "gzip, deflate",

    "Connection": "close",

    "Content-Length": "0",

    "Host": "httpbin.org",

    "User-Agent": "python-requests/2.19.1"

    },

    "json": null,

    "origin": "58.33.136.74",

    "url": "http://httpbin.org/post"

    }

    r1 为 data 提供了值,得到的效果如下:

    >>> print(r1.text)

    {

    "args": {},

    "data": "",

    "files": {},

    "form": {

    "key1": "value1",

    "key2": "value2"

    },

    "headers": {

    "Accept": "/",

    "Accept-Encoding": "gzip, deflate",

    "Connection": "close",

    "Content-Length": "23",

    "Content-Type": "application/x-www-form-urlencoded",

    "Host": "httpbin.org",

    "User-Agent": "python-requests/2.19.1"

    },

    "json": null,

    "origin": "58.33.136.74",

    "url": "http://httpbin.org/post"

    }

    比较上面两个结果,发现后者当 data 被赋值以后,在结果中 form 的值即为 data 所传入的数据,它就是 post 给服务器的内容。

    写在之后

    通过这么一个实例,在上面我们展示了一下第三方模块的应用方法,其实没有什么特殊的地方,安装后和用标准库模块一样。一般来说的话,第三方模块常常在某个方面做得更好,或者性能更优。所以,请不要把它们放在你的视线之外。

    相关文章

      网友评论

        本文标题:Python最强大的第三方库,你有必要了解一下!

        本文链接:https://www.haomeiwen.com/subject/ouyidqtx.html