{"id":1162289,"date":"2025-01-13T19:26:49","date_gmt":"2025-01-13T11:26:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1162289.html"},"modified":"2025-01-13T19:27:04","modified_gmt":"2025-01-13T11:27:04","slug":"python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e8%a1%a8%e5%8d%95%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1162289.html","title":{"rendered":"python\u5982\u4f55\u63d0\u53d6\u8868\u5355\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25203338\/eed7cfd3-9dcc-45da-9cde-103e5b2d2289.webp\" alt=\"python\u5982\u4f55\u63d0\u53d6\u8868\u5355\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u63d0\u53d6\u8868\u5355\u6570\u636e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Flask\u3001Django\u7b49Web\u6846\u67b6\u3001BeautifulSoup\u5e93\u3001Pandas\u5e93\u7b49\u3002\u901a\u8fc7Flask\u6846\u67b6\u53ef\u4ee5\u8f7b\u677e\u5904\u7406HTTP\u8bf7\u6c42\u548c\u8868\u5355\u6570\u636e\uff0c\u901a\u8fc7BeautifulSoup\u53ef\u4ee5\u89e3\u6790HTML\u8868\u5355\u7ed3\u6784\uff0c\u800cPandas\u5e93\u53ef\u4ee5\u5904\u7406Excel\u6216CSV\u683c\u5f0f\u7684\u8868\u5355\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Flask\u6846\u67b6\u6765\u63d0\u53d6\u8868\u5355\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Flask\u6846\u67b6\u63d0\u53d6\u8868\u5355\u6570\u636e<\/h3>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u9002\u5408\u5feb\u901f\u5f00\u53d1Web\u5e94\u7528\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Flask\u63d0\u53d6\u8868\u5355\u6570\u636e\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Flask<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Flask\u5e93\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Flask\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Flask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efaFlask\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\uff0c\u5e76\u914d\u7f6e\u8def\u7531\u6765\u5904\u7406\u8868\u5355\u63d0\u4ea4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, render_template<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    return render_template(&#39;form.html&#39;)<\/p>\n<p>@app.route(&#39;\/submit&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def submit():<\/p>\n<p>    # \u63d0\u53d6\u8868\u5355\u6570\u636e<\/p>\n<p>    username = request.form.get(&#39;username&#39;)<\/p>\n<p>    em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l = request.form.get(&#39;email&#39;)<\/p>\n<p>    return f&quot;Received data: Username - {username}, Email - {email}&quot;<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521b\u5efaHTML\u8868\u5355<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684HTML\u8868\u5355\uff0c\u7528\u4e8e\u6570\u636e\u8f93\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!-- form.html --&gt;<\/p>\n<p>&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Flask Form&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;form action=&quot;\/submit&quot; method=&quot;post&quot;&gt;<\/p>\n<p>        &lt;label for=&quot;username&quot;&gt;Username:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; id=&quot;username&quot; name=&quot;username&quot;&gt;&lt;br&gt;<\/p>\n<p>        &lt;label for=&quot;email&quot;&gt;Email:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;email&quot; id=&quot;email&quot; name=&quot;email&quot;&gt;&lt;br&gt;<\/p>\n<p>        &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u8868\u5355\u6570\u636e<\/h3>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684\u5e93\uff0c\u9002\u5408\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u8868\u5355\u6570\u636e\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5BeautifulSoup<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5BeautifulSoup\u548crequests\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u89e3\u6790HTML\u8868\u5355<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u8868\u5355\uff0c\u5e76\u63d0\u53d6\u8868\u5355\u4e2d\u7684\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>url = &#39;http:\/\/example.com\/form.html&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>html_content = response.content<\/p>\n<h2><strong>\u89e3\u6790HTML<\/strong><\/h2>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u8868\u5355\u6570\u636e<\/strong><\/h2>\n<p>form = soup.find(&#39;form&#39;)<\/p>\n<p>inputs = form.find_all(&#39;input&#39;)<\/p>\n<p>form_data = {input.get(&#39;name&#39;): input.get(&#39;value&#39;) for input in inputs}<\/p>\n<p>print(form_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u5904\u7406Excel\u6216CSV\u8868\u5355\u6570\u636e<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u9002\u5408\u5904\u7406Excel\u6216CSV\u683c\u5f0f\u7684\u8868\u5355\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Pandas\u5904\u7406\u8868\u5355\u6570\u636e\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Pandas<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6Excel\u6216CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u8bfb\u53d6Excel\u6216CSV\u6587\u4ef6\uff0c\u5e76\u63d0\u53d6\u8868\u5355\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>excel_file = &#39;data.xlsx&#39;<\/p>\n<p>df = pd.read_excel(excel_file)<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>csv_file = &#39;data.csv&#39;<\/p>\n<p>df = pd.read_csv(csv_file)<\/p>\n<h2><strong>\u63d0\u53d6\u8868\u5355\u6570\u636e<\/strong><\/h2>\n<p>form_data = df.to_dict(orient=&#39;records&#39;)<\/p>\n<p>print(form_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Django\u6846\u67b6\u5904\u7406\u8868\u5355\u6570\u636e<\/h3>\n<\/p>\n<p><p>Django\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Web\u6846\u67b6\uff0c\u9002\u5408\u5f00\u53d1\u590d\u6742\u7684Web\u5e94\u7528\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Django\u5904\u7406\u8868\u5355\u6570\u636e\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Django<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Django\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install django<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efaDjango\u9879\u76ee\u548c\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Django\u547d\u4ee4\u884c\u5de5\u5177\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\u548c\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">django-admin startproject myproject<\/p>\n<p>cd myproject<\/p>\n<p>python manage.py startapp myapp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5b9a\u4e49\u8868\u5355\u6a21\u578b\u548c\u89c6\u56fe<\/h4>\n<\/p>\n<p><p>\u5728\u5e94\u7528\u4e2d\u5b9a\u4e49\u8868\u5355\u6a21\u578b\u548c\u89c6\u56fe\uff0c\u7528\u4e8e\u5904\u7406\u8868\u5355\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># myapp\/forms.py<\/p>\n<p>from django import forms<\/p>\n<p>class MyForm(forms.Form):<\/p>\n<p>    username = forms.CharField(label=&#39;Username&#39;, max_length=100)<\/p>\n<p>    email = forms.EmailField(label=&#39;Email&#39;)<\/p>\n<h2><strong>myapp\/views.py<\/strong><\/h2>\n<p>from django.shortcuts import render<\/p>\n<p>from .forms import MyForm<\/p>\n<p>def index(request):<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        form = MyForm(request.POST)<\/p>\n<p>        if form.is_valid():<\/p>\n<p>            username = form.cleaned_data[&#39;username&#39;]<\/p>\n<p>            email = form.cleaned_data[&#39;email&#39;]<\/p>\n<p>            return render(request, &#39;submit.html&#39;, {&#39;username&#39;: username, &#39;email&#39;: email})<\/p>\n<p>    else:<\/p>\n<p>        form = MyForm()<\/p>\n<p>    return render(request, &#39;index.html&#39;, {&#39;form&#39;: form})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u521b\u5efaHTML\u6a21\u677f<\/h4>\n<\/p>\n<p><p>\u521b\u5efaHTML\u6a21\u677f\uff0c\u7528\u4e8e\u663e\u793a\u8868\u5355\u548c\u63d0\u4ea4\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!-- templates\/index.html --&gt;<\/p>\n<p>&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Django Form&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;form method=&quot;post&quot;&gt;<\/p>\n<p>        {% csrf_token %}<\/p>\n<p>        {{ form.as_p }}<\/p>\n<p>        &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&lt;!-- templates\/submit.html --&gt;<\/p>\n<p>&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Form Submitted&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;p&gt;Received data: Username - {{ username }}, Email - {{ email }}&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Flask-WTF\u5904\u7406\u8868\u5355\u6570\u636e<\/h3>\n<\/p>\n<p><p>Flask-WTF\u662fFlask\u7684\u4e00\u4e2a\u6269\u5c55\uff0c\u7528\u4e8e\u7b80\u5316\u8868\u5355\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Flask-WTF\u5904\u7406\u8868\u5355\u6570\u636e\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Flask-WTF<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Flask-WTF\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Flask-WTF<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u914d\u7f6eFlask\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u914d\u7f6eFlask\u5e94\u7528\uff0c\u6dfb\u52a0\u8868\u5355\u5904\u7406\u903b\u8f91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, render_template, request<\/p>\n<p>from flask_wtf import FlaskForm<\/p>\n<p>from wtforms import StringField, SubmitField<\/p>\n<p>from wtforms.validators import DataRequired<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>app.config[&#39;SECRET_KEY&#39;] = &#39;your_secret_key&#39;<\/p>\n<p>class MyForm(FlaskForm):<\/p>\n<p>    username = StringField(&#39;Username&#39;, validators=[DataRequired()])<\/p>\n<p>    email = StringField(&#39;Email&#39;, validators=[DataRequired()])<\/p>\n<p>    submit = SubmitField(&#39;Submit&#39;)<\/p>\n<p>@app.route(&#39;\/&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])<\/p>\n<p>def index():<\/p>\n<p>    form = MyForm()<\/p>\n<p>    if form.validate_on_submit():<\/p>\n<p>        username = form.username.data<\/p>\n<p>        email = form.email.data<\/p>\n<p>        return render_template(&#39;submit.html&#39;, username=username, email=email)<\/p>\n<p>    return render_template(&#39;index.html&#39;, form=form)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521b\u5efaHTML\u6a21\u677f<\/h4>\n<\/p>\n<p><p>\u521b\u5efaHTML\u6a21\u677f\uff0c\u7528\u4e8e\u663e\u793a\u8868\u5355\u548c\u63d0\u4ea4\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!-- templates\/index.html --&gt;<\/p>\n<p>&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Flask-WTF Form&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;form method=&quot;post&quot;&gt;<\/p>\n<p>        {{ form.hidden_tag() }}<\/p>\n<p>        &lt;p&gt;<\/p>\n<p>            {{ form.username.label }}&lt;br&gt;<\/p>\n<p>            {{ form.username(size=32) }}&lt;br&gt;<\/p>\n<p>            {% for error in form.username.errors %}<\/p>\n<p>                &lt;span style=&quot;color: red;&quot;&gt;[{{ error }}]&lt;\/span&gt;<\/p>\n<p>            {% endfor %}<\/p>\n<p>        &lt;\/p&gt;<\/p>\n<p>        &lt;p&gt;<\/p>\n<p>            {{ form.email.label }}&lt;br&gt;<\/p>\n<p>            {{ form.email(size=32) }}&lt;br&gt;<\/p>\n<p>            {% for error in form.email.errors %}<\/p>\n<p>                &lt;span style=&quot;color: red;&quot;&gt;[{{ error }}]&lt;\/span&gt;<\/p>\n<p>            {% endfor %}<\/p>\n<p>        &lt;\/p&gt;<\/p>\n<p>        &lt;p&gt;{{ form.submit() }}&lt;\/p&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&lt;!-- templates\/submit.html --&gt;<\/p>\n<p>&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Form Submitted&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;p&gt;Received data: Username - {{ username }}, Email - {{ email }}&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528FastAPI\u5904\u7406\u8868\u5355\u6570\u636e<\/h3>\n<\/p>\n<p><p>FastAPI\u662f\u4e00\u4e2a\u73b0\u4ee3\u3001\u5feb\u901f\uff08\u9ad8\u6027\u80fd\uff09\u7684Web\u6846\u67b6\uff0c\u9002\u5408\u5feb\u901f\u5f00\u53d1\u57fa\u4e8eAPI\u7684\u5e94\u7528\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528FastAPI\u5904\u7406\u8868\u5355\u6570\u636e\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5FastAPI\u548cUvicorn<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5FastAPI\u548cUvicorn\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install fastapi uvicorn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efaFastAPI\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684FastAPI\u5e94\u7528\uff0c\u5e76\u914d\u7f6e\u8def\u7531\u6765\u5904\u7406\u8868\u5355\u63d0\u4ea4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fastapi import FastAPI, Form<\/p>\n<p>from fastapi.responses import HTMLResponse<\/p>\n<p>app = FastAPI()<\/p>\n<p>@app.get(&quot;\/&quot;, response_class=HTMLResponse)<\/p>\n<p>async def get_form():<\/p>\n<p>    return &quot;&quot;&quot;<\/p>\n<p>    &lt;form action=&quot;\/submit&quot; method=&quot;post&quot;&gt;<\/p>\n<p>        &lt;label for=&quot;username&quot;&gt;Username:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; id=&quot;username&quot; name=&quot;username&quot;&gt;&lt;br&gt;<\/p>\n<p>        &lt;label for=&quot;email&quot;&gt;Email:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;email&quot; id=&quot;email&quot; name=&quot;email&quot;&gt;&lt;br&gt;<\/p>\n<p>        &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>@app.post(&quot;\/submit&quot;)<\/p>\n<p>async def submit_form(username: str = Form(...), email: str = Form(...)):<\/p>\n<p>    return {&quot;username&quot;: username, &quot;email&quot;: email}<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    import uvicorn<\/p>\n<p>    uvicorn.run(app, host=&quot;127.0.0.1&quot;, port=8000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528Requests\u5e93\u6a21\u62df\u8868\u5355\u63d0\u4ea4<\/h3>\n<\/p>\n<p><p>Requests\u5e93\u662f\u4e00\u4e2a\u7b80\u5355\u4f46\u529f\u80fd\u5f3a\u5927\u7684HTTP\u5e93\uff0c\u9002\u5408\u6a21\u62df\u8868\u5355\u63d0\u4ea4\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Requests\u5e93\u6a21\u62df\u8868\u5355\u63d0\u4ea4\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Requests\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Requests\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6a21\u62df\u8868\u5355\u63d0\u4ea4<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Requests\u5e93\u6a21\u62df\u8868\u5355\u63d0\u4ea4\uff0c\u5e76\u63d0\u53d6\u54cd\u5e94\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u8868\u5355\u6570\u636e<\/strong><\/h2>\n<p>form_data = {<\/p>\n<p>    &#39;username&#39;: &#39;example_user&#39;,<\/p>\n<p>    &#39;email&#39;: &#39;example@example.com&#39;<\/p>\n<p>}<\/p>\n<h2><strong>\u63d0\u4ea4\u8868\u5355<\/strong><\/h2>\n<p>response = requests.post(&#39;http:\/\/example.com\/submit&#39;, data=form_data)<\/p>\n<h2><strong>\u63d0\u53d6\u54cd\u5e94\u6570\u636e<\/strong><\/h2>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528Selenium\u81ea\u52a8\u5316\u63d0\u53d6\u8868\u5355\u6570\u636e<\/h3>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u7528\u4e8eWeb\u5e94\u7528\u6d4b\u8bd5\u7684\u81ea\u52a8\u5316\u5de5\u5177\uff0c\u9002\u5408\u81ea\u52a8\u5316\u63d0\u53d6\u8868\u5355\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Selenium\u81ea\u52a8\u5316\u63d0\u53d6\u8868\u5355\u6570\u636e\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Selenium<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Selenium\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b89\u88c5WebDriver<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u4f7f\u7528\u7684\u6d4f\u89c8\u5668\uff0c\u5b89\u88c5\u76f8\u5e94\u7684WebDriver\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Chrome\u6d4f\u89c8\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u4e0b\u8f7dChromeDriver\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u7cfb\u7edf\u8def\u5f84<\/p>\n<h2><strong>https:\/\/sites.google.com\/a\/chromium.org\/chromedriver\/<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u81ea\u52a8\u5316\u63d0\u53d6\u8868\u5355\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Selenium\u81ea\u52a8\u5316\u63d0\u53d6\u8868\u5355\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u542f\u52a8\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;http:\/\/example.com\/form.html&#39;)<\/p>\n<h2><strong>\u586b\u5199\u8868\u5355<\/strong><\/h2>\n<p>username_input = driver.find_element_by_name(&#39;username&#39;)<\/p>\n<p>email_input = driver.find_element_by_name(&#39;email&#39;)<\/p>\n<p>username_input.send_keys(&#39;example_user&#39;)<\/p>\n<p>email_input.send_keys(&#39;example@example.com&#39;)<\/p>\n<h2><strong>\u63d0\u4ea4\u8868\u5355<\/strong><\/h2>\n<p>submit_button = driver.find_element_by_xpath(&#39;\/\/input[@type=&quot;submit&quot;]&#39;)<\/p>\n<p>submit_button.click()<\/p>\n<h2><strong>\u63d0\u53d6\u54cd\u5e94\u6570\u636e<\/strong><\/h2>\n<p>response_data = driver.page_source<\/p>\n<p>print(response_data)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u591a\u79cd\u4f7f\u7528Python\u63d0\u53d6\u8868\u5355\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u5305\u62ecFlask\u3001BeautifulSoup\u3001Pandas\u3001Django\u3001Flask-WTF\u3001FastAPI\u3001Requests\u548cSelenium\u7b49\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u9ad8\u6548\u5730\u5904\u7406\u8868\u5355\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u4eceHTML\u8868\u5355\u4e2d\u63d0\u53d6\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u63d0\u53d6HTML\u8868\u5355\u6570\u636e\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Flask\u6216Django\u7b49Web\u6846\u67b6\u5b9e\u73b0\u3002\u4f7f\u7528Flask\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>request.form<\/code>\u5bf9\u8c61\u8bbf\u95ee\u8868\u5355\u6570\u636e\uff1b\u5728Django\u4e2d\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528<code>request.POST<\/code>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5141\u8bb8\u4f60\u8f7b\u677e\u83b7\u53d6\u7528\u6237\u63d0\u4ea4\u7684\u8868\u5355\u5b57\u6bb5\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<p><strong>\u63d0\u53d6\u8868\u5355\u6570\u636e\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u6570\u636e\u683c\u5f0f\uff1f<\/strong><br \/>\u8868\u5355\u6570\u636e\u901a\u5e38\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u5728\uff0c\u5e38\u89c1\u683c\u5f0f\u5305\u62ec\u6587\u672c\u6846\u3001\u4e0b\u62c9\u83dc\u5355\u3001\u590d\u9009\u6846\u7b49\u3002\u5f53\u7528\u6237\u63d0\u4ea4\u8868\u5355\u65f6\uff0c\u6570\u636e\u4f1a\u7f16\u7801\u4e3a<code>application\/x-www-form-urlencoded<\/code>\u6216<code>multipart\/form-data<\/code>\uff0c\u53d6\u51b3\u4e8e\u8868\u5355\u7684<code>enctype<\/code>\u5c5e\u6027\u3002\u8fd9\u4e24\u79cd\u683c\u5f0f\u90fd\u53ef\u4ee5\u901a\u8fc7\u76f8\u5e94\u7684Python\u5e93\u8fdb\u884c\u89e3\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u63d0\u53d6\u7684\u8868\u5355\u6570\u636e\u4e2d\u7684\u7a7a\u503c\u6216\u9519\u8bef\u8f93\u5165\uff1f<\/strong><br \/>\u5728\u63d0\u53d6\u8868\u5355\u6570\u636e\u65f6\uff0c\u52a1\u5fc5\u68c0\u67e5\u5b57\u6bb5\u662f\u5426\u4e3a\u7a7a\u6216\u683c\u5f0f\u662f\u5426\u6b63\u786e\u3002\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u9a8c\u8bc1\u8f93\u5165\uff0c\u786e\u4fdd\u6570\u636e\u7684\u6709\u6548\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>if not data:<\/code>\u6765\u68c0\u67e5\u6570\u636e\u662f\u5426\u4e3a\u7a7a\uff0c\u6216\u8005\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u9a8c\u8bc1\u8f93\u5165\u683c\u5f0f\u3002\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\u53ef\u4ee5\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\uff0c\u5e76\u51cf\u5c11\u540e\u7aef\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u63d0\u53d6\u8868\u5355\u6570\u636e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Flask\u3001Django\u7b49Web\u6846\u67b6\u3001BeautifulSoup [&hellip;]","protected":false},"author":3,"featured_media":1162314,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162289"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1162289"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162289\/revisions"}],"predecessor-version":[{"id":1162321,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162289\/revisions\/1162321"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1162314"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1162289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1162289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1162289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}