{"id":1,"date":"2023-07-14T18:44:47","date_gmt":"2023-07-14T10:44:47","guid":{"rendered":"http:\/\/www.weigot.com\/?p=1"},"modified":"2023-07-14T18:48:11","modified_gmt":"2023-07-14T10:48:11","slug":"hello-world","status":"publish","type":"post","link":"http:\/\/www.weigot.com\/index.php\/2023\/07\/14\/hello-world\/","title":{"rendered":"\u8981\u5b9e\u73b0 Flask \u5e94\u7528\u7a0b\u5e8f\u63a5\u6536 POST \u8bf7\u6c42\u5e76\u5728\u540e\u53f0\u6267\u884c\u81ea\u52a8\u5316\u4efb\u52a1"},"content":{"rendered":"\n<p>\u8981\u5b9e\u73b0 Flask \u5e94\u7528\u7a0b\u5e8f\u63a5\u6536 POST \u8bf7\u6c42\u5e76\u5728\u540e\u53f0\u6267\u884c\u81ea\u52a8\u5316\u4efb\u52a1\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Python \u7684 selenium \u5e93\u6765\u5b9e\u73b0\u7f51\u9875\u81ea\u52a8\u5316\uff0c\u4f7f\u7528 Flask \u6765\u5b9e\u73b0 POST \u8bf7\u6c42\u7684\u63a5\u6536\u548c\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u5728 Flask \u5e94\u7528\u7a0b\u5e8f\u4e2d\u63a5\u6536 POST \u8bf7\u6c42\uff0c\u5e76\u4f7f\u7528 selenium \u5e93\u6765\u81ea\u52a8\u5316\u63d0\u4ea4\u7f51\u9875\u8868\u5355\u548c\u5224\u65ad\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from flask import Flask, request\r\nfrom selenium import webdriver\r\nfrom selenium.webdriver.common.keys import Keys\r\n\r\napp = Flask(__name__)\r\n\r\n@app.route('\/', methods=&#91;'POST'])\r\ndef submit_form():\r\n    # \u83b7\u53d6 POST \u8bf7\u6c42\u7684\u8868\u5355\u6570\u636e\r\n    form_data = request.form\r\n\r\n    # \u83b7\u53d6\u8868\u5355\u6570\u636e\u4e2d\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\r\n    username = form_data.get('username')\r\n    password = form_data.get('password')\r\n\r\n    # \u6253\u5f00\u6d4f\u89c8\u5668\u5e76\u8bbf\u95ee\u767b\u5f55\u9875\u9762\r\n    driver = webdriver.Chrome()\r\n    driver.get('https:\/\/example.com\/login')\r\n\r\n    # \u627e\u5230\u7528\u6237\u540d\u548c\u5bc6\u7801\u8f93\u5165\u6846\uff0c\u5e76\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\r\n    username_input = driver.find_element_by_name('username')\r\n    username_input.send_keys(username)\r\n    password_input = driver.find_element_by_name('password')\r\n    password_input.send_keys(password)\r\n\r\n    # \u63d0\u4ea4\u8868\u5355\r\n    password_input.send_keys(Keys.RETURN)\r\n\r\n    # \u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\r\n    driver.implicitly_wait(10)\r\n\r\n    # \u5224\u65ad\u767b\u5f55\u662f\u5426\u6210\u529f\r\n    if 'Welcome' in driver.page_source:\r\n        result = 'Login succeeded'\r\n    else:\r\n        result = 'Login failed'\r\n\r\n    # \u5173\u95ed\u6d4f\u89c8\u5668\r\n    driver.quit()\r\n\r\n    # \u8fd4\u56de\u7ed3\u679c\r\n    return result\r\n\r\nif __name__ == '__main__':\r\n    app.run(debug=True)<\/code><\/pre>\n\n\n\n<p><br>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a \/ \u8def\u5f84\uff0c\u5e76\u5c06\u5176\u7ed1\u5b9a\u5230 HTTP POST \u8bf7\u6c42\u3002\u5f53\u63a5\u6536\u5230 POST \u8bf7\u6c42\u65f6\uff0c\u6211\u4eec\u4ece\u8868\u5355\u6570\u636e\u4e2d\u83b7\u53d6\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5e76\u4f7f\u7528 selenium \u5e93\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u81ea\u52a8\u5316\u586b\u5199\u8868\u5355\uff0c\u63d0\u4ea4\u8868\u5355\uff0c\u5e76\u5224\u65ad\u767b\u5f55\u662f\u5426\u6210\u529f\u3002\u6700\u540e\uff0c\u6211\u4eec\u8fd4\u56de\u767b\u5f55\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4ec5\u7528\u4e8e\u6f14\u793a\u5982\u4f55\u5728 Flask \u5e94\u7528\u7a0b\u5e8f\u4e2d\u63a5\u6536 POST \u8bf7\u6c42\u548c\u4f7f\u7528 selenium \u5e93\u8fdb\u884c\u7f51\u9875\u81ea\u52a8\u5316\uff0c\u5e76\u4e0d\u5305\u62ec\u5b8c\u6574\u7684\u9519\u8bef\u5904\u7406\u548c\u5b89\u5168\u6027\u63aa\u65bd\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u60a8\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u98ce\u9669\u8fdb\u884c\u9002\u5f53\u7684\u4f18\u5316\u548c\u6539\u8fdb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u5b9e\u73b0 Flask \u5e94\u7528\u7a0b\u5e8f\u63a5\u6536 POST \u8bf7\u6c42\u5e76\u5728\u540e\u53f0\u6267\u884c\u81ea\u52a8\u5316\u4efb\u52a1\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Python \u7684 sele [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1","post","type-post","status-publish","format-standard","hentry","category-suibianjilu"],"_links":{"self":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"count":2,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":7,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/1\/revisions\/7"}],"wp:attachment":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}