{"id":1165738,"date":"2025-01-15T15:24:03","date_gmt":"2025-01-15T07:24:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1165738.html"},"modified":"2025-01-15T15:24:06","modified_gmt":"2025-01-15T07:24:06","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%81%9a%e6%af%94%e5%88%86%e7%bd%91","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1165738.html","title":{"rendered":"\u5982\u4f55\u7528python\u505a\u6bd4\u5206\u7f51"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25210022\/f2c6f768-0452-4e4b-b4ec-37b91effbbb0.webp\" alt=\"\u5982\u4f55\u7528python\u505a\u6bd4\u5206\u7f51\" \/><\/p>\n<p><p> <strong>\u7528Python\u505a\u6bd4\u5206\u7f51\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528Flask\/Django\u6846\u67b6\u6784\u5efaWeb\u5e94\u7528\u3001Scrapy\/BeautifulSoup\u722c\u53d6\u6570\u636e\u3001SQLite\/MySQL\u6570\u636e\u5e93\u5b58\u50a8\u6570\u636e\u3001\u5b9a\u65f6\u4efb\u52a1\u81ea\u52a8\u66f4\u65b0\u6570\u636e\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528Flask\u6846\u67b6\u6784\u5efaWeb\u5e94\u7528\u662f\u6700\u57fa\u7840\u4e5f\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\u3002Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python Web\u6846\u67b6\uff0c\u9002\u5408\u521d\u5b66\u8005\u5feb\u901f\u642d\u5efa\u7b80\u5355\u7684Web\u5e94\u7528\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Flask\u6846\u67b6\u6784\u5efaWeb\u5e94\u7528<\/h3>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python Web\u6846\u67b6\uff0c\u9002\u5408\u5feb\u901f\u6784\u5efaWeb\u5e94\u7528\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5Flask\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Flask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, render_template<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def home():<\/p>\n<p>    return render_template(&#39;index.html&#39;)<\/p>\n<p>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<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8def\u7531<code>\/<\/code>\uff0c\u8be5\u8def\u7531\u6307\u5411<code>home<\/code>\u51fd\u6570\uff0c\u5e76\u8fd4\u56de<code>index.html<\/code>\u6a21\u677f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001Scrapy\/BeautifulSoup\u722c\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u83b7\u53d6\u6bd4\u5206\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528Scrapy\u6216BeautifulSoup\u8fdb\u884c\u7f51\u9875\u6570\u636e\u722c\u53d6\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528BeautifulSoup\u722c\u53d6\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>url = &#39;https:\/\/example.com\/scores&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<p>scores = []<\/p>\n<p>for match in soup.find_all(&#39;div&#39;, class_=&#39;match-score&#39;):<\/p>\n<p>    team1 = match.find(&#39;span&#39;, class_=&#39;team1&#39;).text<\/p>\n<p>    team2 = match.find(&#39;span&#39;, class_=&#39;team2&#39;).text<\/p>\n<p>    score = match.find(&#39;span&#39;, class_=&#39;score&#39;).text<\/p>\n<p>    scores.append({&#39;team1&#39;: team1, &#39;team2&#39;: team2, &#39;score&#39;: score})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\uff0c\u4ece\u800c\u63d0\u53d6\u6bd4\u5206\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001SQLite\/MySQL\u6570\u636e\u5e93\u5b58\u50a8\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b58\u50a8\u722c\u53d6\u5230\u7684\u6bd4\u5206\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528SQLite\u6216MySQL\u6570\u636e\u5e93\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528SQLite\u5b58\u50a8\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;scores.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>cursor.execute(&#39;&#39;&#39;<\/p>\n<p>    CREATE TABLE IF NOT EXISTS scores (<\/p>\n<p>        id INTEGER PRIMARY KEY,<\/p>\n<p>        team1 TEXT,<\/p>\n<p>        team2 TEXT,<\/p>\n<p>        score TEXT<\/p>\n<p>    )<\/p>\n<p>&#39;&#39;&#39;)<\/p>\n<p>for score in scores:<\/p>\n<p>    cursor.execute(&#39;&#39;&#39;<\/p>\n<p>        INSERT INTO scores (team1, team2, score) VALUES (?, ?, ?)<\/p>\n<p>    &#39;&#39;&#39;, (score[&#39;team1&#39;], score[&#39;team2&#39;], score[&#39;score&#39;]))<\/p>\n<p>conn.commit()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aSQLite\u6570\u636e\u5e93\uff0c\u5e76\u521b\u5efa\u4e86<code>scores<\/code>\u8868\u7528\u4e8e\u5b58\u50a8\u6bd4\u5206\u6570\u636e\u3002\u7136\u540e\u5c06\u722c\u53d6\u5230\u7684\u6570\u636e\u63d2\u5165\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9a\u65f6\u4efb\u52a1\u81ea\u52a8\u66f4\u65b0\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u6301\u6bd4\u5206\u6570\u636e\u7684\u5b9e\u65f6\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b9a\u65f6\u4efb\u52a1\u81ea\u52a8\u66f4\u65b0\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528APScheduler\u5e93\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from apscheduler.schedulers.background import BackgroundScheduler<\/p>\n<p>def update_scores():<\/p>\n<p>    # \u722c\u53d6\u6570\u636e\u5e76\u5b58\u50a8\u5230\u6570\u636e\u5e93<\/p>\n<p>    pass<\/p>\n<p>scheduler = BackgroundScheduler()<\/p>\n<p>scheduler.add_job(update_scores, &#39;interval&#39;, minutes=10)<\/p>\n<p>scheduler.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>update_scores<\/code>\u51fd\u6570\u7528\u4e8e\u722c\u53d6\u6570\u636e\u5e76\u5b58\u50a8\u5230\u6570\u636e\u5e93\uff0c\u5e76\u4f7f\u7528APScheduler\u6bcf\u969410\u5206\u949f\u6267\u884c\u4e00\u6b21\u8be5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408Flask\u5c55\u793a\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5c06\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u7684\u6bd4\u5206\u6570\u636e\u5c55\u793a\u5728Flask Web\u5e94\u7528\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/scores&#39;)<\/p>\n<p>def scores():<\/p>\n<p>    conn = sqlite3.connect(&#39;scores.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&#39;SELECT team1, team2, score FROM scores&#39;)<\/p>\n<p>    scores = cursor.fetchall()<\/p>\n<p>    conn.close()<\/p>\n<p>    return render_template(&#39;scores.html&#39;, scores=scores)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8def\u7531<code>\/scores<\/code>\uff0c\u8be5\u8def\u7531\u6307\u5411<code>scores<\/code>\u51fd\u6570\uff0c\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6bd4\u5206\u6570\u636e\uff0c\u5e76\u8fd4\u56de<code>scores.html<\/code>\u6a21\u677f\u8fdb\u884c\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5305\u62ecFlask Web\u5e94\u7528\u3001\u6570\u636e\u722c\u53d6\u3001\u6570\u636e\u5e93\u5b58\u50a8\u548c\u5b9a\u65f6\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, render_template<\/p>\n<p>import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import sqlite3<\/p>\n<p>from apscheduler.schedulers.background import BackgroundScheduler<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>def update_scores():<\/p>\n<p>    url = &#39;https:\/\/example.com\/scores&#39;<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<p>    scores = []<\/p>\n<p>    for match in soup.find_all(&#39;div&#39;, class_=&#39;match-score&#39;):<\/p>\n<p>        team1 = match.find(&#39;span&#39;, class_=&#39;team1&#39;).text<\/p>\n<p>        team2 = match.find(&#39;span&#39;, class_=&#39;team2&#39;).text<\/p>\n<p>        score = match.find(&#39;span&#39;, class_=&#39;score&#39;).text<\/p>\n<p>        scores.append({&#39;team1&#39;: team1, &#39;team2&#39;: team2, &#39;score&#39;: score})<\/p>\n<p>    conn = sqlite3.connect(&#39;scores.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&#39;&#39;&#39;<\/p>\n<p>        CREATE TABLE IF NOT EXISTS scores (<\/p>\n<p>            id INTEGER PRIMARY KEY,<\/p>\n<p>            team1 TEXT,<\/p>\n<p>            team2 TEXT,<\/p>\n<p>            score TEXT<\/p>\n<p>        )<\/p>\n<p>    &#39;&#39;&#39;)<\/p>\n<p>    for score in scores:<\/p>\n<p>        cursor.execute(&#39;&#39;&#39;<\/p>\n<p>            INSERT INTO scores (team1, team2, score) VALUES (?, ?, ?)<\/p>\n<p>        &#39;&#39;&#39;, (score[&#39;team1&#39;], score[&#39;team2&#39;], score[&#39;score&#39;]))<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>scheduler = BackgroundScheduler()<\/p>\n<p>scheduler.add_job(update_scores, &#39;interval&#39;, minutes=10)<\/p>\n<p>scheduler.start()<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def home():<\/p>\n<p>    return render_template(&#39;index.html&#39;)<\/p>\n<p>@app.route(&#39;\/scores&#39;)<\/p>\n<p>def scores():<\/p>\n<p>    conn = sqlite3.connect(&#39;scores.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&#39;SELECT team1, team2, score FROM scores&#39;)<\/p>\n<p>    scores = cursor.fetchall()<\/p>\n<p>    conn.close()<\/p>\n<p>    return render_template(&#39;scores.html&#39;, scores=scores)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6bd4\u5206\u7f51\u3002\u9996\u5148\u4f7f\u7528Flask\u6846\u67b6\u6784\u5efaWeb\u5e94\u7528\uff0c\u7136\u540e\u4f7f\u7528BeautifulSoup\u722c\u53d6\u6bd4\u5206\u6570\u636e\uff0c\u5e76\u5c06\u6570\u636e\u5b58\u50a8\u5230SQLite\u6570\u636e\u5e93\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7\u5b9a\u65f6\u4efb\u52a1\u81ea\u52a8\u66f4\u65b0\u6570\u636e\uff0c\u5e76\u5728Flask Web\u5e94\u7528\u4e2d\u5c55\u793a\u6bd4\u5206\u6570\u636e\u3002\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u6bd4\u5206\u7f51\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u521b\u5efa\u4e00\u4e2a\u5b9e\u65f6\u6bd4\u5206\u7f51\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u521b\u5efa\u5b9e\u65f6\u6bd4\u5206\u7f51\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Flask\u6216Django\u7b49\u6846\u67b6\u6765\u642d\u5efa\u540e\u7aef\uff0c\u5229\u7528WebSocket\u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u66f4\u65b0\u3002\u540c\u65f6\uff0c\u7ed3\u5408API\u83b7\u53d6\u6bd4\u8d5b\u6570\u636e\uff0c\u53ef\u4ee5\u5c55\u793a\u5b9e\u65f6\u6bd4\u5206\u3001\u8d5b\u7a0b\u5b89\u6392\u7b49\u4fe1\u606f\u3002\u901a\u8fc7\u524d\u7aef\u6280\u672f\u5982HTML\u3001CSS\u548cJavaScript\uff0c\u53ef\u4ee5\u7f8e\u5316\u754c\u9762\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u83b7\u53d6\u4f53\u80b2\u6bd4\u8d5b\u6570\u636e\uff1f<\/strong><br \/>\u83b7\u53d6\u4f53\u80b2\u6bd4\u8d5b\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7b2c\u4e09\u65b9API\uff0c\u4f8b\u5982SportsRadar\u6216TheSportsDB\u7b49\u3002\u8fd9\u4e9bAPI\u901a\u5e38\u63d0\u4f9b\u4e30\u5bcc\u7684\u6bd4\u8d5b\u4fe1\u606f\uff0c\u5305\u62ec\u6bd4\u5206\u3001\u7403\u961f\u548c\u7403\u5458\u7edf\u8ba1\u6570\u636e\u3002\u4f7f\u7528Python\u7684requests\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u6240\u9700\u7684\u6570\u636e\uff0c\u5e76\u5c06\u5176\u5904\u7406\u540e\u5c55\u793a\u5728\u6bd4\u5206\u7f51\u4e2d\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316Python\u5199\u7684\u6bd4\u5206\u7f51\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u4e3a\u4e86\u4f18\u5316Python\u7f16\u5199\u7684\u6bd4\u5206\u7f51\u6027\u80fd\uff0c\u53ef\u4ee5\u91c7\u53d6\u591a\u79cd\u63aa\u65bd\u3002\u9996\u5148\uff0c\u8003\u8651\u4f7f\u7528\u7f13\u5b58\u673a\u5236\uff0c\u4f8b\u5982Redis\uff0c\u4ee5\u51cf\u5c11\u5bf9\u6570\u636e\u5e93\u7684\u9891\u7e41\u8bbf\u95ee\u3002\u5176\u6b21\uff0c\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b\uff08\u5982asyncio\uff09\u6765\u5904\u7406\u5e76\u53d1\u8bf7\u6c42\uff0c\u63d0\u9ad8\u54cd\u5e94\u901f\u5ea6\u3002\u6b64\u5916\uff0c\u4f18\u5316\u6570\u636e\u5e93\u67e5\u8be2\uff0c\u786e\u4fdd\u7d22\u5f15\u7684\u5408\u7406\u4f7f\u7528\uff0c\u4e5f\u662f\u63d0\u5347\u6027\u80fd\u7684\u91cd\u8981\u65b9\u9762\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u505a\u6bd4\u5206\u7f51\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528Flask\/Django\u6846\u67b6\u6784\u5efaWeb\u5e94\u7528\u3001Scrapy\/Beau [&hellip;]","protected":false},"author":3,"featured_media":1165745,"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\/1165738"}],"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=1165738"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165738\/revisions"}],"predecessor-version":[{"id":1165746,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165738\/revisions\/1165746"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1165745"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1165738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1165738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1165738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}