速率限制与礼貌抓取
学习如何限制请求速率、遵守 crawl-delay,并避免服务器过载,让您的抓取行为合乎伦理且可持续。
速率限制与礼貌抓取 是 CoddyKit 上的免费 Web Scraping & Bots 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 requestsRandomized 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 secondsConcurrency 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.
常见问题解答
「速率限制与礼貌抓取」课时是免费的吗?
是的 — 「速率限制与礼貌抓取」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web Scraping & Bots 课程的其余内容,请升级到 CoddyKit PRO。 Web Scraping & Bots 课程共包含 4 节课。
「速率限制与礼貌抓取」这节课中我会学到什么?
学习如何限制请求速率、遵守 crawl-delay,并避免服务器过载,让您的抓取行为合乎伦理且可持续。 你通过在浏览器中直接运行的动手代码来练习 Web Scraping & Bots,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Web Scraping & Bots 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Web Scraping & Bots 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「速率限制与礼貌抓取」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Web Scraping & Bots 课中编写并运行代码吗?
能。每节 Web Scraping & Bots 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 了解 Robots.txt
- 服务条款与版权
- 合乎道德的网络抓取实践
- 速率限制与礼貌抓取