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

การบันทึกเหตุการณ์และการจัดการข้อผิดพลาดสำหรับบอต

สร้างบอตอย่างง่ายที่ทนทานด้วยการเพิ่มการบันทึกเหตุการณ์แบบมีโครงสร้างและการจัดการข้อผิดพลาดอย่างราบรื่น เพื่อให้มองเห็นและกู้คืนจากความล้มเหลวได้

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

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

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

Why Bots Need Logs

A bot runs unattended, often on a schedule. When something breaks, you are not watching. Logging leaves a trail so you can diagnose what happened after the fact.

Without logs, a silent failure can go unnoticed for days.

print() Is Not Logging

Beginners use print(), but it has no levels, no timestamps, and no easy way to redirect to a file. Python's logging module solves all of this.

print('something happened')  # no level, no time, hard to manage

Setting Up Logging

Configure logging once at startup. Set a format with a timestamp and a level, and choose where output goes.

import logging

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s %(levelname)s %(message)s'
)
log = logging.getLogger('bot')
log.info('Bot started')

Log Levels

Levels let you control verbosity:

  • DEBUG detailed tracing.
  • INFO normal progress.
  • WARNING something odd but recoverable.
  • ERROR an operation failed.
  • CRITICAL the bot cannot continue.
log.debug('fetched 12 rows')
log.warning('retrying after timeout')
log.error('login failed')

Logging to a File

For unattended bots, write logs to a file so you can review them later. A rotating handler caps file size.

from logging.handlers import RotatingFileHandler

handler = RotatingFileHandler('bot.log', maxBytes=1_000_000, backupCount=3)
log.addHandler(handler)

Catching Exceptions

Wrap risky operations in try/except so one failure does not crash the whole run. Log the error with context.

try:
    submit_form(data)
except Exception as e:
    log.error('form submission failed: %s', e)

Logging Full Tracebacks

Use log.exception inside an except block to capture the full stack trace, which is invaluable for debugging.

try:
    risky()
except Exception:
    log.exception('unexpected error in risky()')

Retrying Transient Failures

Network blips are common. Catch them and retry a few times before giving up, logging each attempt.

for attempt in range(3):
    try:
        fetch()
        break
    except ConnectionError:
        log.warning('attempt %d failed, retrying', attempt + 1)
else:
    log.error('all attempts failed')

Failing Loudly

Silent failures are dangerous. For critical errors, escalate: send an alert email, write a marker file, or exit with a non-zero code so a scheduler notices.

import sys
if not data:
    log.critical('no data scraped, aborting')
    sys.exit(1)

Clean Shutdown

Always release resources even on error. A finally block (or context managers) guarantees browsers close and files flush regardless of what went wrong.

try:
    run_bot()
finally:
    driver.quit()
    log.info('bot finished')

Structured Logging Context

Attach context to each log line, such as the current URL or item id, so when you scan a long log you immediately know which record a message refers to.

log.info('scraped item %s from %s', item_id, url)

Quick Check

Test your understanding of bot logging and error handling.

Recap

You learned to make simple bots resilient: use the logging module instead of print, choose appropriate levels, log to rotating files, catch and log exceptions with tracebacks, retry transient errors, fail loudly on critical issues, and shut down cleanly.

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

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

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

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

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

บทเรียน “การบันทึกเหตุการณ์และการจัดการข้อผิดพลาดสำหรับบอต” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การบันทึกเหตุการณ์และการจัดการข้อผิดพลาดสำหรับบอต”

สร้างบอตอย่างง่ายที่ทนทานด้วยการเพิ่มการบันทึกเหตุการณ์แบบมีโครงสร้างและการจัดการข้อผิดพลาดอย่างราบรื่น เพื่อให้มองเห็นและกู้คืนจากความล้มเหลวได้ คุณปฏิบัติ 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. การกำหนดวัตถุประสงค์ของบอต
  2. การทำให้การส่งแบบฟอร์มอย่างง่ายเป็นอัตโนมัติ
  3. การกำหนดเวลางานพื้นฐาน
  4. การบันทึกเหตุการณ์และการจัดการข้อผิดพลาดสำหรับบอต
← กลับไปที่ Web Scraping & Bots