---
# System prepended metadata

title: 'PYTHON:raised_hands:'

---

# PYTHON:raised_hands:

### :bulb:基本語法:bulb:
|名稱|解釋|名稱|解釋|
|-|-|-|-|
|#|註解|print|在輸出畫面中顯示文字訊息|
|input|輸入資料|eval|文字資料轉數字資料
|%d|字串|%3d|字串前預留3個位置|
|{}|抓取後面資料|str|變成字串|
|int|整數|float|小數or浮點數|
|%|餘數|//|商|
|**|平方|==|判斷兩個值是否相等|
|IF|如果,A條件成立,執行A命令|elif|B條件成立,執行B指令|
|else|否則,以上條件均不符合,執行最後一行語法|and|且|
|or|或|/n|換行|
|!=|不等於|||
|||||


---

# --1215--

### :bulb:輸入輸出:bulb:
```py=
a=3
b=4
print(a+b)
```
* `Ctrl`+`F5`=執行

---

* input=輸入
* eval=文字轉數字

```py=
a=eval(input("a="))
b=eval(input("b="))
print(a+b)
```

---

* %d=字串
* %3d=預留3個位置

```py=
a=eval(input("a="))
b=eval(input("b="))
print("%d+%d=%d" % (a,b,a+b))
```

---

* {}=抓取後面資料

```py=
a=eval(input("a="))
b=eval(input("b="))
print("{}+{}={}" .format(a,b,a+b))
```

---

* str=變成字串

```py=
a="abc"
b="123"
print(a+str(b))
```

---

#### ==計算溫度==

* int=整數
* floot=浮點數

```py=
c=eval(input("請輸入攝氏溫度："))
f=9/5*c+32
print("轉換後的華氏溫度：%.2f"% f)
```

```py=
f=eval(input("請輸入華氏溫度："))
c=(f-32)*5/9
print("轉換後的攝氏溫度：%.2f"% c)
```

---

#### ==計算BMI==

* %=餘數
* //=商
* **=根號

```py=
w=eval(input("請輸入體重(kg)："))
h=eval(input("請輸入身高(cm)："))
h=h/100
b=w/(h**2)
print("轉換後的BMI：%.2f"% b)
```

---

### :bulb:關係運算:bulb:

#### ==絕對值==
```py=
a=eval(input("請輸入數字："))
if a<0:
    b=-a
else:
    b=a
print("%.2f 的絕對值是%.2f" % (a, b))
```
#### ==奇偶判斷==

* == 判斷兩邊是否相等
* %d=顯示a，%s=顯示s
```python=
a=eval(input("請輸入一個整數："))
if a%2 == 0:
    s="偶數"
else:
    s="奇數"
print("%d 是 %s" % (a,s))
```

---

### :bulb:多重判斷:bulb:

#### ==成績判斷==
```py=
score=eval(input("請輸入一個分數："))
if 100>=score and score>=88:
    s="甲等"
elif 80>score>=60:
    s="乙等"
elif 60>score>=0:
    s="丙等" 
else:
    s="輸入錯誤"     
print("%d 是 %s" % (score,s))
```

#### ==二元一次方程式==
```py=
a=eval(input("請輸入a係數："))
b=eval(input("請輸入b係數："))
c=eval(input("請輸入c常數項："))
d=b**2-4*a*c
if d>0:
    print("無解")
elif d==0:
    x=(-b)/(2*a)
    print("一組解：%.2f" % x)
else:
    x1=(-b+d**0.5)/(2*a)
    x2=(-b-d**0.5)/(2*a)
    print("兩組解：%.2f,%.2f" % (x1,x2))
```

---

#### ==商店1==
```py=
a=eval(input("請輸入消費金額："))
b=eval(input("請輸入現金："))
if a>=5000:
    a*=0.7
    b=b-a
    print("折扣後的金額：%.0f" % a)
    print("找零：%.0f" % b)
elif a<5000 and a>=3000:
    a*=0.8
    b=b-a
    print("折扣後的金額：%.0f" % a)
    print("找零：%.0f" % b)
elif a<3000 and a>=1000:
    a*=0.9
    print("折扣後的金額：%.0f" % a)
    print("找零：%.0f" % b)
elif a<1000 and a>=0:
    a=a
    print("折扣後的金額：%.0f" % a)
    print("找零：%.0f" % b)  
else:
    print("金額輸入錯誤")
```

#### ==商店2==
```py=
a=eval(input("請輸入消費金額："))
b=eval(input("請輸入現金："))
if a>=5000:
    a*=0.7
    print("折扣後的金額：%.0f" % a)
    if b<a:
        s=a-b
        print("還須補：%.0f元" % s)
    else:
        b=b-a
        print("找零：%.0f" % b)
elif a<5000 and a>=3000:
    a*=0.8
    print("折扣後的金額：%.0f" % a)
    if b<a:
        s=a-b
        print("還須補：%.0f元" % s)
    else:
        b=b-a
        print("找零：%.0f" % b)
elif a<3000 and a>=1000:
    a*=0.9
    print("折扣後的金額：%.0f" % a)
    if b<a:
        s=a-b
        print("還須補：%.0f元" % s)
    else:
        b=b-a
        print("找零：%.0f" % b)
elif a<1000 and a>=0:
    a=a
    print("折扣後的金額：%.0f" % a)
    if b<a:
        s=a-b
        print("還須補：%.0f元" % s)
    else:
        b=b-a
        print("找零：%.0f" % b)
else :
    print("金額輸入錯誤")
```

---

# --1222--

### :bulb:FOR迴圈:bulb:

#### ==1+2+...+100==
```py=
s=0
# 將數字從 1 加到 100
for i in range(1, 101, 1):
    # 將 i 加到 s 中
    s += i
print("1+2+...+100=%d" % s)
```
* s=0 宣告起始值

#### ==1+3+5+...+99==
```py=
s=0
# 將奇數從 1 加到 99
for i in range(1,100,2): 
    s+=i
print("1+3+...+99=%d" % s)
```

#### ==顯示1~100每5個數字換行==
```py=
for i in range(1,101):
    # %5d 表示輸出的數字佔 5 個位子
    print("%5d" % i, end="")

    # 每 5 個數字就換行
    if i % 5 == 0:
        print()
```
* /n=換行

---

#### ==判斷質數==
```py=
n=eval(input("請輸入一個整數："))
# 預設flag=0是質數
flag=0
# 從 n-1 開始遞減到 2
for i in range(n-1, 1, -1):
    # 如果 n 能被 i 整除，不是質數
    if n % i == 0:
        # 將flag設為 1 ，中斷迴圈
        flag = 1
        break

# 檢查flag，0 是質數
if flag == 0:
    print("%d 是質數" % n)
else:
    print("%d 不是質數" % n)
```

:::success
```py=
n=eval(input("請輸入一個整數："))
# 從 n-1 開始遞減到 2
for i in range(n-1, 1, -1):
    # 如果 n 能被 i 整除，不是質數
    if n % i == 0:
        print("%d 不是質數" % n)
        break
else:
    # 如果在迴圈中沒有被中斷，是質數
    print("%d 是質數" % n)
```
:::

#### ==1~100以內的質數==
```py=
s=0
# 找出 2 到 100 之間的質數
for n in range(2, 101):
    # 從 n-1 開始遞減到 2
    for i in range(n-1, 1, -1):
        # 如果 n 能被 i 整除，不是質數，中斷迴圈
        if n % i == 0:
            break
    else:
        # 輸出質數，質數計數器+1
        print("%5d" % n, end="")
        s += 1

        # 一行 5 個質數，換行
        if s % 5 == 0:
            print()

print("共有 %d 個質數" % s)
```

---

#### ==最大公因數==
```py=
a,b=eval(input("請輸入兩個整數："))
# 從較小的數字開始遞減到 1
for i in range(a, 0, -1):
    # 如果 a 與 b 能被 i 整除，找到最大公因數，中斷迴圈
    if a % i == 0 and b % i == 0:
        gcd = i
        break

print("最大公因數：%d" % gcd)

# 如果最大公因數為 1，兩數互質
if gcd == 1:
    print("%d, %d 兩數互質" % (a, b))
```

#### ==雞兔同籠==
:::success
```py=
t=eval(input("請輸入雞兔的總數："))
leg=eval(input("請輸入雞兔腳的總數："))
# 從 t 開始遞減到 0
for i in range(t, -1, -1):
    # 假設有 c 隻雞，r 隻兔
    c = i
    r = t - c
    
    # 檢查腳的總數
    if c * 2 + r * 4 == leg:
        print("雞有 %d 隻，兔有 %d 隻" % (c, r))
        break
else:
    # 迴圈沒有中斷表示無解
    print("無解")
```
:::

---

# --1229--

### :bulb:WHILE迴圈:bulb:

#### ==1+2+...+100==
```py=
i=1
sum=0
while i <100:
    sum+=i
    i+=1
print("1+2+3+...+100=%d" % sum)
```

#### ==存款利率==
```py=
money=eval(input("存款金額："))
rate=eval(input("輸入銀行利率："))
target=eval(input("輸入目標金額："))
year=0
# 計算需要幾年才能達到目標金額
while money <= target:
    # 存款金額根據銀行利率增加
    money = money + money * rate
    # 年份+1
    year += 1
print("需存 %d 年" % year)
print("存款金額：%.2f元" % money)
```

#### ==數字轉換==
```py=
x=eval(input("請輸入數字："))
y=0
while x > 0:
    # 取 x 的最後一位數，加到 y 的十位上
    y = x % 10 + y * 10
    # 去掉 x 的最後一位數
    x = x // 10
print("%d" % y)
```

---

### **:bulb:函式:bulb:**

#### ==執行函式==
```py=
def printstar(n):
    # 輸出星號，執行 n 次
    for i in range(0, n):
        # 輸出星號不換行
        print("*", end="")

# 傳遞參數 5，輸出 5 個星號
printstar(5)
```

#### ==階層==
```py=
# 計算 n 的階乘
def fact(n):
    p=1
    for i in range(1,n+1):
        p=p*i
    return p

# 計算 5 的階乘除以 2 的階乘
print(fact(5)/fact(2))

# 計算 5 的階乘除以 3 的階乘再除以 2 的階乘
print(fact(5)/fact(3)/fact(2))
```

#### ==1-100的總和與平均==
```py=
def sumaverage(n1,n2=100):
    sum=0
    average=0.0
    
    # 計算總和
    for i in range(n1,n2+1):
        sum+=i
        
    # 計算平均值
    average=sum/(n2-n1+1)
    
    # 返回總和和平均值
    return sum,average

def main():
    s,a=sumaverage(1,100)
    print("總和=%d,平均=%.2f" % (s,a))
```

#### ==分數相加==
:::success
```py=
# 最大公因數函數
def gcd(a, b):
    for i in range(a, 0, -1):
        if (a % i == 0) and (b % i == 0):
            x = i
            break
    return x

# 輸入兩個分數的分子分母
a1 = int(input("輸入第一個分數的分子："))
a2 = int(input("輸入第一個分數的分母："))
b1 = int(input("輸入第二個分數的分子："))
b2 = int(input("輸入第二個分數的分母："))

# 計算最大公因數
m = gcd(a2, b2)

# 計算最小公倍數
lcm = a2 * b2 // m

# 分子相加
x = a1 * (lcm // a2) + b1 * (lcm // b2)

# 化簡
z = gcd(x, lcm)
x = x / z
lcm = lcm / z

# 輸出
print("運算結果：%d / %d " % (x, lcm))
```
:::

---

# --0105--

### :bulb:上機考:bulb:
#### ==01.計算總分及平均==
```py=
# 輸入國英數成績
x=eval(input("請輸入國文成績："))
y=eval(input("請輸入英文成績："))
z=eval(input("請輸入數學成績："))

# 計算總分及平均
t= x + y + z
a= t / 3

# 輸出
print("總分為：%d，平均為：%.2f" % (t,a))
```

#### ==02.判斷三角形==
* **=平方
```py=
x, y, z = eval(input("請輸入三邊長："))

if x + y < z or x + z < y or y + z < x:
    print("無法構成三角形")
elif x**2 + y**2 == z**2 or x**2 + z**2 == y**2 or y**2 + z**2 == x**2:
    print("直角三角形")
elif x**2 + y**2 < z**2 or x**2 + z**2 < y**2 or y**2 + z**2 < x**2:
    print("鈍角三角形")
else:
    print("銳角三角形")
```

#### ==03.判斷質數==
```py=
n=eval(input("請輸入一個整數："))
# 從 n-1 開始遞減到 2
for i in range(n-1, 1, -1):
    # 如果 n 能被 i 整除，不是質數
    if n % i == 0:
        print("%d 不是質數" % n)
        break
else:
    # 如果在迴圈中沒有被中斷，是質數
    print("%d 是質數" % n)
```

#### ==04.雞兔同籠==
```py=
t=eval(input("請輸入雞兔的總數："))
leg=eval(input("請輸入雞兔腳的總數："))
# 從 t 開始遞減到 0
for i in range(t, -1, -1):
    # 假設有 c 隻雞，r 隻兔
    c = i
    r = t - c
    
    # 檢查腳的總數
    if c * 2 + r * 4 == leg:
        print("雞有 %d 隻，兔有 %d 隻" % (c, r))
        break
else:
    # 迴圈沒有中斷表示無解
    print("無解")
```

#### ==05.分數相加==
```py=
# 最大公因數函數
def gcd(a, b):
    for i in range(a, 0, -1):
        if (a % i == 0) and (b % i == 0):
            x = i
            break
    return x

# 輸入兩個分數的分子分母
a1 = int(input("輸入第一個分數的分子："))
a2 = int(input("輸入第一個分數的分母："))
b1 = int(input("輸入第二個分數的分子："))
b2 = int(input("輸入第二個分數的分母："))

# 計算最大公因數
m = gcd(a2, b2)

# 計算最小公倍數
lcm = a2 * b2 // m

# 分子相加
x = a1 * (lcm // a2) + b1 * (lcm // b2)

# 化簡
z = gcd(x, lcm)
x = x / z
lcm = lcm / z

# 輸出
print("運算結果：%d / %d " % (x, lcm))
```

---

# --0301--
### :bulb:LIST串列:bulb:

#### ==抓出list資料==
```py=
list1=[]
list2=[1,2,3,4,5]
print(list2)
i=0
for i in range(0,len(list2)):
    print("%d : %d" % (i,list2[i]))
```

#### ==list資料相加==
```py=
list1=[]
list2=[1,2,3,4,5]
print(list2)
s=0
for a in list2:
    s+=a
print("list2的總和：%d" % s)
```

#### ==移出list資料==
* append=加入進去
* insert=插入
```py=
list1=[]
list1.append(1)
list1.append(2)
list1.insert(1,4)
print(list1)
c=list1.pop(1)
print(c)
```

#### ==list資料排序==
* sort=遞增排序
* reverse=反轉
```py=
list1=[]
list1.append(1)
list1.append(2)
list1.insert(1,4)
print(list1)
list1.sort()
print(list1)
list1.reverse()
print(list1)
```

#### ==list資料大小==
```py=
list1=[]
list1.append(1)
list1.append(2)
list1.insert(1,4)
print(list1)
print(sum(list1))
print(max(list1))
print(min(list1))
```

#### ==抽籤樂透==
```py=
import random
l=[]
c=0
while c<5:
    a=random.randint(1,49)
    # 數字不重複
    if a not in l:
        print(a,end="  ")
        l.append(a)
        c+=1
```

---

### **:bulb:二維串列:bulb:**

#### ==資料相加==
```py=
import random
l=[]
c=0
while c<5:
    a=random.randint(1,49)
    # 數字不重複
    if a not in l:
        print(a,end="  ")
        l.append(a)
        c+=1
```

#### ==新增二維串列==
```py=
import random
l=[]
for i in range(4):
    # 加入空串列
    l.append([])
    for j in range(3):
        # 串列中加入3個隨機數字
        l[i].append(random.randint(1,10))

for i in l:
    for j in i:
        # i是串列，j是整數
        print("%5d" % j,end="  ")
    # 換行
    print()
```

#### ==撲克牌加總==
:::success
```py=
c=[]
for i in range(5):
    a=input()
    c.append(a)

s=0
for x in c:
    if x=="A" or x=="a":
        s+=1
    elif x=="J" or x=="j":
        s+=11
    elif x=="Q" or x=="q":
        s+=12
    elif x=="K" or x=="k":
        s+=13
    else:
        s+=int(x)
print("總和=%d" % s)
```
:::

#### ==亂數最大相加==
:::success
```py=
import random
c=[]
for i in range(10):
    c.append(random.randint(1,100))
# 小到大
c.sort()
for x in c:
    print("%4d" % x,end="  ")
print()
# 大到小
c.reverse()
for i in range(3):
    print("%4d" % c[i],end=" ")
```
:::

---

# --0308--

#### ==1-20的數字重複最多次==
:::success
```py=
import random
l=[0]*20
for i in range(0,10):
    a=random.randint(1,20)
    l[a-1]+=1
print (l)

max=0
index=""
for i in range(0,20):
    if l[i]>max:
        max=l[i]
        index=str(i+1)
    elif l[i]==max:
        index=index + "," + str(i+1)
print ("%s ：%d" % (index, max))
```
:::

---

### **:bulb:DICTIONARY字典:bulb:**

#### ==字典增加值==
```py=
dic10={}
dic10={'Taipei':"101",'Paris':"Tour Eiffel",'London':"Big Ben"}
print (dic10['Taipei'])
print (dic10['London'])
# 增加值
dic10['Berlin']='wall'
# del=刪除值
for i in dic10:
    print("%s: %s" % (i, dic10[i]))
print(dic10.keys())
# keys=鍵值
for k in dic10.keys():
    print(k)
print(dic10.values())
```

#### ==dic數字出現次數==
:::success
```py=
import random
dic={}
for i in range(5):
    a=random.randint(1,10)
    if a in dic:
        dic[a]+=1
    else:
        dic[a]=1
print (dic)
max_int=0
index=""
for i in dic:
    if dic[i]>max_int:
        max_int=dic[i]
        index=str(i)
    elif dic[i]==max_int:
        index=index + ',' + str(i)
print ("%s ：%d" % (index, max_int))
```
:::

---

### **:bulb:檔案:bulb:**

#### ==寫入檔案==
* write=寫入
* read=讀取
* open=開啟
* close=關閉
* \n=換行
```py=
with open('f.txt','w',encoding='utf-8') as f:
    # \n=換行
    f.write('中壢高商\n')
    f.write('資料處理科\n')
    f.write('蔡堯年')
```

#### ==讀取檔案==
```py=
f=open('test.txt','r',encoding='utf-8')
t=f.readline()
while t!="":
    print (t)
    a=t.split(",")
    for i in range(1,4):
        a[i] = eval(a[i])
    t=f.readline()
f.close()
```

---

# --0315--

#### ==讀寫資料==
```py=
f=open('test.txt', 'r',encoding='utf-8')
t=f.readline()
all=[]
while t!="":
    # print(t)
    a=t.split(",")
    s=0
    for i in range(1,4):
        a[i]=eval(a[i])
        s=s+a[i]
    a.append(s)
    all.append(a)
    t=f.readline()
f.close()
f=open('out.txt','w',encoding='utf-8')
for b in all:
    for c in b:
        # print(c,end="  ")
        f.write(str(c)+'  ')
    f.write('\n')    
f.close()
```

#### ==快速讀寫==
:::success
```py=
f1=open('test.txt', 'r',encoding='utf-8')
f2=open('out.txt','w',encoding='utf-8')
t=f1.readline()

while t!="":
    a=t.split(",")
    s=0
    f2.write(a[0]+','+a[1])
    for i in range(2,5):
        a[i]=eval(a[i])
        f2.write(','+str(a[i]))
        s=s+a[i]
    f2.write(','+str(s)+'\n')
    t=f1.readline()
f1.close()  
f2.close()
```
:::

---

### **:bulb:爬蟲:bulb:**
#### ==高鐵訂票網站連線==
* pip install -U requests=安裝套件
```py=
import requests
url="https://irs.thsrc.com.tw/IMINT"
h={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'}
r=requests.get(url,headers=h)
print (r)
```

#### ==PTT電影資料解析==
* pip install -U beautifulsoup4=安裝套件
```py=
import requests
import bs4
url="https://www.ptt.cc/bbs/movie/index.html"
h={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'}
# 連線結果
r=requests.get(url,headers=h)
r.encoding='utf-8'
st=r.text
# 解析結果
root=bs4.BeautifulSoup(st,'html.parser')
# print (root.title.string)
titles=root.find_all('div',class_="title")
print (title.a.string)
```

---

# --0322--

* `F12`➩`Network`➩`F5`➩`Headers`➩`User-Agent:`
#### ==PTT電影資料解析==
```py=
import requests
import bs4
url="https://www.ptt.cc/bbs/movie/index.html"
h={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'}
c=0
output=''
while c<5:
    c=c+1
    r=requests.get(url,headers=h)
    r.encoding='utf-8'
    st=r.text
    # 解析結果
    root=bs4.BeautifulSoup(st,'html.parser')
    # print (root.title.string)
    titles=root.find_all('div',class_="title")
    for title in titles:
        if title.a !=None:
            output+= title.a.text + '\n'
    #         print (title.a.text)
    # url="https://www.ptt.cc/bbs/movie/index"+str(c)+".html"
    # h={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'}
    nextlink=root.find('a',string='‹ 上頁')
    url="http://www.ptt.cc" + nextlink.get('href')

f=open('movie.text','w',encoding='utf-8')
f.write(output)
```
---

# --0329--

### :bulb:上機考:bulb:
#### ==01.1-20的數字重複次數==
```py=
# 導入 random 模組，用於生成隨機數
import random
# 生成隨機整數列表
l=[]
for i in range(0,10):
    l.append(random.randint(1,20))
# 輸出隨機整數列表
for x in l:
    print("%d" % x,end="  ")
print()

max=0
index=""
# 找出出現次數最多的整數
# 1到20的整數範圍
for c in range(1,21):
    # 如果整數 c 在列表 l 中出現的次數大於目前最大次數
    if l.count(c)>max:
        # 更新最大次數
        max=l.count(c)
        # 更新最多次數的整數
        index=str(c)
    # 如果整數 c 在列表 l 中出現的次數等於目前最大次數
    elif l.count(c)==max
        # 將該整數加入到最多次數的整數列表中
        index=index + " , " + str(c)
# 輸出結果
print ("%s：%d" % (index, max)) 
```

:::spoiler NO註解
```py=
import random
l=[]
for i in range(0,10):
    l.append(random.randint(1,20))
for x in l:
    print("%d" % x,end="  ")
print()
max=0
index=""
for c in range(1,21):
    if l.count(c)>max:
        max=l.count(c)
        index=str(c)
    elif l.count(c)==max:
        index=index + " , " + str(c)
print ("%s：%d" % (index, max))
```
:::

#### ==02.檔案讀寫==
```py=
f1 = open('test.txt', 'r', encoding='utf-8')
f2 = open('014328.txt', 'w', encoding='utf-8')

# 讀取 test.txt 檔案的第一行
t = f1.readline()

# 進行迴圈，處理每一行的資料
while t != "":
    # 將每一行的資料以逗號分割成列表
    a = t.split(",")
    s = 0
    
    # 將列表中的第一、第二個元素寫入 014328.txt 檔案
    f2.write(a[0] + ',' + a[1])
    
    # 將列表中的第三至第五個元素轉換成整數且總計，並寫入 014328.txt 檔案
    for i in range(2, 5):
        a[i] = eval(a[i])
        f2.write(',' + str(a[i]))
        s = s + a[i]
    
    # 寫入加總後的數值並換行
    f2.write(',' + str(s) + '\n')
    
    # 讀取 test.txt 檔案的下一行
    t = f1.readline()

f1.close()
f2.close()
```
:::spoiler NO註解
```py=
f1 = open('test.txt', 'r', encoding='utf-8')
f2 = open('014328.txt', 'w', encoding='utf-8')
t = f1.readline()
while t != "":
    a = t.split(",")
    s = 0
    f2.write(a[0] + ',' + a[1])
    for i in range(2, 5):
        a[i] = eval(a[i])
        f2.write(',' + str(a[i]))
        s = s + a[i]
    f2.write(',' + str(s) + '\n')
    t = f1.readline()
f1.close()
f2.close()
```
:::

---

# --0412--

#### ==自由時報抓照片==
```py=
import requests
import bs4
url="https://img.ltn.com.tw/Upload/ent/page/800/2024/02/29/phpwfkBkg.jpg"
h={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'}
r=requests.get(url,headers=h)
print(r)
with open('123.jpg',mode='wb') as file:
# with open('123.pdf',mode='wb') as file:
    file.write(r.content)
```

#### ==PTT美女版抓照片==
```py=
import requests
from bs4 import BeautifulSoup
url="https://www.ptt.cc/bbs/Beauty/M.1712837262.A.726.html"
h={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'}
r=requests.get(url,headers=h,cookies={'over18':'1'})
st=BeautifulSoup(r.text,'html.parser')
imgs=st.find_all('img')
for img in imgs:
    # print(img['src'])
    url=img['src']
    pic=requests.get(url)
    fname=url[-11:]
    f=open(fname,'wb')
    f.write(pic.content)

f.close()
```

---

# --0419--

#### ==CHROM SELENIUM截圖==
* pip install selenium

```py=
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options=Options()
options.chrome_executable_path='C:\0419'
driver=webdriver.Chrome(options=options)
driver.maximize_window()
driver.get('https://www.clvsc.tyc.edu.tw')
driver.save_screenshot('1.png')
```

#### ==EDGE SELENIUM截圖==
* pip install selenium

```py=
from selenium import webdriver
from selenium.webdriver.edge.options import Options
import time
options=Options()
options.edge_executable_path='C:\0419'
driver=webdriver.Edge(options=options)
driver.maximize_window()
driver.get('https://www.clvsc.tyc.edu.tw')
driver.save_screenshot('2.png')
time.sleep(3)
```

# -0502-

#### ==迷克夏飲料菜單==
```py=
from bs4 import BeautifulSoup
import requests
url="https://www.milksha.com/products.php"
r=requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')

titles = soup.find_all("div", class_="col-lg-6 col-md-7")

for div in titles:
    list = div.find("ul", class_="row")
    if list:
        items = list.find_all("li")
        for item in items:
            drink_name = item.text.strip().replace(" ", "").replace("\n", "")
            print(drink_name)
```