0Pricing
Web Scraping & Bots · 课时

自动提交简单表单

开发能够自动填写并提交网页表单的机器人,处理各种输入类型。

自动提交简单表单 是 CoddyKit 上的免费 Web Scraping & Bots 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web Scraping & Bots 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web Scraping & Bots 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What are Web Forms?

Web forms are how you interact with websites! Think of logging in, signing up, searching for products, or leaving comments.

They are collections of input fields, buttons, and other elements that let you send data to a web server. Automating them means your bot can fill and submit these forms for you.

Why Automate Forms?

Automating form submissions saves time and effort. Instead of manually typing information repeatedly, your bot can do it instantly.

  • Sign-ups: Create multiple accounts for testing.
  • Data Entry: Submit data to web applications.
  • Searches: Perform repetitive searches.

It's a core skill for building useful bots!

Identifying Form Inputs

Before submitting, your bot needs to know *what* to fill. We identify form elements using HTML attributes, especially the name attribute.

Common input types include text fields, password fields, checkboxes, radio buttons, and text areas. Each has a unique name that acts as its identifier.

The 'name' Attribute is Key

When you submit a form, the browser sends data as key-value pairs. The key is the name attribute of the input field, and the value is what you typed into it.

For example, an input field like <input type="text" name="username"> tells the server that the data for 'username' is being sent.

Preparing Your Data (Payload)

To submit a form with Python's requests library, you'll create a dictionary. This dictionary holds all the name-value pairs for the form fields you want to fill.

This dictionary is often called the 'payload' or 'form data'.

Simple Form Submission with POST

Most forms use the HTTP POST method to send data securely. The requests.post() function is perfect for this. You pass the target URL and your data payload.

Let's simulate submitting a simple form with a username and password (to a placeholder URL).

import requests

url = "https://httpbin.org/post" # A test URL that echoes POST data

# Prepare the data for the form fields
payload = {
    "username": "coddybot",
    "password": "secure_pass123"
}

# Send the POST request
response = requests.post(url, data=payload)

# Print the response from the server
print("Status Code:", response.status_code)
print("Response JSON:", response.json())

Understanding the Response

After submitting a form, the server sends a response. The response.status_code tells you if the submission was successful (e.g., 200 for OK, 404 for Not Found).

The response.json() or response.text contains the server's reply, which might confirm the submission or show errors.

In the example, httpbin.org/post returns the data it received, confirming our payload was sent.

Handling Checkboxes & Radios

Checkboxes and radio buttons work similarly. You include their name attribute as a key in your payload. For their value, you use the value attribute defined in the HTML.

  • Checkbox: Include the name and value if checked. Omit if not.
  • Radio Button: Include the name and the value of the selected option.

Practice Form Submission

Imagine a form with a checkbox <input type="checkbox" name="agree_terms" value="yes"> and a radio button group <input type="radio" name="color" value="blue">.

If you want to check 'agree_terms' and select 'blue', your payload would look like this:

import requests

url = "https://httpbin.org/post"

payload = {
    "agree_terms": "yes", # Checkbox value
    "color": "blue",      # Selected radio button value
    "message": "Hello Bot!"
}

response = requests.post(url, data=payload)

print("Status Code:", response.status_code)
print("Response JSON:", response.json())

Form Submission Check

Which HTML attribute is MOST crucial for identifying an input field when preparing data for a form submission?

Recap: Automating Forms

You've learned how to make your bot interact with web forms!

  • Identify form fields using the name attribute.
  • Prepare your data as a dictionary (payload).
  • Use requests.post() to send the data to the server.
  • Handle different input types like text fields, checkboxes, and radio buttons.

Next, we'll explore scheduling these tasks to run your bots automatically!

常见问题解答

「自动提交简单表单」课时是免费的吗?

是的 — 「自动提交简单表单」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web Scraping & Bots 课程的其余内容,请升级到 CoddyKit PRO。 Web Scraping & Bots 课程共包含 4 节课。

「自动提交简单表单」这节课中我会学到什么?

开发能够自动填写并提交网页表单的机器人,处理各种输入类型。 你通过在浏览器中直接运行的动手代码来练习 Web Scraping & Bots,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Web Scraping & Bots 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Web Scraping & Bots 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「自动提交简单表单」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Web Scraping & Bots 课中编写并运行代码吗?

能。每节 Web Scraping & Bots 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 定义机器人目标
  2. 自动提交简单表单
  3. 安排基础任务
  4. 机器人日志记录与错误处理
← 返回 Web Scraping & Bots