--- tags: 勤益課程 title: 1101小考3 --- <style> .emp {color:red} .info {color:green} </style> # 第三次小考 ## 1101 Scripting 程式語言 :::info 注意事項 - 請在完成下列題目後,將檔案存放在以學號命名的目錄,壓縮後上傳至:arrow_right: [Dropbox 雲端](https://www.dropbox.com/request/1XF24hNnk0cqHmWDePth) - 上傳時限為 12/31(五) 24:00以前,逾期視同未交。 - 請勿以任何形式抄襲其它同學的答案,若有發現,所有人一律0分。 - 若有參考網路上之程式碼,請於程式中註明出處,避免如上的抄襲疑慮。 ::: # 題目:課程網頁檔尾文字抓取 請使用 tkinter 製作 GUI 畫面,並使用 bs4 的四種擷取方法(find()、find_all()、select_one()、select()),抓取【[課程網頁](https://irw.ncut.edu.tw/peterju/course/scripting/1101/index.html)】的檔尾文字:`© Autumn,2021 Scripting Language Syllabus`,並分別顯現以上4種方法抓取的結果。 ![](https://i.imgur.com/k1yJTPy.png) #### 輸入網址的畫面: ![](https://i.imgur.com/0CsUBwp.png) #### 按下執行的畫面: ![](https://i.imgur.com/sAJMKrT.png) #### 未輸入網址按執行的畫面: ![](https://i.imgur.com/oov2oNU.png) ## 提示 - 使用 tkinter 定義視窗(450x350),再根據老師給的畫面,進行相關佈局(建議使用 grid 或 place) - 輸入課程網頁的網址:https://irw.ncut.edu.tw/peterju/course/scripting/1101/index.html - 按下執行鈕之後,請分別以 bs4 的下列方法,抓取該網址的尾部文字:`© Autumn,2021 Scripting Language Syllabus` - find() - find_all() - select_one() - select() - 請參考老師講義有關 tkinter 的範例程式:https://docs.google.com/document/d/1gljLcch5Z8MwWuVCMZ0aWhDrvImbQEfWGWFm9S7xcdE/edit#heading=h.7cziouo4zkgm - 請使用虛擬環境建置環境,但繳交的壓縮檔無需包含 env 目錄,只要給使用的套件清單即可,可用下列指令產生 ``` pip freeze > requirements.txt ``` - 可能使用到的語法參考 ``` tk.Label(form, text='find()方法:', bg='pink', justify='left') tk.Button(form, text='離開', font=('微軟正黑體',12), fg='blue', command=exit) ``` # 解答 ## requirements.txt :::spoiler ```= beautifulsoup4==4.10.0 bs4==0.0.1 certifi==2021.10.8 charset-normalizer==2.0.9 idna==3.3 requests==2.26.0 soupsieve==2.3.1 urllib3==1.26.7 ``` ::: ## main.py :::spoiler ```python= import tkinter as tk from tkinter import messagebox import requests import bs4 # 建立 tkinter 主視窗 form = tk.Tk() # geometry方法可設定視窗大小與位置(寬X高+水平+垂直) form.geometry('450x350+50+100') # 改變背景顏色 form.configure(background='pink') # 設定視窗標題 form.title('爬蟲擷取') # 建立標籤控制項物件 Label,並定位 lbName = tk.Label(form, text='網址:', bg='pink') lbName.place(x=10, y=10, width=40, height=24) # 建立欄位控制項物件 Entry,設定預設值並定位 entryName = tk.Entry(form) urlstr = 'https://irw.ncut.edu.tw/peterju/course/scripting/1101/index.html' entryName.insert(0, urlstr) # 預設值 # 第二種預設值的設定方式 # urlstr = tk.StringVar(value='https://irw.ncut.edu.tw/peterju/course/scripting/1101/index.html') # entryName = tk.Entry(form, textvariable = urlstr) entryName.place(x=60, y=10, width=375) def button1Click(): ename = entryName.get() if ename == '': messagebox.showerror(title='訊息', message='請輸入網址') else: graplist = bs4qry() global t1,t2,t3,t4 t1["text"]= t1["text"]+'\n'+graplist[0] t2["text"]= t2["text"]+'\n'+graplist[1] t3["text"]= t3["text"]+'\n'+graplist[2] t4["text"]= t4["text"]+'\n'+graplist[3] def bs4qry(): html = requests.get(urlstr) if not html.encoding=="utf-8": html.encoding = 'utf-8' soup = bs4.BeautifulSoup(html.text, 'html.parser') # 利用 find 方法定位,回傳string target = soup.find("div", class_='copyright') # print(f"find方法:{target.p.text}") t1 = target.p.text # 利用 find_all 方法定位,回傳tuple targets = soup.find_all("div", class_='copyright') # print("find_all方法:{}".format(targets[0].text.strip())) t2 = targets[0].text.strip() # 利用 select_one 方法定位,回傳string target = soup.select_one(".copyright p") # print("select_one方法:{}".format(target.text)) t3 = target.text # 利用 select 方法定位,回傳tuple targets = soup.select(".copyright p") # print("select方法:{}".format(targets[0].text)) t4 = targets[0].text return (t1,t2,t3,t4) # 在視窗放上按鈕,設定 click 事件並定位 btn1 = tk.Button(form, text='執行', font=('微軟正黑體',12), fg='blue', command=button1Click) btn1.place(x=140, y=50, width=70, height=24) btn2 = tk.Button(form, text='離開', font=('微軟正黑體',12), fg='blue', command=exit) btn2.place(x=240, y=50, width=70, height=24) # 建立標籤控制項物件,並定位 t1 = tk.Label(form, text='find()方法:', bg='pink', justify='left') t1.place(x=20, y=90) t2 = tk.Label(form, text='find_all()方法:', bg='pink', justify='left') t2.place(x=20, y=150) t3 = tk.Label(form, text='select_one()方法:', bg='pink', justify='left') t3.place(x=20, y=210) t4 = tk.Label(form, text='select()方法:', bg='pink', justify='left') t4.place(x=20, y=270) # 使用主視窗的 mainloop 方法,等待事件觸發 form.mainloop() ``` :::