{"id":1182962,"date":"2025-01-15T19:09:17","date_gmt":"2025-01-15T11:09:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182962.html"},"modified":"2025-01-15T19:09:20","modified_gmt":"2025-01-15T11:09:20","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%86%99%e6%9f%a5%e8%af%be%e8%bd%af%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1182962.html","title":{"rendered":"\u5982\u4f55\u7528python\u5199\u67e5\u8bfe\u8f6f\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25132903\/9ebece41-3239-42bb-b596-060dfc207632.webp\" alt=\"\u5982\u4f55\u7528python\u5199\u67e5\u8bfe\u8f6f\u4ef6\" \/><\/p>\n<p><p> \u8981\u7528Python\u7f16\u5199\u4e00\u4e2a\u67e5\u8bfe\u8f6f\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u6280\u672f\u548c\u5e93\u6765\u5b9e\u73b0\u3002<strong>\u9700\u8981\u4e86\u89e3\u76ee\u6807\u7f51\u7ad9\u7684\u7ed3\u6784\u548c\u6570\u636e\u6e90\u3001\u4f7f\u7528\u7f51\u7edc\u8bf7\u6c42\u5e93\u83b7\u53d6\u6570\u636e\u3001\u89e3\u6790HTML\u6570\u636e\u83b7\u53d6\u9700\u8981\u7684\u4fe1\u606f\u3001\u5904\u7406\u548c\u5c55\u793a\u6570\u636e\u3002<\/strong>\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5b9e\u73b0\u8fd9\u4e9b\u6b65\u9aa4\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4e86\u89e3\u76ee\u6807\u7f51\u7ad9\u7684\u7ed3\u6784\u548c\u6570\u636e\u6e90<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u67e5\u8bfe\u8f6f\u4ef6\u4e4b\u524d\uff0c\u9996\u5148\u8981\u660e\u786e\u76ee\u6807\u7f51\u7ad9\u7684\u7ed3\u6784\u548c\u6570\u636e\u6e90\u3002\u76ee\u6807\u7f51\u7ad9\u901a\u5e38\u662f\u5b66\u6821\u7684\u6559\u52a1\u7cfb\u7edf\u6216\u8bfe\u7a0b\u8868\u7f51\u7ad9\u3002\u901a\u8fc7\u6d4f\u89c8\u5668\u7684\u5f00\u53d1\u8005\u5de5\u5177\u53ef\u4ee5\u67e5\u770b\u9875\u9762\u7684HTML\u7ed3\u6784\u3001\u7f51\u7edc\u8bf7\u6c42\u548c\u54cd\u5e94\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u67e5\u770b\u9875\u9762HTML\u7ed3\u6784<\/strong>\uff1a\u901a\u8fc7\u6d4f\u89c8\u5668\u7684\u201c\u67e5\u770b\u6e90\u4ee3\u7801\u201d\u529f\u80fd\uff0c\u53ef\u4ee5\u770b\u5230\u9875\u9762\u7684HTML\u7ed3\u6784\uff0c\u4e86\u89e3\u6570\u636e\u7684\u4f4d\u7f6e\u3002<\/li>\n<li><strong>\u67e5\u770b\u7f51\u7edc\u8bf7\u6c42<\/strong>\uff1a\u4f7f\u7528\u6d4f\u89c8\u5668\u7684\u201c\u5f00\u53d1\u8005\u5de5\u5177\u201d\u53ef\u4ee5\u67e5\u770b\u9875\u9762\u52a0\u8f7d\u8fc7\u7a0b\u4e2d\u53d1\u51fa\u7684\u7f51\u7edc\u8bf7\u6c42\uff0c\u627e\u5230\u83b7\u53d6\u8bfe\u7a0b\u6570\u636e\u7684API\u63a5\u53e3\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7f51\u7edc\u8bf7\u6c42\u5e93\u83b7\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u7528\u4e8e\u7f51\u7edc\u8bf7\u6c42\u7684\u5e93\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f<code>requests<\/code>\u5e93\u3002\u8fd9\u4e2a\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u53d1\u9001HTTP\u8bf7\u6c42\u548c\u63a5\u6536\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def fetch_course_data(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        return response.text<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790HTML\u6570\u636e\u83b7\u53d6\u9700\u8981\u7684\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u5230HTML\u6570\u636e\u540e\uff0c\u9700\u8981\u89e3\u6790\u5176\u4e2d\u7684\u8bfe\u7a0b\u4fe1\u606f\u3002Python\u4e2d\u5e38\u7528\u7684HTML\u89e3\u6790\u5e93\u662f<code>BeautifulSoup<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>def parse_course_data(html):<\/p>\n<p>    soup = BeautifulSoup(html, &#39;html.parser&#39;)<\/p>\n<p>    courses = []<\/p>\n<p>    # \u6839\u636eHTML\u7ed3\u6784\u63d0\u53d6\u8bfe\u7a0b\u4fe1\u606f\uff0c\u8fd9\u91cc\u5047\u8bbe\u8bfe\u7a0b\u4fe1\u606f\u5728&lt;table&gt;\u6807\u7b7e\u4e2d<\/p>\n<p>    table = soup.find(&#39;table&#39;, {&#39;id&#39;: &#39;course_table&#39;})<\/p>\n<p>    if table:<\/p>\n<p>        rows = table.find_all(&#39;tr&#39;)<\/p>\n<p>        for row in rows[1:]:  # \u8df3\u8fc7\u8868\u5934<\/p>\n<p>            cols = row.find_all(&#39;td&#39;)<\/p>\n<p>            course = {<\/p>\n<p>                &#39;name&#39;: cols[0].text,<\/p>\n<p>                &#39;time&#39;: cols[1].text,<\/p>\n<p>                &#39;location&#39;: cols[2].text,<\/p>\n<p>                &#39;instructor&#39;: cols[3].text<\/p>\n<p>            }<\/p>\n<p>            courses.append(course)<\/p>\n<p>    return courses<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u548c\u5c55\u793a\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u5230\u8bfe\u7a0b\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u9009\u62e9\u5c06\u5176\u4fdd\u5b58\u5230\u6587\u4ef6\u3001\u6570\u636e\u5e93\uff0c\u6216\u76f4\u63a5\u5728\u7ec8\u7aef\u8f93\u51fa\u3002\u8fd9\u91cc\u6211\u4eec\u9009\u62e9\u5728\u7ec8\u7aef\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def display_courses(courses):<\/p>\n<p>    for course in courses:<\/p>\n<p>        print(f&quot;\u8bfe\u7a0b\u540d\u79f0: {course[&#39;name&#39;]}&quot;)<\/p>\n<p>        print(f&quot;\u65f6\u95f4: {course[&#39;time&#39;]}&quot;)<\/p>\n<p>        print(f&quot;\u5730\u70b9: {course[&#39;location&#39;]}&quot;)<\/p>\n<p>        print(f&quot;\u6559\u5e08: {course[&#39;instructor&#39;]}&quot;)<\/p>\n<p>        print(&#39;-&#39; * 20)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b8c\u6574\u7684\u67e5\u8bfe\u8f6f\u4ef6\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u5c06\u4ee5\u4e0a\u6b65\u9aa4\u6574\u5408\u5230\u4e00\u4e2a\u5b8c\u6574\u7684\u7a0b\u5e8f\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>def fetch_course_data(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        return response.text<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<p>def parse_course_data(html):<\/p>\n<p>    soup = BeautifulSoup(html, &#39;html.parser&#39;)<\/p>\n<p>    courses = []<\/p>\n<p>    table = soup.find(&#39;table&#39;, {&#39;id&#39;: &#39;course_table&#39;})<\/p>\n<p>    if table:<\/p>\n<p>        rows = table.find_all(&#39;tr&#39;)<\/p>\n<p>        for row in rows[1:]:<\/p>\n<p>            cols = row.find_all(&#39;td&#39;)<\/p>\n<p>            course = {<\/p>\n<p>                &#39;name&#39;: cols[0].text,<\/p>\n<p>                &#39;time&#39;: cols[1].text,<\/p>\n<p>                &#39;location&#39;: cols[2].text,<\/p>\n<p>                &#39;instructor&#39;: cols[3].text<\/p>\n<p>            }<\/p>\n<p>            courses.append(course)<\/p>\n<p>    return courses<\/p>\n<p>def display_courses(courses):<\/p>\n<p>    for course in courses:<\/p>\n<p>        print(f&quot;\u8bfe\u7a0b\u540d\u79f0: {course[&#39;name&#39;]}&quot;)<\/p>\n<p>        print(f&quot;\u65f6\u95f4: {course[&#39;time&#39;]}&quot;)<\/p>\n<p>        print(f&quot;\u5730\u70b9: {course[&#39;location&#39;]}&quot;)<\/p>\n<p>        print(f&quot;\u6559\u5e08: {course[&#39;instructor&#39;]}&quot;)<\/p>\n<p>        print(&#39;-&#39; * 20)<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    url = &#39;http:\/\/example.com\/course_schedule&#39;<\/p>\n<p>    html = fetch_course_data(url)<\/p>\n<p>    if html:<\/p>\n<p>        courses = parse_course_data(html)<\/p>\n<p>        display_courses(courses)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Failed to fetch course data.&quot;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u73b0\u7528\u6237\u4ea4\u4e92\u548c\u589e\u5f3a\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u8f6f\u4ef6\u66f4\u52a0\u5b9e\u7528\uff0c\u53ef\u4ee5\u589e\u52a0\u7528\u6237\u4ea4\u4e92\u529f\u80fd\uff0c\u5982\u8f93\u5165URL\u3001\u9009\u62e9\u8f93\u51fa\u683c\u5f0f\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def main():<\/p>\n<p>    url = input(&quot;\u8bf7\u8f93\u5165\u8bfe\u7a0b\u8868URL: &quot;)<\/p>\n<p>    html = fetch_course_data(url)<\/p>\n<p>    if html:<\/p>\n<p>        courses = parse_course_data(html)<\/p>\n<p>        display_courses(courses)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Failed to fetch course data.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u53ef\u4ee5\u589e\u52a0\u5c06\u6570\u636e\u4fdd\u5b58\u5230CSV\u6587\u4ef6\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>def save_courses_to_csv(courses, filename):<\/p>\n<p>    with open(filename, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>        writer = csv.writer(file)<\/p>\n<p>        writer.writerow([&#39;\u8bfe\u7a0b\u540d\u79f0&#39;, &#39;\u65f6\u95f4&#39;, &#39;\u5730\u70b9&#39;, &#39;\u6559\u5e08&#39;])<\/p>\n<p>        for course in courses:<\/p>\n<p>            writer.writerow([course[&#39;name&#39;], course[&#39;time&#39;], course[&#39;location&#39;], course[&#39;instructor&#39;]])<\/p>\n<p>def main():<\/p>\n<p>    url = input(&quot;\u8bf7\u8f93\u5165\u8bfe\u7a0b\u8868URL: &quot;)<\/p>\n<p>    html = fetch_course_data(url)<\/p>\n<p>    if html:<\/p>\n<p>        courses = parse_course_data(html)<\/p>\n<p>        display_courses(courses)<\/p>\n<p>        save_option = input(&quot;\u662f\u5426\u5c06\u8bfe\u7a0b\u4fe1\u606f\u4fdd\u5b58\u5230CSV\u6587\u4ef6\uff1f(y\/n): &quot;)<\/p>\n<p>        if save_option.lower() == &#39;y&#39;:<\/p>\n<p>            filename = input(&quot;\u8bf7\u8f93\u5165\u6587\u4ef6\u540d: &quot;)<\/p>\n<p>            save_courses_to_csv(courses, filename)<\/p>\n<p>            print(f&quot;\u8bfe\u7a0b\u4fe1\u606f\u5df2\u4fdd\u5b58\u5230 {filename}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Failed to fetch course data.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u590d\u6742\u7684HTML\u7ed3\u6784\u548c\u52a8\u6001\u52a0\u8f7d<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u7f51\u7ad9\u7684\u8bfe\u7a0b\u6570\u636e\u53ef\u80fd\u662f\u901a\u8fc7JavaScript\u52a8\u6001\u52a0\u8f7d\u7684\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<code>requests<\/code>\u5e93\u65e0\u6cd5\u76f4\u63a5\u83b7\u53d6\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>Selenium<\/code>\u5e93\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u4ece\u800c\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5<code>Selenium<\/code>\u5e93\u548c\u6d4f\u89c8\u5668\u9a71\u52a8\uff08\u5982ChromeDriver\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>Selenium<\/code>\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u6570\u636e\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.chrome.service import Service<\/p>\n<p>from selenium.webdriver.chrome.options import Options<\/p>\n<p>def fetch_dynamic_course_data(url):<\/p>\n<p>    options = Options()<\/p>\n<p>    options.headless = True<\/p>\n<p>    service = Service(&#39;path\/to\/chromedriver&#39;)<\/p>\n<p>    driver = webdriver.Chrome(service=service, options=options)<\/p>\n<p>    driver.get(url)<\/p>\n<p>    # \u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5e76\u83b7\u53d6\u8bfe\u7a0b\u6570\u636e<\/p>\n<p>    driver.implicitly_wait(10)  # \u7b49\u5f8510\u79d2<\/p>\n<p>    html = driver.page_source<\/p>\n<p>    driver.quit()<\/p>\n<p>    return html<\/p>\n<p>def main():<\/p>\n<p>    url = input(&quot;\u8bf7\u8f93\u5165\u8bfe\u7a0b\u8868URL: &quot;)<\/p>\n<p>    html = fetch_dynamic_course_data(url)<\/p>\n<p>    if html:<\/p>\n<p>        courses = parse_course_data(html)<\/p>\n<p>        display_courses(courses)<\/p>\n<p>        save_option = input(&quot;\u662f\u5426\u5c06\u8bfe\u7a0b\u4fe1\u606f\u4fdd\u5b58\u5230CSV\u6587\u4ef6\uff1f(y\/n): &quot;)<\/p>\n<p>        if save_option.lower() == &#39;y&#39;:<\/p>\n<p>            filename = input(&quot;\u8bf7\u8f93\u5165\u6587\u4ef6\u540d: &quot;)<\/p>\n<p>            save_courses_to_csv(courses, filename)<\/p>\n<p>            print(f&quot;\u8bfe\u7a0b\u4fe1\u606f\u5df2\u4fdd\u5b58\u5230 {filename}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Failed to fetch course data.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u767b\u5f55\u8ba4\u8bc1<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u76ee\u6807\u7f51\u7ad9\u9700\u8981\u767b\u5f55\u8ba4\u8bc1\uff0c\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u7684\u4f1a\u8bdd\u529f\u80fd\u6216<code>Selenium<\/code>\u5e93\u6765\u6a21\u62df\u767b\u5f55\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>requests<\/code>\u5e93\u6a21\u62df\u767b\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def login_and_fetch_course_data(login_url, course_url, username, password):<\/p>\n<p>    session = requests.Session()<\/p>\n<p>    login_payload = {<\/p>\n<p>        &#39;username&#39;: username,<\/p>\n<p>        &#39;password&#39;: password<\/p>\n<p>    }<\/p>\n<p>    session.post(login_url, data=login_payload)<\/p>\n<p>    response = session.get(course_url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        return response.text<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<p>def main():<\/p>\n<p>    login_url = &#39;http:\/\/example.com\/login&#39;<\/p>\n<p>    course_url = &#39;http:\/\/example.com\/course_schedule&#39;<\/p>\n<p>    username = input(&quot;\u8bf7\u8f93\u5165\u7528\u6237\u540d: &quot;)<\/p>\n<p>    password = input(&quot;\u8bf7\u8f93\u5165\u5bc6\u7801: &quot;)<\/p>\n<p>    html = login_and_fetch_course_data(login_url, course_url, username, password)<\/p>\n<p>    if html:<\/p>\n<p>        courses = parse_course_data(html)<\/p>\n<p>        display_courses(courses)<\/p>\n<p>        save_option = input(&quot;\u662f\u5426\u5c06\u8bfe\u7a0b\u4fe1\u606f\u4fdd\u5b58\u5230CSV\u6587\u4ef6\uff1f(y\/n): &quot;)<\/p>\n<p>        if save_option.lower() == &#39;y&#39;:<\/p>\n<p>            filename = input(&quot;\u8bf7\u8f93\u5165\u6587\u4ef6\u540d: &quot;)<\/p>\n<p>            save_courses_to_csv(courses, filename)<\/p>\n<p>            print(f&quot;\u8bfe\u7a0b\u4fe1\u606f\u5df2\u4fdd\u5b58\u5230 {filename}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Failed to fetch course data.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>Selenium<\/code>\u6a21\u62df\u767b\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium.webdriver.common.keys import Keys<\/p>\n<p>def login_and_fetch_course_data(url, username, password):<\/p>\n<p>    options = Options()<\/p>\n<p>    options.headless = True<\/p>\n<p>    service = Service(&#39;path\/to\/chromedriver&#39;)<\/p>\n<p>    driver = webdriver.Chrome(service=service, options=options)<\/p>\n<p>    driver.get(url)<\/p>\n<p>    # \u6a21\u62df\u767b\u5f55\u64cd\u4f5c<\/p>\n<p>    username_field = driver.find_element(By.NAME, &#39;username&#39;)<\/p>\n<p>    password_field = driver.find_element(By.NAME, &#39;password&#39;)<\/p>\n<p>    username_field.send_keys(username)<\/p>\n<p>    password_field.send_keys(password)<\/p>\n<p>    password_field.send_keys(Keys.RETURN)<\/p>\n<p>    # \u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5e76\u83b7\u53d6\u8bfe\u7a0b\u6570\u636e<\/p>\n<p>    driver.implicitly_wait(10)<\/p>\n<p>    html = driver.page_source<\/p>\n<p>    driver.quit()<\/p>\n<p>    return html<\/p>\n<p>def main():<\/p>\n<p>    login_url = &#39;http:\/\/example.com\/login&#39;<\/p>\n<p>    username = input(&quot;\u8bf7\u8f93\u5165\u7528\u6237\u540d: &quot;)<\/p>\n<p>    password = input(&quot;\u8bf7\u8f93\u5165\u5bc6\u7801: &quot;)<\/p>\n<p>    html = login_and_fetch_course_data(login_url, username, password)<\/p>\n<p>    if html:<\/p>\n<p>        courses = parse_course_data(html)<\/p>\n<p>        display_courses(courses)<\/p>\n<p>        save_option = input(&quot;\u662f\u5426\u5c06\u8bfe\u7a0b\u4fe1\u606f\u4fdd\u5b58\u5230CSV\u6587\u4ef6\uff1f(y\/n): &quot;)<\/p>\n<p>        if save_option.lower() == &#39;y&#39;:<\/p>\n<p>            filename = input(&quot;\u8bf7\u8f93\u5165\u6587\u4ef6\u540d: &quot;)<\/p>\n<p>            save_courses_to_csv(courses, filename)<\/p>\n<p>            print(f&quot;\u8bfe\u7a0b\u4fe1\u606f\u5df2\u4fdd\u5b58\u5230 {filename}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Failed to fetch course data.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u8be6\u7ec6\u63cf\u8ff0\u4e86\u5982\u4f55\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u67e5\u8bfe\u8f6f\u4ef6\u3002\u9996\u5148\uff0c\u4e86\u89e3\u76ee\u6807\u7f51\u7ad9\u7684\u7ed3\u6784\u548c\u6570\u636e\u6e90\uff1b\u7136\u540e\uff0c\u4f7f\u7528\u7f51\u7edc\u8bf7\u6c42\u5e93\u83b7\u53d6\u6570\u636e\uff1b\u63a5\u7740\uff0c\u89e3\u6790HTML\u6570\u636e\u83b7\u53d6\u9700\u8981\u7684\u4fe1\u606f\uff1b\u6700\u540e\uff0c\u5904\u7406\u548c\u5c55\u793a\u6570\u636e\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u5982\u4f55\u5904\u7406\u590d\u6742\u7684HTML\u7ed3\u6784\u548c\u52a8\u6001\u52a0\u8f7d\u3001\u4ee5\u53ca\u5904\u7406\u767b\u5f55\u8ba4\u8bc1\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u7f16\u5199\u67e5\u8bfe\u8f6f\u4ef6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u9002\u5408\u7684Python\u5e93\u6765\u5f00\u53d1\u67e5\u8bfe\u8f6f\u4ef6\uff1f<\/strong><br \/>\u5728\u5f00\u53d1\u67e5\u8bfe\u8f6f\u4ef6\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684Python\u5e93\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u4f60\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Flask\u6216Django\u8fd9\u7c7bweb\u6846\u67b6\u6765\u6784\u5efa\u540e\u53f0\u670d\u52a1\uff0c\u5229\u7528Beautiful Soup\u6216Scrapy\u8fdb\u884c\u7f51\u9875\u6570\u636e\u6293\u53d6\u3002\u6b64\u5916\uff0cPandas\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002\u6839\u636e\u9700\u6c42\uff0c\u53ef\u4ee5\u7ed3\u5408\u8fd9\u4e9b\u5e93\u6765\u63d0\u5347\u5f00\u53d1\u6548\u7387\u548c\u8f6f\u4ef6\u6027\u80fd\u3002<\/p>\n<p><strong>\u5728\u5f00\u53d1\u67e5\u8bfe\u8f6f\u4ef6\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u7528\u6237\u754c\u9762\u7684\u53cb\u597d\u6027\uff1f<\/strong><br \/>\u7528\u6237\u754c\u9762\u7684\u53cb\u597d\u6027\u76f4\u63a5\u5f71\u54cd\u7528\u6237\u4f53\u9a8c\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u8ba1\u7b80\u6d01\u6e05\u6670\u7684\u5e03\u5c40\u3001\u4f7f\u7528\u76f4\u89c2\u7684\u5bfc\u822a\u83dc\u5355\u548c\u56fe\u6807\u6765\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002\u540c\u65f6\uff0c\u8003\u8651\u4f7f\u7528Bootstrap\u7b49\u524d\u7aef\u6846\u67b6\u6765\u5b9e\u73b0\u54cd\u5e94\u5f0f\u8bbe\u8ba1\uff0c\u4f7f\u8f6f\u4ef6\u5728\u4e0d\u540c\u8bbe\u5907\u4e0a\u90fd\u80fd\u826f\u597d\u8fd0\u884c\u3002\u5bf9\u7528\u6237\u53cd\u9988\u8fdb\u884c\u5b9a\u671f\u5206\u6790\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6301\u7eed\u4f18\u5316\u754c\u9762\u3002<\/p>\n<p><strong>\u67e5\u8bfe\u8f6f\u4ef6\u9700\u8981\u54ea\u4e9b\u57fa\u7840\u529f\u80fd\u6765\u6ee1\u8db3\u7528\u6237\u9700\u6c42\uff1f<\/strong><br \/>\u67e5\u8bfe\u8f6f\u4ef6\u7684\u57fa\u7840\u529f\u80fd\u5305\u62ec\u8bfe\u7a0b\u641c\u7d22\u3001\u65f6\u95f4\u8868\u67e5\u770b\u3001\u8bfe\u7a0b\u8be6\u60c5\u5c55\u793a\u4ee5\u53ca\u7528\u6237\u81ea\u5b9a\u4e49\u8bfe\u7a0b\u63d0\u9192\u7b49\u3002\u9664\u4e86\u8fd9\u4e9b\u57fa\u672c\u529f\u80fd\uff0c\u96c6\u6210\u8bc4\u8bba\u548c\u8bc4\u5206\u7cfb\u7edf\u53ef\u4ee5\u8ba9\u7528\u6237\u5206\u4eab\u4ed6\u4eec\u7684\u8bfe\u7a0b\u4f53\u9a8c\u3002\u6b64\u5916\uff0c\u8003\u8651\u63d0\u4f9bAPI\u63a5\u53e3\uff0c\u5141\u8bb8\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\u8bfe\u7a0b\u6570\u636e\uff0c\u4ece\u800c\u6269\u5c55\u8f6f\u4ef6\u7684\u4f7f\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u7f16\u5199\u4e00\u4e2a\u67e5\u8bfe\u8f6f\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u6280\u672f\u548c\u5e93\u6765\u5b9e\u73b0\u3002\u9700\u8981\u4e86\u89e3\u76ee\u6807\u7f51\u7ad9\u7684\u7ed3\u6784\u548c\u6570\u636e\u6e90\u3001\u4f7f\u7528\u7f51\u7edc\u8bf7\u6c42\u5e93\u83b7 [&hellip;]","protected":false},"author":3,"featured_media":1182971,"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\/1182962"}],"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=1182962"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182962\/revisions"}],"predecessor-version":[{"id":1182973,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182962\/revisions\/1182973"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1182971"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}