Requests 是一個 Python HTTP 庫,但不是內建,所以要另外安裝
- #!/usr/bin/python
- #-*- coding:utf-8 -*-
- import requests
- # 最 陽春/基本 的語法
- # res = requests.get('https://網址,一定要 https')
- # print("res = "+res.text) # 顯示 server 傳來的字串內容
- # 如果網站用的是像 Let's Encrypt 的免費認證
- # 造成傳來的字串內容為 An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security. 的錯誤訊息
- # 可加下列的 headers
- headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0', }
- res = requests.get('https://網址,一定要 https', headers=headers)
- param = {'參數名1':參數值1, '參數名2':參數值2}
- res = requests.get('https://網址,一定要 https', param, headers=headers) # 網址後帶參數 → https://網址?參數名1=參數值1&參數名2=參數值2
- # print(res.json()) # 因為字串內容是 json, 所以這指令可顯示 json 內容
- lstDataset = res.json() # 將 json 內容存入 list
- for row in lstDataset:
- print("prod no = "+row['prodno']+", prod name = "+row['prodname'])
沒有留言:
張貼留言