iT邦幫忙

2025 iThome 鐵人賽

DAY 24
0

明天大概會試著使用所謂的網路爬蟲來幫我們找出需要的資訊,所以今天來介紹網路爬蟲是什麼東西
想像一下,您想收集網路上成千上萬筆公開資訊,例如商品價格、新聞標題。

如果一筆筆手動複製貼上,不僅耗時又容易出錯。這時網路爬蟲就能派上用場,網路爬蟲是一種自動化程式,它能像瀏覽器一樣訪問網頁,並從中抓取您想要的特定資料。Python 因為擁有強大且易於上手的函式庫,所以現在如果想要開發爬蟲的話,它就是我們的首選語言。
首先程式會模擬瀏覽器,向目標網站的伺服器發送一個請求,告訴它:「我想看看這個頁面」。伺服器收到請求後,會回傳包含網頁原始碼(通常是 HTML 格式)的回應。接著程式會分析 HTML 檔案,從中挑出我們需要的資料,例如文章標題、圖片連結、表格數據,在我們在程式中對著特定部分進行篩選後出現的就是我們要的資料啦。

但是這邊要注意一下,很多的網站都會有他們對爬蟲及機器人的規範,基本上都會放在網址的根目錄並且叫做robot.txt,這邊舉個例子

https://shopee.tw/robots.txt

這邊是蝦皮對於爬蟲的規範

User-Agent:Googlebot           # 這組規則適用於 Google 的桌面版搜尋爬蟲
User-Agent:Googlebot-Mobile    # 適用於 Google 的行動版爬蟲
User-Agent:Bingbot             # 適用於 Bing 的搜尋爬蟲
Crawl-delay:0.1                # 希望每 0.1 秒爬一次(大多搜尋引擎會忽略)
Disallow: /cart/               # 禁止爬取購物車頁面
Disallow: /checkout/           # 禁止爬取結帳頁面
Disallow: /buyer/login/otp     # 禁止爬取登入驗證頁面
Disallow: /user/               # 禁止爬取使用者個人頁
Disallow: /me/                 # 禁止爬取個人專屬頁(例如「我的帳號」)
Disallow: /order/              # 禁止爬取訂單相關頁面
Disallow: /daily_discover/     # 禁止爬取每日推薦內容
Disallow: /mall/just-for-you/  # 禁止爬取個人化「為你推薦」頁
Disallow: /mall/*-cat.         # 禁止爬取含 -cat. 的 mall 子頁(分類頁)
Disallow: /from_same_shop/     # 禁止爬取同店推薦商品頁
Disallow: /you_may_also_like/  # 禁止爬取「你可能也喜歡」推薦頁
Disallow: *-i.*/similar?from=flash_sale  # 禁止爬取閃購區的相似商品頁
Disallow: /find_similar_products/        # 禁止爬取找相似商品功能頁
Disallow: /top_products         # 禁止爬取熱門商品清單頁
Disallow: /search*searchPrefill # 禁止爬取帶 searchPrefill 參數的搜尋頁
Disallow: /index.html           # 禁止爬取 index.html(避免與 / 重複內容)
Disallow: /search*hashtag=      # 禁止爬取含 hashtag 查詢參數的搜尋頁
Disallow: /search?shop=         # 禁止爬取含 shop= 的搜尋頁
Disallow: /buyer/*/rating/      # 禁止爬取買家評價頁(避免個資或重複內容)
Disallow: *-cat.*?category=     # 禁止爬取含 -cat. 且帶 category 參數的頁面
Disallow: /shop/*/search        # 禁止爬取商店內搜尋頁
Disallow: /search/?             # 禁止爬取 /search/ 帶 query 的頁面
Disallow: /search?brands=       # 禁止爬取含 brands= 的搜尋頁
Disallow: /search?originalCategoryId=    # 禁止爬取含 originalCategoryId 的搜尋頁
Disallow: /addon-deal-selection/ # 禁止爬取加購優惠選擇頁
Disallow: /bundle-deal/          # 禁止爬取組合優惠頁
Disallow: ?sp_atk=               # 禁止爬取帶 sp_atk 查詢參數的 URL
Disallow: /search_user           # 禁止爬取搜尋使用者頁面
Disallow: /search?keyword=*.com  # 禁止爬取搜尋關鍵字為 *.com 的頁面
Disallow: /blog/?s=              # 禁止爬取部落格搜尋結果頁
Disallow: *utm_source            # 禁止爬取含 utm_source 追蹤參數的頁面
Disallow: /*?*srsltid            # 禁止爬取帶 srsltid 參數的 URL(搜尋追蹤參數

User-Agent:*                    # 適用於所有未明確指定的爬蟲
Crawl-delay:1                   # 建議每次請求間隔 1 秒
Disallow: /cart/                # 禁止爬取購物車頁面
Disallow: /checkout/            # 禁止爬取結帳頁面
Disallow: /buyer/login/otp      # 禁止爬取登入驗證頁面
Disallow: /user/                # 禁止爬取使用者個人頁
Disallow: /me/                  # 禁止爬取個人專屬頁
Disallow: /order/               # 禁止爬取訂單頁面
Disallow: /daily_discover/      # 禁止爬取每日推薦內容
Disallow: /mall/just-for-you/   # 禁止爬取個人化推薦頁
Disallow: /mall/*-cat.          # 禁止爬取含 -cat. 的 mall 分類頁
Disallow: /from_same_shop/      # 禁止爬取同店推薦頁
Disallow: /you_may_also_like/   # 禁止爬取推薦頁
Disallow: *-i.*/similar         # 禁止爬取所有相似商品頁
Disallow: /find_similar_products/ # 禁止爬取找相似商品頁
Disallow: /top_products         # 禁止爬取熱門商品頁
Disallow: /search*searchPrefill # 禁止爬取帶 searchPrefill 參數的搜尋頁
Disallow: /index.html           # 禁止爬取 index.html
Disallow: /blog/?s=             # 禁止爬取部落格搜尋結果頁

千萬注意不要違反這些要求,否則可能有法律問題


上一篇
d23
下一篇
d25
系列文
這是一個一個一個 Python API 與 Gemini 整合、n8n入門指南28
  1. 24
    d24
  2. 25
    d25
  3. 26
    d26
  4. 27
    d27
  5. 28
    d28
完整目錄
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言