{"id":1003837,"date":"2024-12-27T10:20:45","date_gmt":"2024-12-27T02:20:45","guid":{"rendered":""},"modified":"2024-12-27T10:20:47","modified_gmt":"2024-12-27T02:20:47","slug":"python%e7%bd%91%e9%a1%b5%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1003837.html","title":{"rendered":"python\u7f51\u9875\u5982\u4f55\u8f93\u5165\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081018\/cb518b5d-2498-4dcf-8c56-8210eb9c69fe.webp\" alt=\"python\u7f51\u9875\u5982\u4f55\u8f93\u5165\u4ee3\u7801\" \/><\/p>\n<p><p> \u4e00\u3001\u4f7f\u7528Python\u8fdb\u884c\u7f51\u9875\u8f93\u5165\u7684\u57fa\u672c\u65b9\u6cd5  <\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8fdb\u884c\u7f51\u9875\u8f93\u5165\uff0c\u4e3b\u8981\u901a\u8fc7<strong>Web\u6846\u67b6\u3001\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177\u3001API\u63a5\u53e3<\/strong>\u7b49\u51e0\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u4f7f\u7528Web\u6846\u67b6\u53ef\u4ee5\u76f4\u63a5\u6784\u5efa\u52a8\u6001\u7f51\u9875\uff0c\u901a\u8fc7\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177\u5982Selenium\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u64cd\u4f5c\uff0c\u800c\u901a\u8fc7API\u63a5\u53e3\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u7684\u4f20\u8f93\u4e0e\u4ea4\u4e92\u3002<strong>Web\u6846\u67b6<\/strong>\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u5e38\u7528\u7684Web\u6846\u67b6\u5305\u62ecFlask\u548cDjango\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684Flask\u6846\u67b6\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>Flask\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u6613\u4e8e\u4e0a\u624b\uff0c\u9002\u5408\u5c0f\u578b\u5e94\u7528\u7684\u5f00\u53d1\u3002\u4f7f\u7528Flask\uff0c\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u4e00\u4e2aWeb\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u901a\u8fc7HTML\u8868\u5355\u63a5\u6536\u7528\u6237\u8f93\u5165\u3002\u4ee5\u4e0b\u662fFlask\u7684\u57fa\u672c\u4f7f\u7528\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Flask<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u5b89\u88c5Flask\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b9e\u73b0\uff1a<code>pip install flask<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaFlask\u5e94\u7528<\/strong>\uff1a\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aPython\u6587\u4ef6\uff0c\u4f8b\u5982<code>app.py<\/code>\uff0c\u5e76\u5bfc\u5165Flask\u6a21\u5757\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><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u4e49\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570<\/strong>\uff1a\u4f7f\u7528Flask\u7684\u8def\u7531\u88c5\u9970\u5668\u5b9a\u4e49URL\u8def\u5f84\uff0c\u5e76\u521b\u5efa\u76f8\u5e94\u7684\u89c6\u56fe\u51fd\u6570\u6765\u5904\u7406\u8bf7\u6c42\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u9996\u9875\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/&#39;)<\/p>\n<p>def home():<\/p>\n<p>    return render_template(&#39;index.html&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c<code>render_template<\/code>\u7528\u4e8e\u6e32\u67d3HTML\u6a21\u677f\uff0c\u8fd9\u91cc\u9700\u8981\u4e00\u4e2a\u540d\u4e3a<code>index.html<\/code>\u7684\u6a21\u677f\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaHTML\u8868\u5355<\/strong>\uff1a\u5728<code>templates<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa<code>index.html<\/code>\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u8868\u5355\uff0c\u7528\u4e8e\u63a5\u6536\u7528\u6237\u8f93\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Input 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; action=&quot;\/submit&quot;&gt;<\/p>\n<p>        &lt;label for=&quot;input&quot;&gt;Enter something:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; id=&quot;input&quot; name=&quot;input&quot;&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<\/li>\n<li>\n<p><strong>\u5904\u7406\u8868\u5355\u63d0\u4ea4<\/strong>\uff1a\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u8def\u7531\u6765\u5904\u7406\u8868\u5355\u63d0\u4ea4\u7684\u6570\u636e\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/submit&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def submit():<\/p>\n<p>    user_input = request.form[&#39;input&#39;]<\/p>\n<p>    # \u5728\u8fd9\u91cc\u53ef\u4ee5\u5bf9\u7528\u6237\u8f93\u5165\u8fdb\u884c\u5904\u7406<\/p>\n<p>    return f&quot;You entered: {user_input}&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u5e94\u7528<\/strong>\uff1a\u6700\u540e\uff0c\u901a\u8fc7Flask\u7684\u5185\u7f6e\u670d\u52a1\u5668\u8fd0\u884c\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u540e\uff0c\u542f\u52a8Flask\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee<code>http:\/\/127.0.0.1:5000\/<\/code>\uff0c\u5373\u53ef\u770b\u5230\u4e00\u4e2a\u7b80\u5355\u7684\u8f93\u5165\u8868\u5355\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u4fe1\u606f\u5e76\u63d0\u4ea4\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Selenium\u8fdb\u884c\u7f51\u9875\u8f93\u5165<\/p>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177\uff0c\u5e7f\u6cdb\u7528\u4e8eWeb\u6d4b\u8bd5\u548c\u7f51\u9875\u64cd\u4f5c\u81ea\u52a8\u5316\u3002\u901a\u8fc7Selenium\uff0cPython\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u5728\u7f51\u9875\u4e0a\u7684\u8f93\u5165\u548c\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Selenium\u8fdb\u884c\u7f51\u9875\u8f93\u5165\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Selenium<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u5b89\u88c5Selenium\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b9e\u73b0\uff1a<code>pip install selenium<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e0b\u8f7dWebDriver<\/strong>\uff1aSelenium\u9700\u8981\u4e0e\u6d4f\u89c8\u5668\u914d\u5408\u4f7f\u7528\uff0c\u56e0\u6b64\u9700\u8981\u4e0b\u8f7d\u4e0e\u6d4f\u89c8\u5668\u5339\u914d\u7684WebDriver\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f7f\u7528Chrome\u6d4f\u89c8\u5668\uff0c\u9700\u8981\u4e0b\u8f7dChromeDriver\uff0c\u5e76\u5c06\u5176\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199Selenium\u811a\u672c<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\uff0c\u4f8b\u5982<code>selenium_input.py<\/code>\uff0c\u5e76\u5bfc\u5165Selenium\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.common.by import By<\/p>\n<p>from selenium.webdriver.common.keys import Keys<\/p>\n<p>import time<\/p>\n<h2><strong>\u521b\u5efaWebDriver\u5b9e\u4f8b<\/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\/input-form&#39;)<\/p>\n<h2><strong>\u627e\u5230\u8f93\u5165\u6846\u5e76\u8f93\u5165\u6570\u636e<\/strong><\/h2>\n<p>input_box = driver.find_element(By.ID, &#39;input&#39;)<\/p>\n<p>input_box.send_keys(&#39;Hello, Selenium!&#39;)<\/p>\n<h2><strong>\u63d0\u4ea4\u8868\u5355<\/strong><\/h2>\n<p>input_box.send_keys(Keys.RETURN)<\/p>\n<h2><strong>\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u4ee5\u4fbf\u67e5\u770b\u7ed3\u679c<\/strong><\/h2>\n<p>time.sleep(5)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884cSelenium\u811a\u672c<\/strong>\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u8be5\u811a\u672c\uff0cSelenium\u5c06\u81ea\u52a8\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u8bbf\u95ee\u6307\u5b9a\u7684URL\uff0c\u627e\u5230\u8f93\u5165\u6846\u5e76\u8f93\u5165\u6570\u636e\uff0c\u6700\u540e\u63d0\u4ea4\u8868\u5355\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7Selenium\uff0cPython\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u5728\u7f51\u9875\u4e0a\u7684\u5404\u79cd\u64cd\u4f5c\uff0c\u5305\u62ec\u8f93\u5165\u3001\u70b9\u51fb\u3001\u6eda\u52a8\u7b49\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u9700\u8981\u81ea\u52a8\u5316\u64cd\u4f5c\u7684\u7f51\u7ad9\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u901a\u8fc7API\u63a5\u53e3\u8fdb\u884c\u6570\u636e\u8f93\u5165<\/p>\n<\/p>\n<p><p>API\u63a5\u53e3\u662f\u4e00\u79cd\u6807\u51c6\u5316\u7684\u901a\u4fe1\u65b9\u5f0f\uff0c\u5141\u8bb8\u4e0d\u540c\u7684\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u4ea4\u6362\u6570\u636e\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528HTTP\u8bf7\u6c42\u5e93\u5982<code>requests<\/code>\u6765\u4e0eAPI\u63a5\u53e3\u8fdb\u884c\u4ea4\u4e92\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u8f93\u5165\u548c\u83b7\u53d6\u3002\u4ee5\u4e0b\u662f\u901a\u8fc7API\u63a5\u53e3\u8fdb\u884c\u6570\u636e\u8f93\u5165\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Requests\u5e93<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u5b89\u88c5Requests\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b9e\u73b0\uff1a<code>pip install requests<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u8bf7\u6c42\u4ee3\u7801<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\uff0c\u4f8b\u5982<code>api_input.py<\/code>\uff0c\u5e76\u5bfc\u5165Requests\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>import json<\/p>\n<h2><strong>\u5b9a\u4e49API\u7684URL\u548c\u8bf7\u6c42\u5934<\/strong><\/h2>\n<p>api_url = &#39;http:\/\/example.com\/api\/input&#39;<\/p>\n<p>headers = {&#39;Content-Type&#39;: &#39;application\/json&#39;}<\/p>\n<h2><strong>\u5b9a\u4e49\u8981\u53d1\u9001\u7684\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;input&#39;: &#39;Hello, API!&#39;}<\/p>\n<h2><strong>\u53d1\u9001POST\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.post(api_url, headers=headers, data=json.dumps(data))<\/p>\n<h2><strong>\u68c0\u67e5\u54cd\u5e94\u72b6\u6001<\/strong><\/h2>\n<p>if response.status_code == 200:<\/p>\n<p>    print(&#39;Data sent successfully:&#39;, response.json())<\/p>\n<p>else:<\/p>\n<p>    print(&#39;Failed to send data:&#39;, response.status_code, response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u8bf7\u6c42\u4ee3\u7801<\/strong>\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u8be5\u811a\u672c\uff0cPython\u5c06\u901a\u8fc7API\u63a5\u53e3\u53d1\u9001\u6570\u636e\uff0c\u5e76\u6253\u5370\u54cd\u5e94\u7ed3\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7API\u63a5\u53e3\uff0cPython\u53ef\u4ee5\u4e0eWeb\u670d\u52a1\u8fdb\u884c\u4ea4\u4e92\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u8f93\u5165\u548c\u83b7\u53d6\uff0c\u9002\u7528\u4e8e\u6570\u636e\u9a71\u52a8\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7ed3\u5408JavaScript\u4e0ePython\u5b9e\u73b0\u7f51\u9875\u8f93\u5165<\/p>\n<\/p>\n<p><p>\u5728\u73b0\u4ee3Web\u5f00\u53d1\u4e2d\uff0cJavaScript\u4e0ePython\u5e38\u5e38\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002\u901a\u8fc7JavaScript\uff0c\u53ef\u4ee5\u5728\u5ba2\u6237\u7aef\u5b9e\u73b0\u52a8\u6001\u4ea4\u4e92\uff0c\u800cPython\u5219\u7528\u4e8e\u670d\u52a1\u5668\u7aef\u7684\u903b\u8f91\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u7ed3\u5408JavaScript\u4e0ePython\u5b9e\u73b0\u7f51\u9875\u8f93\u5165\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaFlask\u5e94\u7528<\/strong>\uff1a\u9996\u5148\u521b\u5efa\u4e00\u4e2aFlask\u5e94\u7528\uff0c\u5e76\u5b9a\u4e49\u7528\u4e8e\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u8def\u7531\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, jsonify<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/submit&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def submit():<\/p>\n<p>    user_input = request.json.get(&#39;input&#39;)<\/p>\n<p>    response = {&#39;message&#39;: f&quot;You entered: {user_input}&quot;}<\/p>\n<p>    return jsonify(response)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199HTML\u9875\u9762<\/strong>\uff1a\u5728<code>templates<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa<code>index.html<\/code>\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0JavaScript\u4ee3\u7801\uff0c\u4ee5\u901a\u8fc7AJAX\u8bf7\u6c42\u4e0e\u670d\u52a1\u5668\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;AJAX Form&lt;\/title&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        function sendData() {<\/p>\n<p>            const input = document.getElementById(&#39;input&#39;).value;<\/p>\n<p>            fetch(&#39;\/submit&#39;, {<\/p>\n<p>                method: &#39;POST&#39;,<\/p>\n<p>                headers: {<\/p>\n<p>                    &#39;Content-Type&#39;: &#39;application\/json&#39;<\/p>\n<p>                },<\/p>\n<p>                body: JSON.stringify({ input: input })<\/p>\n<p>            })<\/p>\n<p>            .then(response =&gt; response.json())<\/p>\n<p>            .then(data =&gt; {<\/p>\n<p>                document.getElementById(&#39;response&#39;).innerText = data.message;<\/p>\n<p>            })<\/p>\n<p>            .catch(error =&gt; console.error(&#39;Error:&#39;, error));<\/p>\n<p>        }<\/p>\n<p>    &lt;\/script&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;input type=&quot;text&quot; id=&quot;input&quot; placeholder=&quot;Enter something&quot;&gt;<\/p>\n<p>    &lt;button onclick=&quot;sendData()&quot;&gt;Submit&lt;\/button&gt;<\/p>\n<p>    &lt;p id=&quot;response&quot;&gt;&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884cFlask\u5e94\u7528<\/strong>\uff1a\u901a\u8fc7Flask\u7684\u5185\u7f6e\u670d\u52a1\u5668\u8fd0\u884c\u5e94\u7528\uff0c\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee<code>http:\/\/127.0.0.1:5000\/<\/code>\uff0c\u5373\u53ef\u770b\u5230\u4e00\u4e2a\u8f93\u5165\u6846\u548c\u6309\u94ae\u3002\u7528\u6237\u8f93\u5165\u4fe1\u606f\u540e\u70b9\u51fb\u6309\u94ae\uff0cJavaScript\u5c06\u901a\u8fc7AJAX\u8bf7\u6c42\u5c06\u6570\u636e\u53d1\u9001\u5230\u670d\u52a1\u5668\uff0c\u670d\u52a1\u5668\u5904\u7406\u540e\u8fd4\u56de\u7ed3\u679c\uff0c\u5e76\u5728\u9875\u9762\u4e0a\u663e\u793a\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7ed3\u5408JavaScript\u4e0ePython\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u7528\u6237\u4ea4\u4e92\u548c\u6570\u636e\u5904\u7406\uff0c\u9002\u7528\u4e8e\u52a8\u6001Web\u5e94\u7528\u7684\u5f00\u53d1\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528Django\u6846\u67b6\u5b9e\u73b0\u7f51\u9875\u8f93\u5165<\/p>\n<\/p>\n<p><p>Django\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684Python Web\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u5927\u578b\u5e94\u7528\u7684\u5f00\u53d1\u3002\u4f7f\u7528Django\uff0c\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u4e00\u4e2a\u529f\u80fd\u9f50\u5168\u7684Web\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u901a\u8fc7\u8868\u5355\u63a5\u6536\u7528\u6237\u8f93\u5165\u3002\u4ee5\u4e0b\u662fDjango\u5b9e\u73b0\u7f51\u9875\u8f93\u5165\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Django<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u5b89\u88c5Django\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b9e\u73b0\uff1a<code>pip install django<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaDjango\u9879\u76ee<\/strong>\uff1a\u4f7f\u7528Django\u7684\u547d\u4ee4\u884c\u5de5\u5177\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\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<\/li>\n<li>\n<p><strong>\u5b9a\u4e49\u89c6\u56fe\u548cURL<\/strong>\uff1a\u5728<code>myapp\/views.py<\/code>\u4e2d\u5b9a\u4e49\u89c6\u56fe\u51fd\u6570\uff0c\u5e76\u5728<code>myapp\/urls.py<\/code>\u4e2d\u5b9a\u4e49URL\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># myapp\/views.py<\/p>\n<p>from django.shortcuts import render<\/p>\n<p>from django.http import HttpResponse<\/p>\n<p>def input_form(request):<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        user_input = request.POST.get(&#39;input&#39;)<\/p>\n<p>        return HttpResponse(f&quot;You entered: {user_input}&quot;)<\/p>\n<p>    return render(request, &#39;input_form.html&#39;)<\/p>\n<h2><strong>myapp\/urls.py<\/strong><\/h2>\n<p>from django.urls import path<\/p>\n<p>from . import views<\/p>\n<p>urlpatterns = [<\/p>\n<p>    path(&#39;&#39;, views.input_form, name=&#39;input_form&#39;),<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaHTML\u6a21\u677f<\/strong>\uff1a\u5728<code>myapp\/templates<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa<code>input_form.html<\/code>\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u8868\u5355\uff0c\u7528\u4e8e\u63a5\u6536\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Django Input 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>        &lt;label for=&quot;input&quot;&gt;Enter something:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; id=&quot;input&quot; name=&quot;input&quot;&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<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u9879\u76eeURL<\/strong>\uff1a\u5728<code>myproject\/urls.py<\/code>\u4e2d\u5305\u542b\u5e94\u7528\u7684URL\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.contrib import admin<\/p>\n<p>from django.urls import include, path<\/p>\n<p>urlpatterns = [<\/p>\n<p>    path(&#39;admin\/&#39;, admin.site.urls),<\/p>\n<p>    path(&#39;&#39;, include(&#39;myapp.urls&#39;)),<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884cDjango\u9879\u76ee<\/strong>\uff1a\u901a\u8fc7Django\u7684\u5f00\u53d1\u670d\u52a1\u5668\u8fd0\u884c\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python manage.py runserver<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee<code>http:\/\/127.0.0.1:8000\/<\/code>\uff0c\u5373\u53ef\u770b\u5230\u4e00\u4e2a\u8f93\u5165\u8868\u5355\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u4fe1\u606f\u5e76\u63d0\u4ea4\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7Django\u6846\u67b6\uff0c\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u53ef\u6269\u5c55\u7684Web\u5e94\u7528\u7a0b\u5e8f\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u9879\u76ee\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u7f51\u9875\u8f93\u5165\uff0c\u5305\u62ec\u4f7f\u7528Web\u6846\u67b6\uff08\u5982Flask\u548cDjango\uff09\u3001\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177\uff08\u5982Selenium\uff09\u548cAPI\u63a5\u53e3\u7b49\u3002\u6bcf\u79cd\u65b9\u5f0f\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u52bf\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0cPython\u53ef\u4ee5\u5b9e\u73b0\u4ece\u7b80\u5355\u8868\u5355\u5230\u590d\u6742\u4ea4\u4e92\u7684\u5404\u79cdWeb\u8f93\u5165\u529f\u80fd\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u5f00\u53d1\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7f51\u9875\u5e94\u7528\u4e2d\u8f93\u5165\u4ee3\u7801\uff1f<\/strong><br \/>\u5728Python\u7f51\u9875\u5e94\u7528\u4e2d\uff0c\u7528\u6237\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u6587\u672c\u6846\u6216\u4ee3\u7801\u7f16\u8f91\u5668\u6765\u8f93\u5165\u4ee3\u7801\u3002\u4f7f\u7528\u8bf8\u5982Flask\u6216Django\u7b49\u6846\u67b6\u65f6\uff0c\u53ef\u4ee5\u5728\u7f51\u9875\u4e0a\u521b\u5efa\u4e00\u4e2a\u8868\u5355\uff0c\u8ba9\u7528\u6237\u63d0\u4ea4\u4ee3\u7801\u3002\u4e3a\u4e86\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\uff0c\u53ef\u4ee5\u96c6\u6210JavaScript\u4ee3\u7801\u9ad8\u4eae\u5e93\uff0c\u5982Codemirror\u6216Ace Editor\uff0c\u63d0\u4f9b\u66f4\u53cb\u597d\u7684\u8f93\u5165\u73af\u5883\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u8f93\u5165\u7684Python\u4ee3\u7801\u5b89\u5168\uff1f<\/strong><br \/>\u4e3a\u4e86\u9632\u6b62\u7528\u6237\u8f93\u5165\u6076\u610f\u4ee3\u7801\uff0c\u786e\u4fdd\u6267\u884c\u73af\u5883\u7684\u5b89\u5168\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u4f7f\u7528\u6c99\u7bb1\u6280\u672f\u6765\u9650\u5236\u4ee3\u7801\u7684\u6267\u884c\u6743\u9650\uff0c\u6216\u4f7f\u7528\u4e13\u95e8\u7684\u5e93\uff08\u5982RestrictedPython\uff09\u6765\u9694\u79bb\u548c\u9650\u5236\u7528\u6237\u4ee3\u7801\u7684\u529f\u80fd\u3002\u6b64\u5916\uff0c\u5bf9\u7528\u6237\u8f93\u5165\u8fdb\u884c\u4e25\u683c\u9a8c\u8bc1\u548c\u6e05\u7406\u4e5f\u662f\u9632\u6b62\u5b89\u5168\u6f0f\u6d1e\u7684\u91cd\u8981\u63aa\u65bd\u3002<\/p>\n<p><strong>\u5728Python\u7f51\u9875\u5e94\u7528\u4e2d\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u4ee3\u7801\uff1f<\/strong><br \/>\u7528\u6237\u8f93\u5165\u7684\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u540e\u7aef\u8fdb\u884c\u5904\u7406\u548c\u6267\u884c\u3002\u4f7f\u7528<code>exec<\/code>\u6216<code>eval<\/code>\u7b49\u51fd\u6570\u6765\u6267\u884c\u7528\u6237\u8f93\u5165\u7684\u4ee3\u7801\uff0c\u4f46\u8981\u5c0f\u5fc3\u8fd9\u4e9b\u51fd\u6570\u7684\u5b89\u5168\u6027\u3002\u7406\u60f3\u7684\u505a\u6cd5\u662f\u5c06\u7528\u6237\u4ee3\u7801\u7684\u6267\u884c\u7ed3\u679c\u53d1\u9001\u56de\u524d\u7aef\uff0c\u5e76\u901a\u8fc7\u754c\u9762\u5c55\u793a\u7ed9\u7528\u6237\uff0c\u786e\u4fdd\u6267\u884c\u7ed3\u679c\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u4f7f\u7528Python\u8fdb\u884c\u7f51\u9875\u8f93\u5165\u7684\u57fa\u672c\u65b9\u6cd5 \u5728Python\u4e2d\u8fdb\u884c\u7f51\u9875\u8f93\u5165\uff0c\u4e3b\u8981\u901a\u8fc7Web\u6846\u67b6\u3001\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177 [&hellip;]","protected":false},"author":3,"featured_media":1003839,"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\/1003837"}],"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=1003837"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1003837\/revisions"}],"predecessor-version":[{"id":1003840,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1003837\/revisions\/1003840"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1003839"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1003837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1003837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1003837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}