<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國際加速解決方案。 廣告
                上節課,我們學習了什么是網絡、網絡的發展歷程和常見網絡協議。這節課,我們重點學習一下HTTP協議和怎么用python請求、解析HTTP數據。 ## 什么是HTTP ![](https://www.simplilearn.com/ice9/blog/wp-content/uploads/2014/12/Why-HTML-is-important-for-content-writers.gif) HTTP是**超文本傳輸協議**的簡稱。所謂**超文本**,就是在互聯網上傳輸的信息。我們在瀏覽器里瀏覽的網頁,就是使用超文本標記語言HTML實現的,下章我們會詳細介紹HTML。 ![](http://2.bp.blogspot.com/_4l9wMe5bbSk/TMpvwVcMT3I/AAAAAAAAAK4/sCEjRQCkF1o/s1600/Client+Server+communication.GIF) 典型的HTTP傳輸如下:客戶端(瀏覽器)對指定的url發起http請求。url指定的服務端在接收到請求后,將自己的數據(經常是html或json)返回給客戶端。 我們分別看一下發起請求和請求響應的部分。 一個典型的請求頭如下: ~~~ GET / HTTP/1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) Accept: */* ~~~ ![](https://www.safaribooksonline.com/library/view/restful-net/9780596155025/httpatomoreillycomsourceoreillyimages224471.png) 我們去請求服務端數據時,有`get`,`post`,`put`,`delete`4種方式,其中 get和post較為常用。 請求的響應數據格式如下: ~~~ HTTP/1.0 200 OK Content-Type: text/plain Content-Length: 137582 Expires: Thu, 05 Dec 1997 16:00:00 GMT Last-Modified: Wed, 5 August 1996 15:55:28 GMT Server: Apache 0.84 <html> <body>Hello World</body> </html> ~~~ 回應的格式是"頭信息 + 一個空行(\r\n) + 數據"。其中,第一行是"協議版本 + 狀態碼(status code) + 狀態描述"。 這里有個非常重要的,狀態碼。具體含義如下: ![](http://aviraer.info/images/2018/HTTP%E7%8A%B6%E6%80%81%E7%A0%81%E7%B1%BB%E5%88%AB.png) ## 什么是URL ## python的request模塊 在python中,我們使用`requests`模塊來發起發起http請求。 1. request 安裝 我們可以使用`pip install requests`命令安裝requests包。 2. 請求數據demo ~~~ r = requests.get('https://api.github.com/events') ~~~ 調用 requests的get方法發起請求,請求參數是目標地址。 3. post請求的參數 post請求時的附帶參數可以這樣模擬: ~~~ requests.post('http://baidu.com', data = {'key':'value'}) ~~~ get請求的參數 ~~~ payload = {'key1': 'value1', 'key2': 'value2'} r = requests.get("http://baidu.com", params=payload) print(r.url) ~~~ 4. 獲取請求返回數據 ~~~ r = requests.get('https://api.github.com/events') print(r.text) ~~~ 5. 返回結果json解碼 ~~~ r = requests.get('https://api.github.com/events') print(r.json()) ~~~ 6. 設置超時(秒單位) ~~~ requests.get('http://github.com', timeout=3) ~~~ ## 代碼實例 下面,我們來看一個python通過http請求獲取天氣的小demo,看代碼: ~~~ import requests from tkinter import * def queryWeather(): city = cityEntry.get() headers = {'Authorization': 'APPCODE 2c571bbe36b24e9aa3cadaaee4d0adbf'} r = requests.get( 'https://saweather.market.alicloudapi.com/spot-to-weather?area='+city+'&need3HourForcast=0&needAlarm=0&needHourData=0&needIndex=0&needMoreDay=0', headers=headers) weather = r.json()['showapi_res_body']['now']['weather'] weatherLabel = Label(root, text=weather) weatherLabel.pack() return weather root=Tk() cityEntry = Entry(root) cityEntry.pack() button = Button(root,text='查詢天氣', command=queryWeather) button.pack() root.mainloop() ~~~ 代碼的第二行,我們往http的header里設置了請求碼,用來做鑒權。當獲得數據后,我們首先將結果轉為json格式。然后讀取返回數據的當前天氣字段。 request模塊里還有更多的方法來發起復雜HTTP請求,我們在具體項目使用時再一起學習。 **阿達老師-孩子身邊的編程專家** *完整課程請關注阿達老師,主頁里有完整的課程目錄和觀看地址*
                  <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>

                              哎呀哎呀视频在线观看