iT邦幫忙

2021 iThome 鐵人賽

DAY 14
0
AI & Data

網路爬蟲,萬物皆爬 - 30 天搞懂並實戰網路爬蟲及應對反爬蟲技術系列 第 14

【Day 14】- 實戰爬取 Ubuntu ISO 映像檔下載網址

前情提要

前三篇文章帶各位開發了一隻 PTT 爬蟲,具備持續爬取,並將爬取到的文隻內容儲存於 JSON 檔案中。

開始之前

本篇將帶各位寫 ISO 映像檔下載連結爬蟲。

預期效果

爬取位於 http://ftp.ubuntu-tw.org/ubuntu-releases/ 的特定版本下載連結,並將其存放於 JSON 檔中。

實作

首先,我們先來觀察一下該網站,發現如果要下載某一個版本的映像檔,只要在網址後後方加入該版本即可。

e.g. 要進入 21.04/ 為 http://ftp.ubuntu-tw.org/ubuntu-releases/21.04/

(此為理所當然,因為此為檔案列表網站)

本篇將取得 21.04/ 20.10/ 20.04/ 18.04/ 16.04/ 14.04/ 內的 ubuntu-{版本}-desktop-amd64.iso ubuntu-{版本}-live-server-amd64.iso 的下載連結。

我們先用個串列將欲爬取的版本及用個變數將預設網址儲存。

version_list = ['21.04/', '20.10/', '20.04/', '18.04/', '16.04/', '14.04/']
url = 'http://ftp.ubuntu-tw.org/ubuntu-releases/'

接下來用 for-loop 將 version_list 內的版本遍歷一遍,並分別用 requests.get 發送請求,能夠成功取得下載連結。

import requests

version_list = ['21.04/', '20.10/', '20.04/', '18.04/', '16.04/', '14.04/']
url = 'http://ftp.ubuntu-tw.org/ubuntu-releases/'

for version in version_list:
    r = requests.get(url+version)
    print(r.text)

之後,我們能使用 BeautifulSoup 去解析該頁面。再來利用 BeautifulSoup 上正規表達式的模糊搜尋去找到目標元素

import requests
import re
from bs4 import BeautifulSoup

version_list = ['21.04/', '20.10/', '20.04/', '18.04/', '16.04/', '14.04/']
url = 'http://ftp.ubuntu-tw.org/ubuntu-releases/'

for version in version_list:
    r = requests.get(url+version)
    soup = BeautifulSoup(r.text, 'html5lib')
    desktop_iso = soup.find('a', string=re.compile(
        'ubuntu-\d{2}\.\d{2}\.?\d{0,2}-desktop-amd64\.iso'))['href']
    server_iso = soup.find('a', string=re.compile(
        'ubuntu-\d{2}\.\d{2}\.?\d{0,2}(-live)?-server-amd64\.iso'))['href']
    print(desktop_iso)
    print(server_iso)

最後,我們能透過昨天的技巧來將下載網址到以版本為 key 存到字典中,並將字典存於 JSON 檔案中。

import requests
import re
import json
from bs4 import BeautifulSoup

version_list = ['21.04/', '20.10/', '20.04/', '18.04/', '16.04/', '14.04/']
url = 'http://ftp.ubuntu-tw.org/ubuntu-releases/'
result_dict = {}

for version in version_list:
    r = requests.get(url+version)
    soup = BeautifulSoup(r.text, 'html5lib')
    desktop_iso = soup.find('a', string=re.compile(
        'ubuntu-\d{2}\.\d{2}\.?\d{0,2}-desktop-amd64\.iso'))['href']
    server_iso = soup.find('a', string=re.compile(
        'ubuntu-\d{2}\.\d{2}\.?\d{0,2}(-live)?-server-amd64\.iso'))['href']
    result_dict[version] = {
        "desktop_iso": r.url + desktop_iso,
        "server_iso": r.url + server_iso
    }

with open('iso-image.json', 'w', encoding='utf-8') as f:
    json.dump(result_dict, f, indent=2,
              sort_keys=True, ensure_ascii=False)

結語

今天實作了爬取 ISO 映像,有了爬取之後存下來的 JSON 檔,便可用一些方式去將 ISO 映像備份了,如 wget 等方式。

明日內容

明天會帶各位爬取 google 上的幣種匯率。

補充資料

Ubuntu release version : http://ftp.ubuntu-tw.org/ubuntu-releases/


上一篇
【Day 13】- 用 JSON 儲存爬來的 PTT 文章。(實戰 PTT 爬蟲 3/3)
下一篇
【Day 15】- 匯率什麼的。爬! (實戰匯率爬蟲 on chrome)
系列文
網路爬蟲,萬物皆爬 - 30 天搞懂並實戰網路爬蟲及應對反爬蟲技術30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
arguskao
iT邦新手 4 級 ‧ 2022-05-16 15:54:19

請問
string=re.compile(
'ubuntu-\d{2}.\d{2}.?\d{0,2}-desktop-amd64.iso'))['href']

re.compile()這是?

Vincent55 iT邦新手 4 級 ‧ 2022-05-26 23:42:14 檢舉

https://ithelp.ithome.com.tw/articles/10268012

這篇的範例中有提到會編譯 Pattern 為 re.Pattern 這個物件

我要留言

立即登入留言