0Pricing
Web Scraping & Bots · درس

التعامل مع مصادقة المستخدم

ابنِ روبوتات يمكنها تسجيل الدخول إلى مواقع الويب باستخدام بيانات الاعتماد وإدارة الجلسات والتعامل مع ملفات تعريف الارتباط

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

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Bots Need to Log In

Many websites require you to log in to access certain data or features. Bots often need to simulate this process to get past paywalls, access dashboards, or retrieve personalized content.

  • Access restricted content.
  • Perform user-specific actions.
  • Maintain a persistent identity.

Understanding Login Forms

When you log in manually, your browser sends your username and password to the server. This is usually done via an HTTP POST request to a specific URL.

  • POST request: Sends data securely in the request body.
  • Form fields: Input fields for credentials (e.g., username, password).
  • Action URL: The server endpoint that processes the login.

Finding Login Form Details

To automate a login, you need to identify the form's action URL and the name attributes of the input fields (username, password). Browser developer tools are essential here.

  • Right-click on the login form → "Inspect".
  • Look for the <form> tag: find its action attribute.
  • Look for <input> tags: find their name attributes for credentials.

Sending Login Data with Requests

The Python requests library can send POST requests. You'll prepare a dictionary of your credentials and pass it to requests.post().

  • The dictionary keys should match the input field name attributes.
  • The values will be your username and password.
  • The url will be the form's action URL.

Basic Login Attempt

Here's a simple example of trying to log in. Note that without session management, you'll likely be logged out immediately after the request, as no cookies are persisted.

import requests

login_url = "http://httpbin.org/post" # Example URL
credentials = {
    "username": "my_user",
    "password": "my_password"
}

response = requests.post(login_url, data=credentials)

print(f"Status Code: {response.status_code}")
print("Response data (simulated):")
print(response.json().get('form'))

Sessions & Cookies for Persistence

After a successful login, websites typically send back a cookie. This small piece of data is stored by your browser and sent with subsequent requests, telling the server you're still logged in.

  • Session: A continuous interaction between a user and a website.
  • Cookies: Small text files used by websites to remember information about you.
  • Crucial for maintaining a logged-in state across multiple page visits.

Managing Sessions with `requests.Session()`

The requests library has a Session object that automatically handles cookies for you. Once you log in using a Session object, it will include the authentication cookies in all subsequent requests made through that same session.

  • Create a session object: s = requests.Session().
  • Use s.post() for login and s.get() for subsequent requests.
  • No need to manually manage cookies.

Login with `requests.Session()`

This example demonstrates how to use requests.Session() to maintain your logged-in state. After logging in, the session object automatically includes the cookies for the subsequent GET request.

import requests

login_url = "http://httpbin.org/post" # Example login endpoint
dashboard_url = "http://httpbin.org/get" # Example page after login

credentials = {
    "username": "my_user",
    "password": "my_password"
}

with requests.Session() as s:
    # First, post login data
    login_response = s.post(login_url, data=credentials)
    print(f"Login Status: {login_response.status_code}")
    print(f"Cookies after login: {s.cookies.get_dict()}")

    # Now, access a protected page using the same session
    protected_page_response = s.get(dashboard_url)
    print(f"Protected Page Status: {protected_page_response.status_code}")
    print("Protected page content snippet:")
    print(protected_page_response.text[:100])

Checking Login Success

How do you know if your bot actually logged in successfully? It's crucial to verify the outcome.

  • Status Code: A 200 OK often means success, but sometimes redirects happen (e.g., 302 Found).
  • Redirects: Check response.history to see if you were redirected to a dashboard or profile page.
  • Page Content: Look for specific text that only appears when logged in (e.g., "Welcome, [username]", a "Logout" button, or absence of login form).

Session Management Check

You're building a bot to log into a website and then navigate to a profile page. Which of the following are true about using requests.Session()?

Recap: User Authentication

In this lesson, we learned how to build bots that can log into websites to access protected content.

  • We covered inspecting login forms to find the action URL and input field names.
  • We used requests.post() to send credentials.
  • Most importantly, we learned to use requests.Session() to manage and persist cookies, allowing our bots to stay logged in and access authenticated content.
  • Finally, we discussed how to verify successful logins by checking status codes, redirects, and page content.

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

هل درس «التعامل مع مصادقة المستخدم» مجاني؟

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

ماذا ستتعلم في «التعامل مع مصادقة المستخدم»؟

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

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

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

كم من الوقت يستغرق درس «التعامل مع مصادقة المستخدم»؟

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

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

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

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

  1. التعامل مع مصادقة المستخدم
  2. محاكاة مسارات المستخدم المعقدة
  3. الدمج مع واجهات API
  4. إدارة الجلسات وملفات تعريف الارتباط
← العودة إلى Web Scraping & Bots