||
# 下載特定網址資料 # 下載特定網址資料 src = "https://www.ntu.edu.tw/" à 台大首頁 with request.urlopen(src) as response: data = response.read().decode("utf-8") # 取得網站原始碼(HTML, CSS, JS) print("讀取 "+src+" 網頁原始碼: ") print(data) print()
# 串接, 擷取公開資料 # 下載模組 import json # 台北市政府公開資料 -> 臺北市內湖科技園區廠商名錄 https://data.taipei/#/dataset/detail?id=15c3e1ae-899b-466c-a536-208497e3a369 src = "https://data.taipei/api/v1/dataset/296acfa2-5d93-4706-ad58-e83cc951863c?scope=resourceAquire" with request.urlopen(src) as response: data = json.load(response) # 利用 json 模組處理 json 資料格式 print("讀取 "+src+" 網頁原始碼: ") print(data)
# 將公司名稱列表 clist = data["result"]["results"] print("公司名稱: ") for company in clist: print(company["公司名稱"]) # 公司名稱列表 寫入檔案 with open("公司名稱列表.txt", "w", encoding="utf-8") as wFile: for company in clist: wFile.write(company["公司名稱"]+" ") |