日期 | 內容 |
---|---|
11/12 | 封包、requests |
11/19 | DOM解析、API |
11/26 | 專案細節討論 |
12/03 | 專案實作 |
12/10 | 專案實作 |
12/17 | 專案驗收 |
12/26 | 期末社員大會 |
Header中會有
Payload則是視需求而定
Header中會有
Header底下則是回傳的資料
可能是網頁原始碼或二進制圖片等等
除了F12的DevTool之外,有專門抓封包的工具
一般就是個Proxy server
pip install requests
一個基本的GET請求長這樣
import requests rs = requests.get('https://www.google.com.tw/') # 對目標網站送出請求 print(rs.status_code) # 輸出伺服器狀態碼 print(rs.text) # 輸出page sourse(網頁原始碼)
可以帶上Params(參數),會自動加到URL Query
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跟原本的差異
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請求很少不帶Payload(?
所以就示範帶Payload的基礎用法
my_data ={'key1': 'value1', 'key2': 'value2'} rs = requests.post('http://httpbin.org/post', data=my_data)
有時,POST也會需要傳送檔案,在requests的post
my_files = {'docs': open('my_file.docx', 'rb')} # 要上傳的檔案 rs = requests.post('http://httpbin.org/post', files=my_files)