# NKUST ITC 108-1 Python[3]
###### [https://python-slide.macs1207.dev](https://python-slide.macs1207.dev)
###### 2019/11/12
###### tags: `NKUST ITC` `Python 社課`
[TOC]
---
## [Github](https://github.com/macs1207/python-tutorial)
----
## 下半學期進度(超理想)
|日期|內容|
|-|-|
|11/12|封包、requests|
|11/19|DOM解析、API|
|11/26|專案細節討論|
|12/03|專案實作|
|12/10|專案實作|
|12/17|專案驗收|
|12/26|期末社員大會|
---
## 封包 (Http/Https)
----
### 當我今天打開一個網站..

- 以Google為例,在瀏覽器中按F12可以使用DevTools
----
- 每一個項目,都是一個封包
- 載入一個網頁,除了載入Page Source,可能還需要載入一些icon、圖片及js
- 一個封包中帶有一個http request
- 傳送給伺服器的request會對應伺服器回傳的response
- 分為Header與Payload兩個部分
----
### 封包裡面有什麼呢
- Request

----
Header中會有
1. Method(GET、POST) + URL + HTTP version
2. HOST
3. User-Agent
4. Cookie
Payload則是視需求而定
----
- Response

----
Header中會有
1. HTTP version + Status code
2. Time
3. Set-Cookie
Header底下則是回傳的資料
可能是網頁原始碼或二進制圖片等等
----
### GET
- 從伺服器取得資料
- 參數會加在URL後面
- 使用瀏覽器瀏覽網頁
----
### POST
- 傳送資料給伺服器
- 參數是在在封包中的Header之後
- 送出表單之類的行為通常使用POST
----
除了F12的DevTool之外,有專門抓封包的工具</br>一般就是個Proxy server
- Fiddler
- Charles Proxy
----
### F12 DEMO
- 目標是[這個](http://webap.nkust.edu.tw/nkust/)
----
### 接下來會教大家如何使用Python送出request
---
## requests
----
### 安裝requests
```
pip install requests
```
----
### GET 請求
----
一個基本的GET請求長這樣
```python=
import requests
rs = requests.get('https://www.google.com.tw/') # 對目標網站送出請求
print(rs.status_code) # 輸出伺服器狀態碼
print(rs.text) # 輸出page sourse(網頁原始碼)
```
----
可以帶上Params(參數),會自動加到URL Query
```python=
import requests
url = "http://httpbin.org/get"
my_params = {'key1': 'value1', 'key2': 'value2'}
rs = requests.get(url, params=my_params)
print("原本的URL:{}".format(url))
print("改變後URL:{}".format(rs.url)) # 觀察URL跟原本的差異
```
----
### 使用requests下載圖片
```python=
import requests
url = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
rs = requests.get(url)
with open("logo.png", "wb") as f:
f.write(rs.content)
```
----
### POST 請求
----
原則上,POST請求很少不帶Payload(?
所以就示範帶Payload的基礎用法
```python=
my_data ={'key1': 'value1', 'key2': 'value2'}
rs = requests.post('http://httpbin.org/post', data=my_data)
```
----
有時,POST也會需要傳送檔案,在requests的post
```python=
my_files = {'docs': open('my_file.docx', 'rb')} # 要上傳的檔案
rs = requests.post('http://httpbin.org/post', files=my_files)
```
----
### 練習
1. 使用POST method,送出登入校務系統的請求,並取得200的response
2. 使用GET method,任意下載一張圖片回來並儲存
---
## Discord
- [https://discord.gg/hmUeXeH](https://discord.gg/hmUeXeH)

----
## Telegram
- [https://t.me/kuasitc](https://t.me/kuasitc)

----
## 作者
- 社長 [Macs](https://github.com/macs1207)
- 副社長 [呆呆大蛙](https://github.com/daidaidarwa)
----
## 參考資料
- [Python-100-Days](https://github.com/jackfrued/Python-100-Days)
- [Wikipedia](https://zh.wikipedia.org)
- [成大X-Village教材](https://www.facebook.com/pages/category/Education/X-Village-423736361424301)
{"metaMigratedAt":"2023-06-15T01:33:48.573Z","metaMigratedFrom":"Content","title":"NKUST ITC 108-1 Python[3]","breaks":true,"contributors":"[{\"id\":\"0543727d-0e35-443a-a198-84223cf6d534\",\"add\":4771,\"del\":3269},{\"id\":\"8a9f2da6-22f2-4329-8d35-37824be8c99d\",\"add\":2530,\"del\":610}]"}