owned this note changed 5 years ago
Linked with GitHub

Web Scrapping, Crawler and PHP - 李昀陞

tags: COSCUP2020 中階 RB105

歡迎來到 https://hackmd.io/@coscup/2020 共筆

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

點擊本頁上方的 開始用 Markdown 一起寫筆記!
手機版請點選上方 按鈕展開議程列表。

請從這裡開始

http://slides.com/peter279k/web-scrapping-crawler-and-php

About me

  • 開源專案貢獻者
  • 現任職於工研院
  • PHP 5.3~7+

Jokes about web crawler and php

為什麼 Python 爬蟲相關的書那麼多,PHP 卻只有一本?

My small story about learning web crawler

  • 《Webbots, crawlers, and screen scrapers》
    • 作者有回信

My first web crawler

校園 Wi-Fi 自動驗證

Screen scrapers

iMacros

  • 用腳本操控Web瀏覽器
  • 商用授權,後來就沒聽到人使用了

First hackathon in 2015

  • 自動爬取學校新聞爬蟲

Back nowadays

Fundameals

  • Web Bot
  • Web Crawler
  • Web Scraping

Develop your first web crawler

Environment

  • Linux(Especially Ubuntu 16.04+
  • Google Chrome
  • PHP

Installation

  • 安裝 Composer

  • guzzle

  • symfony/dom-crawler

  • symfony/css-selector

  • 或是用打包好的 Docker (待補)

Inspection

Target URL

Latest News URL

HTTP inspection

  • 頁面自動載入
  • Chrome 瀏覽器的開發者工具列可以看載入頁面的請求 header, Centent-Type 為 text/html
  • 開啟 Response 分頁結果內容是 json

Organize thoughts

  1. 請求網址
  2. 發送請求Two
  3. 解析 a tag 得出下一頁的網址
  4. 最後會得到一個關聯式陣列

DOM Tree

  • 取得底下某個 tag 的相關資訊

Basic Case Study

Target URL for iframe

  • 台電備轉容量
  • Intern: I cannot find these data sets
  • 圖表是一個 iframe 的獨立頁面
  • 載入 3 個 CSV 檔(107~109 民國年)

Advanced/Useful

Subject System: ASP.NET

  • 最難爬取
  • 選課系統
  • 奇怪的 hidden value

Invalid User Agent

Subject System Crawling

  • Request Header 帶過去
  • Cookie 設好

Invalid Order Form Params

  • 參數順序錯就會觸發 Invalid ViewState 錯誤

Javascript

  • 在瀏覽器前端做一些處理

Web Page loading

  • Open Web browser and type URL
  • Web page loading
  • DOM ready
  • DOMContentLoaded
  • Execute JS
  • Page Ready

Web Page loading for Web Driver/Engine

  • Selenium
  • headless chrome
    • peppeteer
      • 簡化 headless chrome 操作
  • PhantomJS(現已不再維護)

headless chrome

chome-php/chrome php 套件

Case Study

某廣播網站

  • 節目頁面內容全部載入 json ,假的載入更多實現
  • 播放頁面音檔 key 放在 local storage
    • json string 的 json string(!)
  • 使用 headless Chrome 執行 javascript 取得需要的東西

Anti Crawler / Anti-Bot

  • 用 sleep 來模擬人類操作迴避

Are you afraid of Captcha

  • 假的驗證碼
  • 圖片驗證碼
    • 用 OCR 解決(Tesseract)

Organize Thoughts

Command Wrapper Tesseract in PHP

Tesseract OCR for PHP

More funny captchas

funny-captcha.md

總結

  • 寫給 PHP 開發者的 30 堂網路爬蟲開發
  • 有趣驗證碼的分析
  • Tesseract
  • 通靈

Q&A

Select a repo