Web Scraping & Bots · درس

تحديد معدّل الطلبات والزحف باحترام

تعلّم كيفية خنق الطلبات، واحترام crawl-delay، وتجنب إرهاق الخوادم، حتى يظل الكشط أخلاقيًا ومستدامًا.

الدرس 4 من 413 خطوة

تحديد معدّل الطلبات والزحف باحترام درس مجاني في Web Scraping & Bots على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «تحديد معدّل الطلبات والزحف باحترام» مجاني؟

نعم — نص درس «تحديد معدّل الطلبات والزحف باحترام» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Web Scraping & Bots، انتقل إلى CoddyKit PRO. تتضمن دورة Web Scraping & Bots 4 دروس في المجموع.

ماذا ستتعلم في «تحديد معدّل الطلبات والزحف باحترام»؟

تعلّم كيفية خنق الطلبات، واحترام crawl-delay، وتجنب إرهاق الخوادم، حتى يظل الكشط أخلاقيًا ومستدامًا. تتمرن على Web Scraping & Bots مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Web Scraping & Bots؟

لا تُشترط خبرة سابقة. Web Scraping & Bots على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «تحديد معدّل الطلبات والزحف باحترام»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Web Scraping & Bots هذا؟

نعم. كل درس في Web Scraping & Bots يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. فهم Robots.txt
  2. شروط الخدمة وحقوق الطبع والنشر
  3. ممارسات كشط الويب الأخلاقية
  4. تحديد معدّل الطلبات والزحف باحترام
← العودة إلى Web Scraping & Bots