Code Monkey home page Code Monkey logo

scraper-tw-hotels's Introduction

scraper-TW-hotels (台灣旅宿網爬蟲程式)

簡介

為協助女友解決工作需要抓取台灣所有合法的旅宿資訊,因此找到「台灣旅宿網」此網站並爬取旅館、民宿的基本資訊,並保存到 Excel 檔案中。該程式於 2017/08/05 完成,透過 Python 2.7requestsbeautifuloup4 實作,後因該網站改版更新,已有提供 CSV 資料匯出所以不需要此專案,不過目前依然保持維護及更新,作為功能優化與重構的練習。

「台灣旅宿網」首頁截圖

「台灣旅宿網」旅館列表截圖

「台灣旅宿網」資訊頁面截圖

目前版本更新至 Python 3.7 並改用 pipenv 安裝套件,並且新增了儲存到 json 檔案格式中,用戶可以在一開始決定要儲存至 Excel 或是儲存到 json 檔案。

爬蟲方面因和原先舊版的網站撈取資料方式不同,採用 seleniunm 模擬點擊縣市取得各縣市的市區鄉鎮資料後,再透過 aiohttp 改成非同步請求網頁 HTML,並使用 lxml 以 XPath 抓取資料,再搭配少量 beautifuloup4 HTML 解析,抓取下來依照原先選擇的儲存方式。

最後依照一開始選擇保存方式,若為到 Excel 則使用 xlsxwriter 寫入到 Excel 檔案中;選擇 json 則透過 aiofiles 非同步儲存到 json 格式的檔案內。

儲存至 Excel - 金門縣

儲存至 Json 資料 - 台北市

過程中使用 fake-useragent 模擬 Header,另外因為網站多次請求會導致異常頁面,所以採用支持非同步的 Retry 函式庫 tenacity ,於偵測回應網址為異常網頁後,延遲依定秒數重新重試以確保資料撈取。

架構設計

由於該程式應用單純,不含業務場景,於是原先採用輕量的 Transaction Script 流程。而後為了達到整潔、職責分離,嘗試以 DDD 的思維導入,透過 Domain Model 設計,並且以 DDD 的 Module 方式劃分區塊,不過因為此爬蟲程式的 Scope 較小,業務場景難以捕捉,所以基本上以 Domain Service 搭配 Value Object 為主,或許未來可以嘗試再做調整。

--- apps # 應用服務模組
|  |
|   -- assembler # 組裝器,負責從不同的 Domain 的資料組裝成需要的資料集並轉換成 DTO 回傳
|  |
|   -- dto # 資料傳輸物件:展示層或 View 層需要的資料
|  |
|   -- services # 應用服務層:承接來自 Controller 的資料並作為處理業務需求相關的服務動作之第一層介面,類似 Facade,包裝業務場景的功能細節,會去調用 Domain 層的 Models 處理邏輯,以及儲存至資料庫等動作,或是做 Trasacntion 的檢查、日誌紀錄等。
|
--- domain # 領域模組:負責業務場景的細節
|   |
|    -- models # 領域模型
|
--- infra # infrastructure 基礎建設模組:放置領域模型或應用服務會使用的基礎工具或服務或實現領域會調用的底層細節
|   | 
|   -- asynchttp
|   |
|   -- excepts
|   |
|   -- logging
|
---- settings # 存放設定檔

開發環境

  • 程式語言:Python 3.7
  • 使用套件:aiohttp, beautifuloup4, lxml, xlsxwriter, fake-useragent, seleniunm, tenacity, aiofiles

啟動執行

$> pipenv install # 安裝 Pipfile 套件
$> pipenv shell # 進入 Pipenv 環境
$> python scraping.py # 執行

執行過程

指令輸入

抓取過程

scraper-tw-hotels's People

Contributors

kokokuo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.