2024年5月18日 星期六

把JSON格式的線上火車時刻表儲存到Excel檔案中

參考資料:陳會安,看圖學Python+Excel辦公室自動化程式設計,全華

資料來源:https://fchart.github.io/json/TaiwanRailway.json

範例一、使用 pandas 套件來處理和儲存資料到 Excel 檔案

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import requests
import json
import pandas as pd

# 從URL取得JSON資料
r = requests.get('https://fchart.github.io/json/TaiwanRailway.json')
json_data = json.loads(r.text)

# 轉換成DataFrame
df = pd.DataFrame(json_data)

# 儲存到Excel檔案
df.to_excel('TaiwanRailway.xlsx', index=False)

print("資料已儲存到 TaiwanRailway.xlsx")

註:index=False 表示不將 DataFrame 的索引寫入到 Excel 檔案中。

範例二、使用 openpyxl 套件直接操作 Excel 文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import requests
import json
from openpyxl import Workbook

# 從URL取得JSON資料
r = requests.get('https://fchart.github.io/json/TaiwanRailway.json')
json_data = json.loads(r.text)

# 建立一個新的Workbook
wb = Workbook()
ws = wb.active
ws.title = "TaiwanRailway"

# 寫入標題行
headers = ['車種車次 (始發站 → 終點站)', '出發時間', '抵達時間', '行駛時間', '經由', '詳細資訊', '全票', '孩童票', '訂票']
ws.append(headers)

# 寫入資料
for item in json_data:
    row = [
        item.get('車種車次 (始發站 → 終點站)'),
        item.get('出發時間'),
        item.get('抵達時間'),
        item.get('行駛時間'),
        item.get('經由'),
        item.get('詳細資訊'),
        item.get('全票'),
        item.get('孩童票'),
        item.get('訂票')
    ]
    ws.append(row)

# 儲存到Excel檔案
wb.save('TaiwanRailway.xlsx')

print("資料已儲存到 TaiwanRailway.xlsx")

範例三、使用 openpyxl 套件直接操作 Excel 文件[不使用get]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import requests
import json
from openpyxl import Workbook

# 從URL取得JSON資料
r = requests.get('https://fchart.github.io/json/TaiwanRailway.json')
json_data = json.loads(r.text)

# 建立一個新的Workbook
wb = Workbook()
ws = wb.active
ws.title = "TaiwanRailway"

# 寫入標題行
headers = ['車種車次 (始發站 → 終點站)', '出發時間', '抵達時間', '行駛時間', '經由', '詳細資訊', '全票', '孩童票', '訂票']
ws.append(headers)

# 寫入資料
for item in json_data:
    row = [
        item['車種車次 (始發站 → 終點站)'],
        item['出發時間'],
        item['抵達時間'],
        item['行駛時間'],
        item['經由'],
        item['詳細資訊'],
        item['全票'],
        item['孩童票'],
        item['訂票']
    ]
    ws.append(row)

# 儲存到Excel檔案
wb.save('TaiwanRailway.xlsx')

print("資料已儲存到 TaiwanRailway.xlsx")


2024年5月11日 星期六

在pythonanywhere設計Django網站的登入功能加上圖形驗證

上一篇文章:在pythonanywhere設計Django網站的登入功能

1.安裝django-simple-capcha套件


2.新增captchas APP到mysite/mysit/setting.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 4.0.6.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-8c5r7ez2#g2j5n@yxeczdyhw6-zwgj_l64=nark$wc_mn+kf*4'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['cmlin.pythonanywhere.com']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'bootstrap5',
    'myapp',
    'captcha',
]

CAPTCHA_NOISE_FUNCTIONS = (
    #'captcha.helpers.noise_null', #没有樣式
    'captcha.helpers.noise_arcs', #線
    'captcha.helpers.noise_dots', #點
)

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR/'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# default static files settings for PythonAnywhere.
# see https://help.pythonanywhere.com/pages/DjangoStaticFiles for more info
MEDIA_ROOT = '/home/cmlin/mysite/media'
MEDIA_URL = '/media/'
STATIC_ROOT = '/home/cmlin/mysite'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    BASE_DIR/'static',
    ]

2.修改mysite/mysite/urls.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from myapp.views import index, vegetable, fruit, post1, post2, edit, login, logout, register

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', index),
    path('index/', index),
    path('vegetable/', vegetable),
    path('fruit/', fruit),
    path('post1/', post1),
    path('post2/', post2),
    path('edit/<int:id>/',edit),
    path('edit/<int:id>/<str:mode>', edit), # 由 edit.html 按 送出 鈕
    path('login/', login),
    path('logout/', logout),
    path('register/', register),
    path('captcha/', include('captcha.urls')),
]

3.修改mysite/myapp/froms.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from django import forms
from captcha.fields import CaptchaField

class PostForm(forms.Form):
	image = forms.CharField(max_length=20,initial='')
	title = forms.CharField(max_length=20,initial='',required=False)
	subtitle = forms.CharField(max_length=20,initial='',required=False)

class PostForm2(forms.Form):
    username = forms.CharField(max_length=20,initial='')
    pd = forms.CharField(max_length=20,initial='')
    captcha = CaptchaField()

4.修改mysite/myapp/views.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from django.shortcuts import render, redirect
from myapp.models import vegetableDB
from myapp.forms import PostForm, PostForm2
from django.contrib.auth import authenticate
from django.contrib import auth
from django.contrib.auth.models import User
from django.http import HttpResponse

# Create your views here.
def index(request):
    slide1 = {'image':'fruits01.jpg', 'active':'active', 'interval':'10000', 'title':'水果01', 'subtitle':'好水果在雲林'}
    slide2 = {'image':'fruits02.jpg', 'active':'', 'interval':'2000', 'title':'水果02', 'subtitle':'好水果在雲林'}
    slide3 = {'image':'fruits03.jpg', 'active':'', 'interval':'3000', 'title':'水果03', 'subtitle':'好水果在雲林'}
    slides = [slide1, slide2, slide3]
    if 'login' in request.session:
        login = request.session['login']
    else:
        login = 0
    return render(request, 'index.html', locals())

def vegetable(request):
    vegetables = vegetableDB.objects.all().order_by('id')
    return render(request, 'vegetable.html', locals())

def fruit(request):
    return render(request, 'fruit.html', locals())

def post1(request):
	if request.method == "POST":
		image1 = request.POST['image']
		title1 =  request.POST['title']
		subtitle1 =  request.POST['subtitle']
		#新增一筆記錄
		unit = vegetableDB.objects.create(image=image1, title=title1, subtitle=subtitle1)
		unit.save()
		return redirect('/')
	else:
		message = '請輸入資料(資料不作驗證)'
	return render(request, "post1.html", locals())

def post2(request):  #新增資料,資料必須通過驗證
	if request.method == "POST":  #如果是以POST方式才處理
		postform = PostForm(request.POST)  #建立forms物件
		if postform.is_valid():			#通過forms驗證
			image1 = postform.cleaned_data['image'] #取得表單輸入資料
			title1 =  postform.cleaned_data['title']
			subtitle1 =  postform.cleaned_data['subtitle']
			#新增一筆記錄
			unit = vegetableDB.objects.create(image=image1, title=title1, subtitle=subtitle1)
			unit.save()
			return redirect('/')
		else:
			message = '驗證碼錯誤!'
	else:
		message = 'image, title, subtitle'
		postform = PostForm()
	return render(request, "post2.html", locals())

def edit(request,id=None,mode=None):
	if mode == "edit":  # 由 edit.html 按 submit
		unit = vegetableDB.objects.get(id=id)  #取得要修改的資料記錄
		unit.image=request.GET['image']
		unit.title=request.GET['title']
		unit.subtitle=request.GET['subtitle']
		unit.save()  #寫入資料庫
		return redirect('/vegetable/')
	else: # 由網址列
		try:
			unit = vegetableDB.objects.get(id=id)  #取得要修改的資料記錄
		except:
			message = "此 id不存在!"
		return render(request, "edit.html", locals())

def login(request):
	if request.method == 'POST':
	    postform2 = PostForm2(request.POST)
	    if postform2.is_valid():
	        name = postform2.cleaned_data['username']
	        password = postform2.cleaned_data['pd']
	        user = authenticate(username=name, password=password)
	        if user is not None:
	            if user.is_active:
	                auth.login(request,user)
	                message = '登入成功!'
	                request.session['login']=1
	                return redirect('/index/')
	            else:
	                message = '帳號尚未啟用!'
	                request.session['login']=0
	    else:
	        request.session['login']=0
	        return redirect('/index/')
	else:
		postform2 = PostForm2()
	return render(request, "login.html", locals())

def logout(request):
	auth.logout(request)
	request.session['lgoin']=0
	return redirect('/index/')

def register(request):
	if request.method == 'POST':
		name = request.POST['username']
		first_name = request.POST['first_name']
		last_name = request.POST['last_name']
		password = request.POST['password']
		email = request.POST['email']
		user = authenticate(username=name, password=password)
		try:
		    user=User.objects.get(username=name)
		except:
		    user=None
		if user!=None:
		    message = user.username + " 帳號已建立!<a href='/index/'>Home</a>"
		    return HttpResponse(message)
		else:	# 建立 test 帳號
		    user=User.objects.create_user(name,email,password)
		    user.first_name=first_name
		    user.last_name=last_name
		    user.is_staff=True
		    user.save()
		    return redirect('/index/')
	return render(request, "register.html", locals())

5.修改mysite/templates/login.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<html>
<form action="." method="POST" name="form1">
    {% csrf_token %}
    <div>帳號:{{postform2.username}}</div>
    <div>密碼:{{postform2.pd}}</div>
    <div>驗證碼:{{postform2.captcha}}</div>
    <div>
    <input type="submit" name="button" id="button" value="登入" />
    </div>
    <span style="color:red">{{message}}</span>
</form>
<h2><a href='/register/'>註冊</a></h2>
</html>

6.執行migrate指令

7.執行結果:


2024年5月10日 星期五

在pythonanywhere使用Python和Django設計落花生農產品交易行情網站

參考文章:

請先按第一篇文章建立Django網站,並參考第二篇文章的爬蟲程式。

完整程式如下:

1.mysite/mysite/settings.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 4.0.6.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-3j-#(vzts_r-tw_@j!)o3kk6777%n#p9#@+8gtgx^dwdqs+vmn'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['chengmin.pythonanywhere.com', 'data.coa.gov.tw']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# default static files settings for PythonAnywhere.
# see https://help.pythonanywhere.com/pages/DjangoStaticFiles for more info
MEDIA_ROOT = '/home/chengmin/mysite/media'
MEDIA_URL = '/media/'
STATIC_ROOT = '/home/chengmin/mysite/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    BASE_DIR/'static',
    ]

2. mysite/mysite/urls.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from myapp.views import index

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', index)
]

3.mysite/myapp/views.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django.shortcuts import render
import requests
import json

# Create your views here.

def index(request):
#    proxies = {'http': 'proxy.server:3128', 'https': 'proxy.server:3128'}
    r = requests.get('https://data.moa.gov.tw/Service/OpenData/FromM/FarmTransData.aspx')
#    r = str(r).strip("'<>() ").replace('\'', '\"')
    peanut = json.loads(r.text)
    rows = []
    for row in peanut:
        if row['作物名稱'] is None:
            continue
        if '落花生' in row['作物名稱']:
            rows.append(row)
    return render(request, "index.html", locals())

4.mysite/templates/index.html


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<html>
<head>
<TITLE>
尚虎雲產銷平台
</TITLE>
</head>
<body>
<center>
<h1>
尚虎雲產銷平台
</h1>

</center>
{% for row in rows %}
{% if forloop.first %}
<table style='width:100%' border='1'>
<caption><h2>落花生市場交易情形</h2></caption>
<tr><th>交易日期</th><th>作物代號</th><th>作物名稱</th><th>市場代碼</th><th>市場名稱</th><th>上價</th><th>中價</th><th>下價</th><th>平均價</th><th>交易量</th></tr>
{% endif %}
<tr>
<td><center>{{ row.交易日期 }}</center></td>
<td><center>{{ row.作物代號 }}</center></td>
<td><center>{{ row.作物名稱 }}</center></td>
<td><center>{{ row.市場代號 }}</center></td>
<td><center>{{ row.市場名稱 }}</center></td>
<td><center>{{ row.上價 }}</center></td>
<td><center>{{ row.中價 }}</center></td>
<td><center>{{ row.下價 }}</center></td>
<td><center>{{ row.平均價 }}</center></td>
<td><center>{{ row.交易量 }}</center></td>
</tr>
{% if forloop.last %}
</table>
{% endif %}
{% endfor %}
</body>

</html>


2024年5月9日 星期四

用Python來撰寫顯示吳郭魚在斗南市場交易情形的程式

開放資料:漁產品交易行情

1.程式碼:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import requests
import json
r = requests.get('https://data.moa.gov.tw/Service/OpenData/FromM/AquaticTransData.aspx')
text = json.loads(r.text)
for row in text:
    if '吳郭魚' in row['魚貨名稱'] and '斗南' in row['市場名稱']:
        print ('交易日期:'+row['交易日期'])
        print ('品種代碼:'+str(row['品種代碼']))
        print ('魚貨名稱:'+row['魚貨名稱'])
        print ('市場名稱:'+row['市場名稱'])
        print ('上價:'+str(row['上價']))
        print ('中價:'+str(row['中價']))
        print ('下價:'+str(row['下價']))        
        print ('交易量:'+str(row['交易量']))
        print ('平均價:'+str(row['平均價'])) 

2.執行結果:
交易日期:1130509
品種代碼:1011
魚貨名稱:吳郭魚
市場名稱:斗南
上價:82.0
中價:76.9
下價:72.0
交易量:1559.1
平均價:76.9
交易日期:1130508
品種代碼:1011
魚貨名稱:吳郭魚
市場名稱:斗南
上價:82.0
中價:74.9
下價:67.3
交易量:1620.9
平均價:74.8
交易日期:1130507
品種代碼:1011
魚貨名稱:吳郭魚
市場名稱:斗南
上價:82.0
中價:77.2
下價:65.6
交易量:1890.9
平均價:75.8
交易日期:1130505
品種代碼:1011
魚貨名稱:吳郭魚
市場名稱:斗南
上價:83.1
中價:82.4
下價:82.0
交易量:1771.6
平均價:82.4
交易日期:1130504
品種代碼:1011
魚貨名稱:吳郭魚
市場名稱:斗南
上價:82.0
中價:81.7
下價:77.4
交易量:1989.9
平均價:80.9
交易日期:1130503
品種代碼:1011
魚貨名稱:吳郭魚
市場名稱:斗南
上價:85.2
中價:83.1
下價:82.0
交易量:1622.4
平均價:83.3
交易日期:1130502
品種代碼:1011
魚貨名稱:吳郭魚
市場名稱:斗南
上價:84.2
中價:82.0
下價:82.0
交易量:1397.8
平均價:82.4
交易日期:1130501
品種代碼:1011
魚貨名稱:吳郭魚
市場名稱:斗南
上價:82.0
中價:76.4
下價:71.5
交易量:1691.7
平均價:76.5
交易日期:1130430
品種代碼:1011
魚貨名稱:吳郭魚
市場名稱:斗南
上價:84.1
中價:81.5
下價:77.0
交易量:1725.0
平均價:81.1

2024年5月7日 星期二

使用Python和Django設計落花生農產品交易行情網站


參考文章:使用Python和Django設計百香果農產品交易行情網站

1.安裝Django套件


2.建立Django專案和應用程式


3.開啟瀏覽器,輸入127.0.0.1:8000


3.修改peanut/peanut/settings.py,黃底字是新增的指令。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
Django settings for peanut project.

Generated by 'django-admin startproject' using Django 5.0.5.

For more information on this file, see
https://docs.djangoproject.com/en/5.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure--zjwskz2!r4a(-)ck#db$_44trhc%b0v()e(vd9_2n)srub#cl'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'peanutapp',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
 ]

ROOT_URLCONF = 'peanut.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'peanut.wsgi.application'


# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = 'zh-hant'

TIME_ZONE = 'Asia/Taipei'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = 'static/'
STATIC_DIRS = [
    BASE_DIR / 'static',
]
# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

4.修改peanut/peanut/urls.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
URL configuration for peanut project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/5.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from peanutapp.views import index

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', index),
]

5.新增index函式在peanut/peanutapp/views.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from django.shortcuts import render
import requests
import json

def index(request):
    r = requests.get('https://data.coa.gov.tw/Service/OpenData/FromM/FarmTransData.aspx')
    peanuts = json.loads(r.text)
    rows = []
    for row in peanuts:
        if row['作物名稱'] is None:
            continue
        if '落花生' in row['作物名稱']:
            rows.append(row)
    return render(request, "index.html", locals())

6.新增index.html到peanut/templates

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<html>
<head>
	<TITLE>
		尚虎雲產銷平台
	</TITLE>
</head>
<body>
	<center>
		<h1>尚虎雲產銷平台</h1>
		<h2>執行單位:<a href="http://www.nkut.edu.tw">國立虎尾科技大學 電機資訊學院</a></h2>
	</center>
	{% for row in rows %}
		{% if forloop.first %}
			<table style='width:100%' border='1'>
				<caption><h2>落花生市場交易情形</h2></caption>
				<tr>
					<th>交易日期</th>
					<th>作物代號</th>
					<th>作物名稱</th>
					<th>市場代碼</th>
					<th>市場名稱</th>
					<th>上價</th>
					<th>中價</th>
					<th>下價</th>
					<th>平均價</th>
					<th>交易量</th>
				</tr>
		{% endif %}
				<tr>
					<td><center>{{ row.交易日期 }}</center></td>
					<td><center>{{ row.作物代號 }}</center></td>
					<td><center>{{ row.作物名稱 }}</center></td>
					<td><center>{{ row.市場代號 }}</center></td>
					<td><center>{{ row.市場名稱 }}</center></td>
					<td><center>{{ row.上價 }}</center></td>
					<td><center>{{ row.中價 }}</center></td>
					<td><center>{{ row.下價 }}</center></td>
					<td><center>{{ row.平均價 }}</center></td>
					<td><center>{{ row.交易量 }}</center></td>
				</tr>
		{% if forloop.last %}
			</table>
		{% endif %}
	{% endfor %}
</body>

</html>

7.執行結果:
如文章開頭的圖片。

2024年5月6日 星期一

自動化下載網路圖檔

參考資料:陳會安,看圖學Python+Excel辦公室自動化程式設計,全華

1.下載單一圖檔

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import requests
from PIL import Image

url = "https://csie.nfu.edu.tw/images/虎科大-資工系-t-w-LOGO-x.png"
path = "LOGO.png"
response = requests.get(url)
if response.status_code == 200:
    with open(path, 'wb') as fp:
        for chunk in response:
            fp.write(chunk)
    print("LOGO圖檔已經下載")
    im = Image.open(path)
    im.show()
else:
    print("錯誤! HTTP請求失敗...")

執行結果:
LOGO圖檔已經下載



2.下載兩個png圖檔

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import requests
from PIL import Image

url = ["https://csie.nfu.edu.tw/images/虎科大-資工系-t-w-LOGO-x.png",
       "http://nfuee.nfu.edu.tw/wp-content/uploads/2022/12/nfu-logo.png"]
path = "LOGO"
for i, pic in enumerate(url):
    response = requests.get(pic)
    if response.status_code == 200:
        with open(path+str(i+1)+".png", 'wb') as fp:
            for chunk in response:
                fp.write(chunk)
        print("LOGO", i+1,"圖檔已經下載")
        im = Image.open(path+str(i+1)+".png")
        im.show()
    else:
        print("錯誤! HTTP請求失敗...")

執行結果:
LOGO 1 圖檔已經下載
LOGO 2 圖檔已經下載





2024年5月2日 星期四

將圖片剪裁成8*8的小圖

1.原始圖片


2.撰寫程式

1
2
3
4
5
6
7
from PIL import Image

im = Image.open("rural.jpg")
for i in range(8):
    for j in range(8):
        im2 = im.crop((128*i,128*j,128*(i+1),128*(j+1)))
        im2.save("rural_croped"+str(i)+str(j)+".jpg")

3.執行結果: