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

แนวปฏิบัติด้านจริยธรรมสำหรับการดึงข้อมูล

นำแนวทางที่ดีที่สุดมาใช้ เช่น การจำกัดอัตราการส่งคำขอ การระบุ user-agent อย่างเหมาะสม และการคำนึงถึงภาระของเซิร์ฟเวอร์ เพื่อดึงข้อมูลอย่างมีความรับผิดชอบ

บทเรียน 3 จาก 411 ขั้นตอน

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

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

Why Scrape Ethically?

Welcome to Ethical Scraping Practices! Web scraping is a powerful tool, but it comes with responsibilities.

Being an ethical scraper means more than just avoiding legal trouble. It's about being a good internet citizen, respecting website resources, and ensuring the sustainability of your scraping efforts.

Respect Server Load

Imagine thousands of requests hitting a website at once. This can overwhelm the server, slow down the site for other users, or even crash it. This is similar to a Denial-of-Service (DoS) attack.

An ethical scraper avoids putting undue strain on a website's infrastructure. We want to collect data, not cause problems!

Implement Rate Limiting

The best way to respect server load is through rate limiting. This means introducing delays between your requests to a website.

By waiting a few seconds between each page fetch, you give the server time to process your request and serve other users, mimicking human browsing behavior.

Rate Limiting Example

Here's a simple Python example using time.sleep() to introduce a delay between requests. Try running it!

import requests
import time

def fetch_url_with_delay(url, delay_seconds):
  print(f"Fetching {url}...")
  try:
    response = requests.get(url)
    print(f"Status: {response.status_code}")
  except requests.exceptions.RequestException as e:
    print(f"Error fetching {url}: {e}")
  time.sleep(delay_seconds) # Wait before next request

if __name__ == "__main__":
  target_url = "https://httpbin.org/get" # A safe test URL
  print("Starting requests with delays...")
  for i in range(2):
    fetch_url_with_delay(target_url, 3) # Wait 3 seconds
  print("Finished scraping with delays.")

Identify Yourself (Politely!)

When your browser makes a request, it sends a User-Agent header. This header tells the server information about the client, like the browser type (e.g., Chrome, Firefox) and operating system.

As an ethical scraper, you should set a custom, descriptive User-Agent. Include your bot's name and contact information so website administrators can reach you if there are issues.

Custom User-Agent

Setting a custom User-Agent is straightforward with the requests library. Here’s how you can do it:

import requests

def fetch_with_custom_ua(url):
  headers = {
    "User-Agent": "CoddyKitScraper/1.0 (contact@example.com)",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
  }
  print(f"Fetching {url} with custom User-Agent...")
  try:
    response = requests.get(url, headers=headers)
    print(f"Status: {response.status_code}")
    print(f"User-Agent sent: {response.request.headers['User-Agent']}")
  except requests.exceptions.RequestException as e:
    print(f"Error fetching {url}: {e}")

if __name__ == "__main__":
  target_url = "https://httpbin.org/get" # A safe test URL
  fetch_with_custom_ua(target_url)
  print("Finished request with custom User-Agent.")

Check robots.txt (Again!)

Even if you're rate limiting and using a proper User-Agent, always remember to check a website's robots.txt file.

This file is a standard way for websites to communicate their scraping policies, telling you which parts of the site they prefer you don't access. Respecting it is a cornerstone of ethical scraping.

Handle Data Responsibly

Ethical scraping extends beyond just the act of collecting data; it also covers what you do with it afterward. Consider these points:

  • Privacy: Avoid collecting personally identifiable information (PII) without explicit consent.
  • Anonymization: Anonymize data where possible to protect individuals.
  • Compliance: Adhere to data privacy regulations like GDPR or CCPA.
  • Misuse: Do not misrepresent, resell, or exploit scraped data in ways that harm individuals or businesses.

Key Ethical Practices

To summarize, here are the core ethical practices for web scraping:

  • Respect robots.txt: Always check and follow its directives.
  • Rate Limit Your Requests: Introduce delays to avoid overwhelming servers.
  • Use a Descriptive User-Agent: Identify your bot with contact information.
  • Handle Data Responsibly: Prioritize privacy and legal compliance.
  • Monitor Server Load: Be aware of your impact and adjust if necessary.

Ethical Scraper Quiz

Test your understanding of ethical scraping practices.

Recap & Next Steps

You've learned that ethical scraping is crucial for responsible data collection. This involves respecting server load through rate limiting, clearly identifying your bot with a proper User-Agent, and handling collected data responsibly.

Always strive to be a good internet citizen! In the next lessons, we'll explore more advanced topics like data storage and building your first bot.

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

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

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

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

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

บทเรียน “แนวปฏิบัติด้านจริยธรรมสำหรับการดึงข้อมูล” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “แนวปฏิบัติด้านจริยธรรมสำหรับการดึงข้อมูล”

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

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

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

บทเรียน “แนวปฏิบัติด้านจริยธรรมสำหรับการดึงข้อมูล” ใช้เวลานานแค่ไหน

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

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

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

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

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