# 第二週 大數據分析實務&商業智慧與巨量資料分析 > ==**文章、新聞網站 爬蟲**==[color=#EA0000] ###### tags: `大數據分析實務` `商業智慧與巨量資料分析` `碩士` `複習用` `高科大` >:::spoiler 文章目錄 >[TOC] >::: {%hackmd @chiaoshin369/bigdata_url_temple %} ## 第二周內容 > **時間:2024/3/6(三)** [color=#ffe260] > **2025/2/25(二)、2025/2/27(四)** ### 說明 主要講解文章如何使用美麗的湯去爬出資料,講小作業。 ### 下載檔案 >[!Note] >選取 w02 所有檔案 download。 ![image](https://hackmd.io/_uploads/r1cmncq5ye.png) 如果 **無法使用** 或 **檔案連結已被老師變更**,請至 `Github倉庫` 下載 >[!Important] >[bigdata/2025/class2 at main · chiaoshin/bigdata | Github倉庫下載連結](https://github.com/chiaoshin/bigdata/blob/main/2025/class2/drive-download-20250227T012903Z-001.zip) >![bigdata_download](https://hackmd.io/_uploads/rJwJfUT5yx.png) 檔案結構目錄: 1. [介紹美麗的湯、爬取高科校網圖片 | w02-1-Beautiful soup introduction]() 2. [爬取一篇CNA新聞內容報導、爬取多篇CNA類別新聞 | w02-2-Crawl CNA news content using beautiful soup4]() 3. [作業 | Crawl articles from a specific category on your chosen news website]() ### 簡介 分析 「新聞網站」,爬新聞。 Beautiful Soup是用Python寫的一個HTML/XML的解析器。 尋找特定標籤或有著特定屬性的標籤Tag,有以下兩種方法。 ### findAll & select用法 > 用來找多筆資料。 > attrs > `findAll(name=None,attrs={},recursive=True,text=None,limit=None,**kwargs)` ### find & selectone用法 > 用來找第一筆資料。 例如:抓校網第一張圖片。 ```python= # 兩種寫法可抓出 page find('mate',{'property':"og:image"})['content'] page find('mate',{'property':"og:image"}).get('content') # 最終輸出 # 'https://www.nkust.edu.tw/var/file/0/1000/pictures/740/m/mczh-tw400x400_small41300_240231455219.jpg' # 用變數承接 photo_url = page find('mate',{'property':"og:image"})['content'] # 導出圖片 from IPython.display import Image Image(photo_url) ``` ## 先嘗試抓取高科校網的圖片 > 校網資料: [備戰放視大賞 高科大文創系畢展「轉檔完成」 - 國立高雄科技大學,NKUST,National Kaohsiung University of Science and Technology](https://www.nkust.edu.tw/p/406-1000-41300,r12.php?Lang=zh-tw) 根據 `10-Get(Crawl) a simple page (高科新聞) using bs4.ipynb`檔案,執行其內容 >[!Caution] Caution >請留意 ! ! ! 如果無法執行其中一段程式碼,有可能是環境尚未安裝成功,請開啟虛擬環境ai25後,將錯誤題示所說的套件安裝。 > Ex. `pip install lxml` ```python=1 import requests from bs4 import BeautifulSoup import re import lxml url = 'https://www.nkust.edu.tw/p/406-1000-41300,r12.php?Lang=zh-tw' r = requests.get( url ) r print(r.text) page = BeautifulSoup(r.text,'lxml') page page.find(name = "p") page.find(name = "p", style="text-align: justify;") # the same result page.find(name = "p", attrs = {'style' : "text-align: justify;"}) # get content text page.find(name = "p", attrs = {'style' : "text-align: justify;"}).text page.findAll('p', style="text-align: justify;") [content.text for content in page.findAll('p', style="text-align: justify;")] nkust_news = [content.text for content in page.findAll('p', style="text-align: justify;")] nkust_news "\n".join(nkust_news) result = "\n".join(nkust_news) print(result) page.select_one('p') page.select_one('p[style="text-align: justify;"]') page.select('p[style="text-align: justify;"]') # Get Picture page.findAll("meta") page.find('meta',{'property':"og:image"}) page.find('meta',{'property':"og:image"})['content'] page.find('img') page.findAll('img') page.find('img', {"class": "img-responsive"}) page.findAll('img', {"class": "img-responsive"}) for img in page.findAll('img', {"class": "img-responsive"}): print(img['src']) [img['src'] for img in page.findAll('img', {"class": "img-responsive"})] ["https://www.nkust.edu.tw"+img['src'] for img in page.findAll('img', {"class": "img-responsive"})] # show url picture photo_url = page.find('meta',{'property':"og:image"})['content'] from IPython.display import Image Image(photo_url) ``` 最後,我們會獲得該校網的其中一張圖片 ![image](https://hackmd.io/_uploads/BJIABHa5yg.png) ## 回家作業 HW w02-3-HW@w02-Crawl articles from a specific category on your chosen news website ![image](https://hackmd.io/_uploads/rk0I7wTq1e.png) :::spoiler 作業說明(大學部,碩班請照教學平台說明,參考內容切勿照做) :::success 請依教材講義內容撰寫程式,爬取特定類型平台的新聞(避免使用老師範例新聞,如:高科新聞、CNA中央通訊社等),可提前規劃期中報告專案主題,爬取相關文章內容。 完成後,須將程式執行過程及結果,整理至同一個 .ipynb 檔案內,確保專案可正常執行,並將所有相關檔案壓縮成 zip,最後命名為《第2週-作業1-新聞爬蟲.zip》後繳交。 注意: 檔案若有遺漏或無法執行,將以零分計算。 Crawl articles from a specific category on your chosen news website ::: --- :::spoiler 最後更新日期 >==第一版==[time=2024 3 27 , 2:30 AM][color=#786ff7] >第二版[time=2025 2 27 , 12:05 PM][color=#ce770c] <!-- >第三版[time=2022 9 23 , 9:11 PM][color=#ce770c] --> >**最後版[time=2025 2 27 , 12:05 PM]**[color=#EA0000] :::