## wk15_1214_lineStickerDownload_陳姿頴
1. 課本原始檔
2. 影音教學 step by step
3. stickershop 貼圖完整
4. emojishop 表情貼
5. chatGpt - local jpg
6. chatGpt - line png
## 【inclass practice】
### 期末專題參考
##### 1. line貼圖收集器
```python
## 課本原始檔
import requests, json, os
from bs4 import BeautifulSoup
url='https://store.line.me/stickershop/product/29787/zh-Hant'
#url = 'https://store.line.me/emojishop/product/5bff5de1040ab1c34cc33891/zh-Hant' #手寫的ㄅㄆㄇ就是有感情
#url='https://store.line.me/emojishop/product/64c3754739b56b2f79dcfe93/zh-Hant' #軟爛貓咪動態表情貼 ˗ˏˋ ♡ ˎˊ˗
html = requests.get(url)
sp = BeautifulSoup(html.text, 'html.parser')
datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem static-sticker'})
#datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem'}) #表情貼
# 建立目錄儲存圖片
images_dir= "line_image/"
if not os.path.exists(images_dir):
os.mkdir(images_dir)
for data in datas:
imginfo = json.loads(data.get('data-preview'))
id = imginfo['id']
imgfile = requests.get(imginfo['staticUrl'])
full_path = images_dir + id + '.png'
with open(full_path, 'wb') as f:
f.write(imgfile.content)
print(full_path)
```
```python
#lineSticker 教學-1
import requests, json, os
from bs4 import BeautifulSoup
```
```python
#lineSticker 教學-2
url = 'https://store.line.me/stickershop/product/8991459/zh-Hant'
html = requests.get(url)
html
```
```python
#lineSticker 教學-3
html.text
```
```python
#lineSticker 教學-4
sp = BeautifulSoup(html.text, 'html.parser')
sp
```
```python
#lineSticker 教學-5
sp.title.text
```
```python
#lineSticker 教學-6
sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem static-sticker'})
```
```python
#lineSticker 教學-7
sp = BeautifulSoup(html.text, 'html.parser')
datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem static-sticker'})
print(datas)
```
```python
#lineSticker 教學-8
len(datas)
```
```python
#lineSticker 教學-9
for data in datas:
print(data.get('data-preview'))
```
```python
#lineSticker 教學-10
for data in datas:
imginfo = json.loads(data.get('data-preview'))
print(imginfo['id'])
print(imginfo['staticUrl'])
```
```python
#lineSticker 教學-11
imges_dir = "line_image/"
if not os.path.exists(imges_dir):
os.mkdir(imges_dir)
for data in datas:
imginfo = json.loads(data.get('data-preview'))
id = imginfo['id']
imgfile = requests.get(imginfo['staticUrl'])
full_path = imges_dir + id + '.png'
with open(full_path, 'wb') as f:
f.write(imgfile.content)
print(full_path)
```
```python
#stickershop 完整
import requests, json, os
from bs4 import BeautifulSoup
#url = 'https://store.line.me/stickershop/product/8991459/zh-Hant' #人的日常
url = 'https://store.line.me/stickershop/product/24885624/zh-Hant' #喵咪跟兔嘰
html = requests.get(url)
sp = BeautifulSoup(html.text, 'html.parser')
datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem static-sticker'})
imges_dir = "line_image/"
if not os.path.exists(imges_dir):
os.mkdir(imges_dir)
for data in datas:
imginfo = json.loads(data.get('data-preview'))
id = imginfo['id']
imgfile = requests.get(imginfo['staticUrl'])
full_path = imges_dir + id + '.png'
with open(full_path, 'wb') as f:
f.write(imgfile.content)
print(full_path)
```
```python
#表情貼-1
import requests, json, os
from bs4 import BeautifulSoup
#url = 'https://store.line.me/stickershop/product/8991459/zh-Hant' #人的日常
#url = 'https://store.line.me/stickershop/product/24885624/zh-Hant' #喵咪跟兔嘰
url = 'https://store.line.me/emojishop/product/646b23ff903b5543729ffa4d/zh-Hant' #超實用❤生活用語對話框
#url = 'https://store.line.me/emojishop/product/61b04cbaf864dd7f3763ab8b/zh-Hant' #超實用❤手勢/符號 動態表情貼
html = requests.get(url)
html
```
```python
#表情貼-2
sp = BeautifulSoup(html.text, 'html.parser')
#datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem static-sticker'})
#datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem'})
datas = sp.find_all('li', {'class':'mdCMN09Li FnStickerPreviewItem'})
print(datas)
```
```python
#表情貼-3
imges_dir = "line_image_emojishop/生活用語對話框/"
if not os.path.exists(imges_dir):
os.mkdir(imges_dir)
for data in datas:
imginfo = json.loads(data.get('data-preview'))
id = imginfo['id']
imgfile = requests.get(imginfo['staticUrl'])
full_path = imges_dir + id + '.png'
with open(full_path, 'wb') as f:
f.write(imgfile.content)
print(full_path)
```
```python
## chatGpt local jpg
import shutil
source_path = r'C:\zzz\111.png'
destination_path = r'C:\zzz\1.png'
# 複製文件
shutil.copyfile(source_path, destination_path)
print(f'已從 {source_path} 複製到 {destination_path}')
```
```python
## chatGpt line png
import requests
import os
url = 'https://stickershop.line-scdn.net/stickershop/v1/sticker/230054638/android/sticker.png?v=1'
save_path = r'C:\zzz\3.jpg'
#response = requests.get(url, stream=True)
response = requests.get(url)
with open(save_path, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
print(f'已從 {url} 下載並保存到 {save_path}')
```
###### 2. ceaser凱薩密碼
參考文件: https://github.com/CrypTools/CaesarCipher
```python
import string
import matplotlib.pyplot as plt
LETTERS = 'abcdefghijklmnopqrstuvwxyz0123456789,.() '
def encrypt(initial, shift):
initial = initial.lower()
output = ""
for char in initial:
if char in LETTERS:
output += LETTERS[(LETTERS.index(char) + shift) % len(LETTERS)]
return output
def decrypt(initial, shift):
initial = initial.lower()
output = ""
for char in initial:
if char in LETTERS:
output += LETTERS[(LETTERS.index(char) - shift) % len(LETTERS)]
return output
def plot_letter_frequency(text):
text = text.lower()
letter_count = {letter: 0 for letter in string.ascii_lowercase}
for char in text:
if char.isalpha():
letter_count[char] += 1
letters = list(letter_count.keys())
counts = list(letter_count.values())
plt.bar(letters, counts)
plt.xlabel('Letters')
plt.ylabel('Frequency')
plt.title('Letter Frequency in Text')
plt.savefig('./result.png')
## --------------- Main ------------------
message = input('請輸入要加密的訊息: ')
key = int(input('請輸入密鑰: '))
# 加密(產生偏移)
cipher = encrypt(message, key)
print(f'\n加密結果: {cipher}')
# 解密
plain = decrypt(cipher, key)
print(f'\n解密結果: {plain}')
#字數統計
plot_letter_frequency(cipher)
```
## 【afterclass practice】
##### ( A ) 1. 以open(filename[,mode][,encode]) 開啟檔案,下列何者是 mode 參數預設的模式?
(A) 讀取模式 (B) 寫入模式 (C) 附加模式 (D)以上皆是
##### ( C ) 2. Python 提供何種內建函式,可以開啟指定的檔案,以便進行檔案內容的讀取、寫入或修改?
(A) file() (B) input() (C) open() (D) output()
##### ( B ) 3. 下列何函式可以讀取一列字元?
(A) readable() (B) read() (C) readlines() (D)get(ch)
##### ( B ) 4. 下列程式建立的檔案物件,可以執行何種動作?
f=open('file1.txt','w')
f.write("Hello Python!")
f.close()
(A) 讀取 (B) 寫入 (C) 可讀取也可寫入 (D) 以上皆非
##### ( D ) 5. 執行下列程式,下列顯示結果何者正確?
try:
print(x)
except:
print("y")
finally:
print("z")
(A) x (B) y (C) xz (D) yz
```python
try:
print(x)
except:
print("y")
finally:
print("z")
```
y
z
##### ( A ) 6. open(filename,mode,encode) 函式的參數中,其中只有那一個參數是必填?
(A) filename (B) mode (C) encode (D) 以上皆是
##### ( B ) 7. 如果作業系統是繁體中文 Windows 系統,預設的編碼為何?
(A) UTF-8 (B) cp950 (C) unicode (D) GB2312
##### ( D ) 8. 下列有關 readlines() 的敘述,何者正確?
(A) 會讀取全部文件內容 (B) 以串列方式傳回
(C) 包括「\n」跳列字元,甚至是隱含的字元 (D) 以上皆是
##### ( A ) 9. 執行下列程式,下列顯示結果何者正確?
n=1
try:
print(n)
except:
print("變數不存在!")
(A) 1 (B) n (C) 變數不存在 (D) 以上皆是
```python
n=1
try:
print(n)
except:
print("變數不存在!")
```
1
##### ( C ) 10. 班上的除錯高手大匹,在他的程式中加入了錯誤的補捉在 try…except…finally 敘述中,無論例外有沒有發生都會執行下列那些程式區塊?
(A) try (B) except (C) finally (D) 以上皆是