<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                標準庫的內容已經非常多了,前面僅僅列舉幾個,但是python給編程者的支持還不僅僅在于標準庫,它還有不可勝數的第三方庫。因此,如果作為一個python編程者,即使你達到了master的水平,最好的還是要在做某個事情之前,在網上搜一下是否有標準庫或者第三方庫替你完成那件事。因為,偉大的艾薩克·牛頓爵士說過: > 如果我比別人看得更遠,那是因為我站在巨人的肩上。 編程,就要站在巨人的肩上。標準庫和第三方庫以及其提供者,就是巨人,我們本應當謙卑地向其學習,并應用其成果。 ## [](https://github.com/qiwsir/StarterLearningPython/blob/master/228.md#安裝第三方庫)安裝第三方庫 要是用第三方庫,第一步就是要安裝,在本地安裝完畢,就能如同標準庫一樣使用了。其安裝方法如下: **方法一:利用源碼安裝** 在github.com網站可以下載第三方庫的源碼(或者其它途徑),得到源碼之后,在本地安裝。 一般情況,得到的碼格式大概都是 zip 、 tar.zip、 tar.bz2格式的壓縮包。解壓這些包,進入其文件夾,通常會看見一個 setup.py 的文件。如果是Linux或者Mac(我是用ubuntu,特別推薦哦),就在這里運行shell,執行命令: ~~~ python setup.py install ~~~ 如果用的是windows,需要打開命令行模式,執行上述指令即可。 如此,就能把這個第三庫安裝到系統里。具體位置,要視操作系統和你當初安裝python環境時設置的路徑而定。默認條件下,windows是在`C:\Python2.7\Lib\site-packages`,Linux在`/usr/local/lib/python2.7/dist-packages`(這個只是參考,不同發行版會有差別,具體請讀者根據自己的操作系統,自己找找),Mac在?`/Library/Python/2.7/site-packages`。 有安裝就要有卸載,卸載所安裝的庫非常簡單,只需要到相應系統的site-packages目錄,直接刪掉庫文件即卸載。 **方法二:pip** 用源碼安裝,不是我推薦的,我推薦的是用第三方庫的管理工具安裝。有一個網站,是專門用來存儲第三方庫的,所有在這個網站上的,都能用pip或者easy_install這種安裝工具來安裝。這個網站的地址:[https://pypi.python.org/pypi](https://pypi.python.org/pypi) 首先,要安裝pip(python官方推薦這個,我當然要順勢了,所以,就只介紹并且后面也只使用這個工具)。如果讀者跟我一樣,用的是ubuntu或者其它某種Linux,基本不用這個操作,在安裝操作系統的時候已經默認把這個東西安裝好了(這還不是用ubuntu的理由嗎?)。如果因為什么原因,沒有安裝,可以使用如下方法: Debian and Ubuntu: ~~~ sudo apt-get install python-pip ~~~ Fedora and CentOS: ~~~ sudo yum install python-pip ~~~ 當然,也可以這里下載文件[get-pip.py](https://bootstrap.pypa.io/get-pip.py),然后執行`python get-pip.py`來安裝。這個方法也適用于windows。 pip安裝好了。如果要安裝第三方庫,只需要執行`pip install XXXXXX`(XXXXXX代表第三方庫的名字)即可。 當第三方庫安裝完畢,接下來的使用就如同前面標準庫一樣。 ## [](https://github.com/qiwsir/StarterLearningPython/blob/master/228.md#舉例requests庫)舉例:requests庫 以requests模塊為例,來說明第三方庫的安裝和使用。之所以選這個,是因為前面介紹了urllib和urllib2兩個標準庫的模塊,與之有類似功能的第三方庫中requests也是一個用于在程序中進行http協議下的get和post請求的模塊,并且被網友說成“好用的要哭”。 **說明**:下面的內容是網友1world0x00提供,我僅做了適當編輯。 ### [](https://github.com/qiwsir/StarterLearningPython/blob/master/228.md#安裝)安裝 ~~~ pip install requests ~~~ 安裝好之后,在交互模式下: ~~~ >>> import requests >>> dir(requests) ['ConnectionError', 'HTTPError', 'NullHandler', 'PreparedRequest', 'Request', 'RequestException', 'Response', 'Session', 'Timeout', 'TooManyRedirects', 'URLRequired', '__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__', 'adapters', 'api', 'auth', 'certs', 'codes', 'compat', 'cookies', 'delete', 'exceptions', 'get', 'head', 'hooks', 'logging', 'models', 'options', 'packages', 'patch', 'post', 'put', 'request', 'session', 'sessions', 'status_codes', 'structures', 'utils'] ~~~ 從上面的列表中可以看出,在http中常用到的get,cookies,post等都赫然在目。 ### [](https://github.com/qiwsir/StarterLearningPython/blob/master/228.md#get請求)get請求 ~~~ >>> r = requests.get("http://www.itdiffer.com") ~~~ 得到一個請求的實例,然后: ~~~ >>> r.cookies <<class 'requests.cookies.RequestsCookieJar'>[]> ~~~ 這個網站對客戶端沒有寫任何cookies內容。換一個看看: ~~~ >>> r = requests.get("http://www.1world0x00.com") >>> r.cookies <<class 'requests.cookies.RequestsCookieJar'>[Cookie(version=0, name='PHPSESSID', value='buqj70k7f9rrg51emsvatveda2', port=None, port_specified=False, domain='www.1world0x00.com', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]> ~~~ 原來這樣呀。繼續,還有別的屬性可以看看。 ~~~ >>> r.headers {'x-powered-by': 'PHP/5.3.3', 'transfer-encoding': 'chunked', 'set-cookie': 'PHPSESSID=buqj70k7f9rrg51emsvatveda2; path=/', 'expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'keep-alive': 'timeout=15, max=500', 'server': 'Apache/2.2.15 (CentOS)', 'connection': 'Keep-Alive', 'pragma': 'no-cache', 'cache-control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'date': 'Mon, 10 Nov 2014 01:39:03 GMT', 'content-type': 'text/html; charset=UTF-8', 'x-pingback': 'http://www.1world0x00.com/index.php/action/xmlrpc'} >>> r.encoding 'UTF-8' >>> r.status_code 200 ~~~ 下面這個比較長,是網頁的內容,僅僅截取顯示部分: ~~~ >>> print r.text <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>1world0x00sec</title> <link rel="stylesheet" href="http://www.1world0x00.com/usr/themes/default/style.min.css"> <link rel="canonical" href="http://www.1world0x00.com/" /> <link rel="stylesheet" type="text/css" href="http://www.1world0x00.com/usr/plugins/CodeBox/css/codebox.css" /> <meta name="description" content="愛生活,愛拉芳。不裝逼還能做朋友。" /> <meta name="keywords" content="php" /> <link rel="pingback" href="http://www.1world0x00.com/index.php/action/xmlrpc" /> ...... ~~~ 請求發出后,requests會基于http頭部對相應的編碼做出有根據的推測,當你訪問r.text之時,requests會使用其推測的文本編碼。你可以找出requests使用了什么編碼,并且能夠使用r.coding屬性來改變它。 ~~~ >>> r.content '\xef\xbb\xbf\xef\xbb\xbf<!DOCTYPE html>\n<html lang="zh-CN">\n <head>\n <meta charset="utf-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0">\n <title>1world0x00sec</title>\n <link rel="stylesheet" href="http://www.1world0x00.com/usr/themes/default/style.min.css">\n <link ...... 以二進制的方式打開服務器并返回數據。 ~~~ ### [](https://github.com/qiwsir/StarterLearningPython/blob/master/228.md#post請求)post請求 requests發送post請求,通常你會想要發送一些編碼為表單的數據——非常像一個html表單。要實現這個,只需要簡單地傳遞一個字典給data參數。你的數據字典在發出請求時會自動編碼為表單形式。 ~~~ >>> 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的請求,看看效果: [![](https://box.kancloud.cn/2015-09-07_55ed51929e7c0.jpg)](https://box.kancloud.cn/2015-09-07_55ed51929e7c0.jpg) r1是加了data的請求,看效果: [![](https://box.kancloud.cn/2015-09-07_55ed519533afb.jpg)](https://box.kancloud.cn/2015-09-07_55ed519533afb.jpg) 多了form項。喵。 ### [](https://github.com/qiwsir/StarterLearningPython/blob/master/228.md#http頭部)http頭部 ~~~ >>> r.headers['content-type'] 'application/json' ~~~ 注意,在引號里面的內容,不區分大小寫`'CONTENT-TYPE'`也可以。 還能夠自定義頭部: ~~~ >>> r.headers['content-type'] = 'adad' >>> r.headers['content-type'] 'adad' ~~~ 注意,當定制頭部的時候,如果需要定制的項目有很多,需要用到數據類型為字典。 網上有一個更為詳細敘述有關requests模塊的網頁,可以參考:[http://requests-docs-cn.readthedocs.org/zh_CN/latest/index.html](http://requests-docs-cn.readthedocs.org/zh_CN/latest/index.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看