Web Scraping & Bots · บทเรียน

การจำกัดอัตราและการรวบรวมข้อมูลอย่างเคารพ

เรียนรู้วิธีควบคุมความถี่คำขอ ปฏิบัติตาม crawl-delay และหลีกเลี่ยงการทำให้เซิร์ฟเวอร์รับภาระเกิน เพื่อให้การขูดเว็บของคุณมีจริยธรรมและยั่งยืน

บทเรียน 4 จาก 413 ขั้นตอน

การจำกัดอัตราและการรวบรวมข้อมูลอย่างเคารพ เป็นบทเรียน Web Scraping & Bots ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Scraping & Bots และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Rate Limiting Matters

Even when scraping is legally permitted, hammering a server with rapid requests can degrade the site for real users and get your IP banned.

Respectful crawling means pacing your requests so you gather data without harming the host.

The Cost of a Request

Every request consumes server CPU, bandwidth, and database load. A scraper firing hundreds of requests per second behaves like a denial-of-service attack, even unintentionally.

  • Small sites have limited capacity.
  • Bursts spike server load.
  • Steady, slow traffic is far kinder.

Reading Crawl-Delay

Many robots.txt files include a Crawl-delay directive specifying seconds to wait between requests. Honor it as a baseline minimum.

User-agent: *
Crawl-delay: 10
Disallow: /private/

A Simple Delay

The most basic throttle adds a pause between requests. A fixed delay is fine for low-volume jobs.

import time
import requests

for url in urls:
    requests.get(url)
    time.sleep(2)  # at least 2 seconds between requests

Randomized Delays

Fixed intervals create a robotic, easily-detected pattern. Adding jitter makes traffic look more human and spreads load.

import random, time

delay = random.uniform(1.5, 4.0)
time.sleep(delay)

Token Bucket Throttling

For steadier control, a token bucket permits a fixed average rate while allowing small bursts. Tokens refill over time; each request spends one.

import time

class RateLimiter:
    def __init__(self, rate):
        self.rate = rate
        self.last = time.time()
    def wait(self):
        now = time.time()
        gap = 1.0 / self.rate
        sleep = gap - (now - self.last)
        if sleep > 0:
            time.sleep(sleep)
        self.last = time.time()

Respecting HTTP 429

A 429 Too Many Requests response is the server telling you to slow down. Check for a Retry-After header and back off for that duration.

resp = requests.get(url)
if resp.status_code == 429:
    wait = int(resp.headers.get('Retry-After', 30))
    time.sleep(wait)

Exponential Backoff

On repeated errors, increase the wait exponentially rather than retrying immediately. This relieves a struggling server and improves your success rate.

delay = 1
for attempt in range(5):
    resp = requests.get(url)
    if resp.ok:
        break
    time.sleep(delay)
    delay *= 2  # 1, 2, 4, 8, 16 seconds

Concurrency Limits

Parallel requests speed things up but multiply server load. Cap simultaneous connections with a semaphore so you never exceed a polite ceiling.

import threading
sem = threading.Semaphore(4)  # max 4 in flight

def fetch(url):
    with sem:
        requests.get(url)

Scheduling Off-Peak

Run heavy jobs during the site's low-traffic hours (often overnight in the host's timezone). Combined with throttling, this minimizes your footprint and reduces the chance of disruption.

Caching to Avoid Re-Fetching

The kindest request is the one you never send. Cache responses locally so re-running a job does not re-hit pages you already have. Only fetch what is new or stale.

import os
if not os.path.exists(cache_path):
    resp = requests.get(url)
    open(cache_path, 'w').write(resp.text)
html = open(cache_path).read()

Quick Check

Test your understanding of respectful crawling.

Recap

You learned to crawl respectfully: honor Crawl-delay, add randomized delays, use token buckets, react to 429 with backoff, cap concurrency, and schedule off-peak.

Polite pacing keeps your scraper sustainable and your access intact.

เริ่มต้นได้ฟรี

เรียนรู้ Python ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การจำกัดอัตราและการรวบรวมข้อมูลอย่างเคารพ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจำกัดอัตราและการรวบรวมข้อมูลอย่างเคารพ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Scraping & Bots ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจำกัดอัตราและการรวบรวมข้อมูลอย่างเคารพ”

เรียนรู้วิธีควบคุมความถี่คำขอ ปฏิบัติตาม crawl-delay และหลีกเลี่ยงการทำให้เซิร์ฟเวอร์รับภาระเกิน เพื่อให้การขูดเว็บของคุณมีจริยธรรมและยั่งยืน คุณปฏิบัติ Web Scraping & Bots ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Scraping & Bots หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Scraping & Bots บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การจำกัดอัตราและการรวบรวมข้อมูลอย่างเคารพ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Web Scraping & Bots นี้ได้ไหม

ได้ บทเรียน Web Scraping & Bots ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ทำความเข้าใจ Robots.txt
  2. ข้อกำหนดการให้บริการและลิขสิทธิ์
  3. แนวปฏิบัติด้านจริยธรรมสำหรับการดึงข้อมูล
  4. การจำกัดอัตราและการรวบรวมข้อมูลอย่างเคารพ
← กลับไปที่ Web Scraping & Bots