An-Sheng Huang
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- slideOptions: mouseWheel: true width: 100% height: 90% margin: 0.1 minScale: 1.0 maxScale: 2.0 loop: true --- # 網頁爬蟲應用程式實作 黃安聖 ###### tags: `Python程式設計與網頁爬蟲應用程式實作(一)` ---- ### 什麼是網頁爬蟲 ---- ### 基礎的網路爬蟲 透過對伺服器發送HTTP Request取得特定網頁的原始碼(通常為==HTML==),擷取HTML內特定的文字。 ---- ### pyquery [![](https://i.imgur.com/khx6BDh.png)](https://pythonhosted.org/pyquery/) ---- ### pyquery 一套包含==取得網頁原始碼==與==內容擷取規則==Python模組。 ---- #### 網頁內容擷取規則 Pyquery採用[CSS選擇器](https://zh.wikipedia.org/zh-tw/CSS_%E6%BF%BE%E5%99%A8)作為網頁內容的篩選擷取規則,這是一種用於網頁設計師描述內容樣式時選擇規則。 ---- ### 引用pyquey至專案內 ```python= from pyquery import PyQuery ``` ---- ### 如果出現ModuleNotFoundError 代表我們的電腦環境尚未安裝過pyquery模組 ---- 解決方法,透過指令安裝pyquery ```shell= !pip install pyquery ``` ---- ### 取得網頁全部原始碼 ```python= html = PyQuery('目標網址') print(html) # 印出網頁全部原始碼 ``` ---- 網頁原始碼的內容大部分都不是我們要的,所以我們必須透過==CSS選擇器的擷取規則==來取得特定的資訊。 ---- ### HTML的基本組成 網頁的內容由許多的==HTML標籤==所組成,每個標籤有代表著不同的內容格式 ---- 標籤可能帶有多個屬性,這些資訊都是==重要截取條件==喔 ```htmlmixed= <標籤名稱 屬性1="值" 屬性2="值"> 內容 </標籤名稱> ``` ---- ### 擷取規則 標籤名稱: `標籤名稱` class: `.class名稱` id: `#ID名稱` 其他屬性: `[屬性名稱=值]` ---- ```python= from pyquery import PyQuery as pq html = ''' <h1 class="title">嗨我是目標文字</h1> <h1>嗨我是一個普通的文字</h1> ''' selector = pq(html) print(selector('h1.title')) ``` ---- ```python= from pyquery import PyQuery as pq html = ''' <h1 id="title1">嗨我是目標文字</h1> <h1>嗨我是一個普通的文字</h1> ''' selector = pq(html) print(selector('h1#title1')) ``` ---- ```python= from pyquery import PyQuery as pq html = ''' <h1 class="title" data-title="important">嗨我是目標文字</h1> <h1 class="title">嗨我是一個普通的文字</h1> ''' selector = pq(html) print(selector('h1.title[data-title="important"]')) ``` ---- ### 擷取規則 標籤名稱: `標籤名稱` class: `.class名稱` id: `#ID名稱` 其他屬性: `[屬性名稱=值]` ---- ### 擷取規則 標籤名稱: `h1` class: `.title` id: `#title1` 其他屬性: `[data-title=important]` ---- ### 課堂實作練習 爬爬看==臺灣銀行牌告匯率== https://rate.bot.com.tw/xrt?Lang=zh-TW ---- ## 整合檔案管理(1) 試著寫入一個txt檔案紀錄爬取的內容 ```python= with open('example.txt', 'w') as f: f.write('第一行\n') f.write('第二行\n') f.write('第三行\n') ``` ---- ## 整合檔案管理(2) 寫入xlsx檔案,首先需要安裝xlsxwriter ``` pip install xlsxwriter ``` ---- ## 引用Xlswriter ```python= import xlsxwriter ``` ---- ## 建立工作表 ```python= workbook = xlsxwriter.Workbook('檔案名稱.xlsx') # 建立一個工作表 worksheet = workbook.add_worksheet() ``` ---- ## 寫入格子 ```python= # 寫入資料至格子內 # worksheet.write(row, column, "要新增的資料") worksheet.write(0, 0, "0.0") # 第一行第一格 worksheet.write(0, 1, "0.1") # 第一行第二格 # 關閉並保存檔案 workbook.close() ``` ---- ### 隨堂練習 請結合剛才透過爬蟲擷取的資料透過Python寫一個Xlsx檔案 |貨幣名稱|現金買入|現金賣出| |-|-|-| |美金|30.91|30.21| |港幣|3.120|3.27| ---- #### 爬蟲目標列表參考(1) 104工作職缺 https://www.104.com.tw/jobs/search/?ro=0&order=11&asc=0&zone=16&page=1&mode=s&jobsource=2018indexpoc ---- #### 爬蟲目標列表參考(2) 貴金屬交易中心 https://www.truney.com/product-category/silver/silver-coins/ ---- #### 爬蟲目標列表參考(3) 科技新報 https://technews.tw/ ---- #### 爬蟲目標列表參考(4) 數位時代 https://www.bnext.com.tw/ ---- #### 期末成果展示(每人5分鐘內) 1. 應用程式運作展示(亦可使用螢幕錄影) 2. 製作過程中遇到的困難?未來想加入的新功能?

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully