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

การจัดการเซสชันและคุกกี้

เรียนรู้ว่าบอตรักษาสถานะข้ามคำขอด้วยเซสชัน คุกกี้ และโทเค็น เพื่อรองรับเวิร์กโฟลว์ที่ยืนยันตัวตนและมีหลายขั้นตอนได้อย่างไร

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

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

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

HTTP Is Stateless

Each HTTP request is independent: the server forgets you between calls. To build multi-step workflows, a bot must carry state itself.

Cookies and sessions are how that state travels between requests.

What a Cookie Is

A cookie is a small key-value pair the server sets via a Set-Cookie header. The client returns it on every subsequent request, letting the server recognize the same client.

Set-Cookie: session_id=abc123; Path=/; HttpOnly

Session Objects

The requests.Session object automatically stores and resends cookies across requests, so a login persists for the rest of your workflow.

import requests

session = requests.Session()
session.post('https://site.com/login', data={'user': 'a', 'pass': 'b'})
# cookies now persist on subsequent calls
profile = session.get('https://site.com/profile')

Inspecting Cookies

You can read what cookies a session holds. This helps debug why an authenticated request unexpectedly redirects to a login page.

for c in session.cookies:
    print(c.name, '=', c.value)

Setting Cookies Manually

Sometimes you obtain a cookie elsewhere (a browser export) and inject it into your session to skip the login step.

session.cookies.set('session_id', 'abc123', domain='site.com')

CSRF Tokens

Forms often embed a hidden CSRF token that must be sent back on submit. Scrape it from the page first, then include it in your POST.

page = session.get('https://site.com/form')
token = extract_hidden(page.text, 'csrf_token')
session.post('https://site.com/form', data={'csrf_token': token, 'msg': 'hi'})

Bearer Tokens

API-driven sites issue a token after login that you send in an Authorization header rather than as a cookie. Store it and attach it to every call.

token = login_resp.json()['access_token']
session.headers.update({'Authorization': 'Bearer ' + token})

Persisting Sessions to Disk

To resume a workflow later without re-authenticating, serialize the cookie jar and reload it on the next run.

import pickle

with open('cookies.pkl', 'wb') as f:
    pickle.dump(session.cookies, f)

# later
with open('cookies.pkl', 'rb') as f:
    session.cookies.update(pickle.load(f))

Handling Expiry

Sessions and tokens expire. Detect a redirect to login or a 401 response, then re-authenticate transparently before retrying the original request.

resp = session.get(url)
if resp.status_code == 401:
    relogin(session)
    resp = session.get(url)

Sessions in Selenium

In a browser-driven bot, Selenium manages cookies for you. You can still read or transplant them between a requests.Session and the browser to share authentication.

for c in driver.get_cookies():
    session.cookies.set(c['name'], c['value'])

Security of Stored Sessions

A saved cookie jar is as sensitive as a password: anyone with it can act as you. Store session files with restricted permissions and never commit them to version control.

import os
os.chmod('cookies.pkl', 0o600)  # owner read/write only

Quick Check

Test your understanding of session management.

Recap

You learned how bots maintain state: cookies, requests.Session, CSRF and bearer tokens, persisting cookies to disk, handling expiry with re-login, and sharing auth between Selenium and requests.

Managing sessions is essential for any authenticated multi-step workflow.

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

เรียนรู้ 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. การผสานรวมกับ API
  4. การจัดการเซสชันและคุกกี้
← กลับไปที่ Web Scraping & Bots