{"id":29,"date":"2023-09-28T13:44:07","date_gmt":"2023-09-28T05:44:07","guid":{"rendered":"http:\/\/www.weigot.com\/?p=29"},"modified":"2023-09-28T13:44:07","modified_gmt":"2023-09-28T05:44:07","slug":"fastapi-%e4%b8%ad%e8%bf%94%e5%9b%9e%e7%ac%ac%e4%b8%89%e6%96%b9%e6%b3%a8%e5%86%8c%e7%9a%84%e7%bb%93%e6%9e%9c%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/www.weigot.com\/index.php\/2023\/09\/28\/fastapi-%e4%b8%ad%e8%bf%94%e5%9b%9e%e7%ac%ac%e4%b8%89%e6%96%b9%e6%b3%a8%e5%86%8c%e7%9a%84%e7%bb%93%e6%9e%9c%e4%bf%a1%e6%81%af\/","title":{"rendered":"FastAPI \u4e2d\u8fd4\u56de\u7b2c\u4e09\u65b9\u6ce8\u518c\u7684\u7ed3\u679c\u4fe1\u606f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u5728 FastAPI \u4e2d\u8fd4\u56de\u7b2c\u4e09\u65b9\u6ce8\u518c\u7684\u7ed3\u679c\u4fe1\u606f\uff0c\u4f60\u53ef\u4ee5\u5c06\u7ed3\u679c\u4fe1\u606f\u4f5c\u4e3a JSON \u6570\u636e\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from fastapi import FastAPI\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.service import Service\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.chrome.options import Options\n\napp = FastAPI()\n\n@app.post(\"\/register\")\ndef register_user(username: str, password: str):\n    # \u914d\u7f6e Selenium Chrome WebDriver\n    chrome_options = Options()\n    chrome_options.add_argument(\"--headless\")  # \u65e0\u5934\u6a21\u5f0f\u8fd0\u884c\u6d4f\u89c8\u5668\uff0c\u4e0d\u4f1a\u6253\u5f00\u6d4f\u89c8\u5668\u7a97\u53e3\n    service = Service('\/path\/to\/chromedriver')  # \u66ff\u6362\u4e3a\u4f60\u7684 Chrome WebDriver \u8def\u5f84\n    driver = webdriver.Chrome(service=service, options=chrome_options)\n\n    # \u6253\u5f00\u7b2c\u4e09\u65b9\u6ce8\u518c\u9875\u9762\n    driver.get(\"https:\/\/third-party-website.com\/register\")\n\n    # \u5728\u6ce8\u518c\u9875\u9762\u586b\u5199\u8868\u5355\n    username_input = driver.find_element(By.ID, \"username\")\n    username_input.send_keys(username)\n\n    password_input = driver.find_element(By.ID, \"password\")\n    password_input.send_keys(password)\n\n    # \u63d0\u4ea4\u8868\u5355\n    submit_button = driver.find_element(By.ID, \"submit\")\n    submit_button.click()\n\n    # \u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u5e76\u83b7\u53d6\u7ed3\u679c\n    driver.implicitly_wait(10)  # \u7b49\u5f85 10 \u79d2\u949f\n    result_element = driver.find_element(By.ID, \"result\")\n    result = result_element.text\n\n    # \u5173\u95ed\u6d4f\u89c8\u5668\n    driver.quit()\n\n    return {\"message\": result}\n\nif __name__ == \"__main__\":\n    import uvicorn\n    uvicorn.run(app, host=\"0.0.0.0\", port=8000)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u7b2c\u4e09\u65b9\u6ce8\u518c\u7ed3\u679c\u4fe1\u606f\u5b58\u50a8\u5728 <code>result<\/code> \u53d8\u91cf\u4e2d\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a JSON \u6570\u636e\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002\u5ba2\u6237\u7aef\u5c06\u6536\u5230\u5305\u542b\u6ce8\u518c\u7ed3\u679c\u4fe1\u606f\u7684 JSON \u54cd\u5e94\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf7\u786e\u4fdd\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u63d0\u4f9b\u6b63\u786e\u7684 WebDriver \u8def\u5f84\uff0c\u5e76\u6839\u636e\u76ee\u6807\u7f51\u7ad9\u7684\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u76f8\u5e94\u7684\u4fee\u6539\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u4f60\u5e0c\u671b\u5728 FastAPI \u4e2d\u8fd4\u56de\u7b2c\u4e09\u65b9\u6ce8\u518c\u7684\u7ed3\u679c\u4fe1\u606f\uff0c\u4f60\u53ef\u4ee5\u5c06\u7ed3\u679c\u4fe1\u606f\u4f5c\u4e3a JSON \u6570\u636e\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002\u4ee5 [&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-29","post","type-post","status-publish","format-standard","hentry","category-suibianjilu"],"_links":{"self":[{"href":"https:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":1,"href":"https:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":30,"href":"https:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions\/30"}],"wp:attachment":[{"href":"https:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}