---
# System prepended metadata

title: My Final Project

---

# My Final Project

```python=
import requests
import pandas as pd
import json
import gspread
from datetime import datetime

import_data_oil = []
ipmort_data_4L = []
import_data_30L = []

today = datetime.now().strftime("%Y-%m-%d")
print(today)

import_data_oil.append(today)
ipmort_data_4L.append(today)
import_data_30L.append(today)

def convert_dict(tup):
    dic = dict(tup)
    return dic

url = "https://www.consumer.gov.mo/api02/api/fuel/getBounsData/cn?category=001&sid=fuel_next_app"
rs = requests.get(url)
json_data = json.loads(rs.content)
json_data["data"]

df = pd.DataFrame.from_dict(json_data["data"])
df = df.drop(columns=["StationCode", "StationImgLink","StationGoogle_map_x",
                      "StationGoogle_map_y","MarkCode","BonusCode","MarkSort","MarkCHImgLink","bouns_order"])

df = df.loc[df["BonusCHName"] == "會員優惠"]
df2 = df.groupby(by=["MarkCHName","dout"])

list_oil = []
for i,j in df2:
  list_oil.append(i)


dict_oil = convert_dict(list_oil)
for i in dict_oil.keys():
    oil_price_4L = float(dict_oil[i]) * 4
    oil_price_30L = float(dict_oil[i]) * 30
    
    import_data_oil.append(dict_oil[i])
    ipmort_data_4L.append(oil_price_4L)
    import_data_30L.append(oil_price_30L)
    
    print(f"{i} - ${dict_oil[i]}/L：\n4L - ${oil_price_4L:5.2f}、30L - ${oil_price_30L:5.2f}")
    print("###########################")


gc = gspread.service_account(filename='service_account.json')
sh = gc.open("油價記錄")

wks = sh.worksheet("油價")
last_row = wks.row_values(wks.row_count)
rs = wks.append_row(import_data_oil)
print(rs)

wks = sh.worksheet("4L")
last_row = wks.row_values(wks.row_count)
rs = wks.append_row(ipmort_data_4L)
print(rs)

wks = sh.worksheet("30L")
last_row = wks.row_values(wks.row_count)
rs = wks.append_row(import_data_30L)
print(rs)
```


```python=
import requests
from bs4 import BeautifulSoup

url = "https://www.io.gov.mo/cn/news/list/b/?d=11"

rs = requests.get(url)
html_data = rs.content.decode("utf-8")

soup = BeautifulSoup(html_data, "html.parser")

t1 = soup.find_all("td", class_="col-md-9")

for td in t1: # 找出所有 td 後，要使用迴圈一個一個提取 td 中的內容
    
    a_tags = td.find_all("a") # 每個 td 中含有 a 標籤
    td_text = td.text
    
    if("開考。" in td_text):
        for a_tag in a_tags: # 再使用迴圈提取每一個 a 標籤中的 href
            href = a_tag.get("href")
            href = 'https://www.io.gov.mo' + href
            print(td_text)
            print(href)
    
```
