{"id":174101,"date":"2024-05-08T18:25:02","date_gmt":"2024-05-08T10:25:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/174101.html"},"modified":"2024-05-08T18:25:10","modified_gmt":"2024-05-08T10:25:10","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87python%e6%8f%90%e5%8f%96%e7%bd%91%e9%a1%b5%e5%ae%9e%e6%97%b6%e6%b1%87%e7%8e%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/174101.html","title":{"rendered":"\u5982\u4f55\u901a\u8fc7Python\u63d0\u53d6\u7f51\u9875\u5b9e\u65f6\u6c47\u7387"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27050246\/c3466b61-7b36-4fc4-b5c3-a6f6aaacdb41.webp\" alt=\"\u5982\u4f55\u901a\u8fc7Python\u63d0\u53d6\u7f51\u9875\u5b9e\u65f6\u6c47\u7387\" \/><\/p>\n<p><p>\u8981\u901a\u8fc7Python\u63d0\u53d6\u7f51\u9875\u5b9e\u65f6\u6c47\u7387\uff0c\u53ef\u4ee5<strong>\u4f7f\u7528\u8bf7\u6c42\u5e93\uff08\u5982requests\uff09\u53d1\u8d77\u7f51\u7edc\u8bf7\u6c42\uff0c\u518d\u901a\u8fc7\u89e3\u6790\u5e93\uff08\u5982BeautifulSoup\u6216lxml\uff09\u89e3\u6790HTML\u5185\u5bb9<\/strong>\uff0c\u6700\u540e\u4ece\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6c47\u7387\u6570\u636e\u3002\u5728\u5c55\u5f00\u63cf\u8ff0\u4e2d\uff0c\u4ee5<strong>requests\u5e93<\/strong>\u4e3a\u4f8b\uff0c\u5b83\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684HTTP\u5ba2\u6237\u7aef\u5e93\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u4ee5\u4e00\u4e2aPythonic\u7684\u65b9\u5f0f\u53d1\u9001HTTP\u8bf7\u6c42\u3002\u6211\u4eec\u9700\u8981\u9996\u5148\u5b89\u88c5\u8be5\u5e93\uff08\u4f7f\u7528pip install requests\uff09\uff0c\u7136\u540e\u901a\u8fc7GET\u65b9\u6cd5\u83b7\u53d6\u76ee\u6807\u7f51\u9875\u7684\u5185\u5bb9\u3002\u6b64\u5e93\u81ea\u52a8\u5904\u7406URL\u7f16\u7801\u3001HTTP\u5934\u90e8\u7b49\u7ec6\u8282\uff0c\u6781\u5927\u7b80\u5316\u4e86\u7f51\u7edc\u8bf7\u6c42\u7684\u590d\u6742\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8bbe\u7f6ePython\u73af\u5883\u548c\u5b89\u88c5\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fddPython\u73af\u5883\u5df2\u6b63\u786e\u8bbe\u7f6e\uff0c\u5e76\u5b89\u88c5\u4e86requests\u548cBeautifulSoup\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install requests<\/p>\n<p>pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528requests\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_html(url):<\/p>\n<p>    try:<\/p>\n<p>        response = requests.get(url)<\/p>\n<p>        response.r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se_for_status()<\/p>\n<p>        response.encoding = response.apparent_encoding<\/p>\n<p>        return response.text<\/p>\n<p>    except requests.HTTPError as http_err:<\/p>\n<p>        print(f&#039;HTTP error occurred: {http_err}&#039;)<\/p>\n<p>    except Exception as err:<\/p>\n<p>        print(f&#039;An error occurred: {err}&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>get_html<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2aURL\u4f5c\u4e3a\u53c2\u6570\uff0c\u4f7f\u7528requests\u53d1\u8d77GET\u8bf7\u6c42\uff0c\u5982\u679c\u8bf7\u6c42\u6210\u529f\uff0c\u8fd4\u56de\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>def parse_html(html):<\/p>\n<p>    soup = BeautifulSoup(html, &#039;html.parser&#039;)<\/p>\n<p>    # \u6839\u636e\u5b9e\u9645\u7f51\u9875\u7ed3\u6784\u63d0\u53d6\u6c47\u7387\u6570\u636e<\/p>\n<p>    return rates<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u51fd\u6570<code>parse_html<\/code>\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528BeautifulSoup\u6765\u89e3\u6790\u8fd4\u56de\u7684html\u5185\u5bb9\u5e76\u63d0\u53d6\u5b9e\u65f6\u6c47\u7387\u3002\u7f51\u9875\u7684\u7ed3\u6784\u4e0d\u540c\uff0c\u63d0\u53d6\u7684\u65b9\u6cd5\u4e5f\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u4e00\u822c\u662f\u5b9a\u4f4d\u5230\u5305\u542b\u6c47\u7387\u4fe1\u606f\u7684\u6807\u7b7e\uff0c\u7136\u540e\u83b7\u53d6\u5176\u6587\u672c\u5185\u5bb9\u6216\u5c5e\u6027\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ec4\u5408\u5b8c\u6574\u7684\u6570\u636e\u63d0\u53d6\u51fd\u6570<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def get_exchange_rates(url):<\/p>\n<p>    html = get_html(url)<\/p>\n<p>    if html:<\/p>\n<p>        rates = parse_html(html)<\/p>\n<p>        return rates<\/p>\n<p>    return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u7ec4\u5408\u8d77\u6765\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86<code>get_exchange_rates<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u5c06\u7f51\u9875\u5730\u5740\u4f20\u9012\u7ed9<code>get_html<\/code>\uff0c\u7136\u540e\u5c06\u83b7\u53d6\u7684HTML\u5185\u5bb9\u4f20\u9012\u7ed9<code>parse_html<\/code>\u89e3\u6790\uff0c\u6700\u7ec8\u83b7\u5f97\u5b9e\u65f6\u6c47\u7387\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8fd0\u884c\u548c\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u4e00\u5207\u8fd0\u8f6c\u6b63\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u5b9e\u9645\u7684\u7f51\u9875\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5e76\u6253\u5370\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if __name__ == &#039;__main__&#039;:<\/p>\n<p>    url = &#039;https:\/\/www.x-rates.com\/&#039;  # \u793a\u4f8b\u7f51\u5740<\/p>\n<p>    rates = get_exchange_rates(url)<\/p>\n<p>    if rates:<\/p>\n<p>        print(rates)<\/p>\n<p>    else:<\/p>\n<p>        print(&#039;Failed to retrieve exchange rates.&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u53ea\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5177\u4f53\u7684URL\u6839\u636e\u5b9e\u9645\u7f51\u9875\u8fdb\u884c\u66ff\u6362\u3002\u5b9e\u73b0\u4ee5\u4e0a\u6b65\u9aa4\u540e\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7Python\u63d0\u53d6\u7f51\u9875\u4e0a\u7684\u5b9e\u65f6\u6c47\u7387\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u7f51\u9875\u5b9e\u65f6\u6c47\u7387\u5982\u4f55\u88ab\u63d0\u53d6\u5230Python\u4e2d\uff1f<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528Python\u7684\u7f51\u7edc\u722c\u866b\u5e93\uff0c\u53ef\u4ee5\u4ece\u5916\u90e8\u7f51\u7ad9\u4e0a\u63d0\u53d6\u5b9e\u65f6\u6c47\u7387\u6570\u636e\u3002\u5176\u4e2d\uff0c\u5e38\u7528\u7684\u7f51\u7edc\u722c\u866b\u5e93\u5305\u62ecBeautifulSoup\u3001Scrapy\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u5e93\u4e2d\u7684\u65b9\u6cd5\u548c\u51fd\u6570\uff0c\u901a\u8fc7\u6307\u5b9aURL\u548c\u63d0\u53d6\u89c4\u5219\uff0c\u4ece\u7f51\u9875\u4e2d\u83b7\u53d6\u6240\u9700\u7684\u5b9e\u65f6\u6c47\u7387\u6570\u636e\u3002<\/p>\n<p><strong>2. \u4f7f\u7528Python\u63d0\u53d6\u7684\u7f51\u9875\u5b9e\u65f6\u6c47\u7387\u53ef\u4ee5\u5982\u4f55\u5904\u7406\uff1f<\/strong><\/p>\n<p>\u4e00\u65e6\u4f7f\u7528Python\u6210\u529f\u63d0\u53d6\u5230\u7f51\u9875\u4e0a\u7684\u5b9e\u65f6\u6c47\u7387\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u4fdd\u5b58\u5230\u672c\u5730\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u4ee5\u5907\u540e\u7eed\u4f7f\u7528\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5bf9\u83b7\u53d6\u5230\u7684\u6c47\u7387\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u548c\u5206\u6790\uff0c\u4f8b\u5982\u8ba1\u7b97\u6c47\u7387\u7684\u5747\u503c\u3001\u6700\u5927\u503c\u3001\u6700\u5c0f\u503c\u7b49\u7edf\u8ba1\u4fe1\u606f\uff0c\u6216\u8005\u8fdb\u884c\u53ef\u89c6\u5316\u5c55\u793a\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u52a8\u6001\u7684\u5b9e\u65f6\u6c47\u7387\u5237\u65b0\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u60f3\u8981\u5b9e\u73b0\u52a8\u6001\u7684\u5b9e\u65f6\u6c47\u7387\u5237\u65b0\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b9a\u65f6\u4efb\u52a1\u5e93\uff0c\u5982APScheduler\u3001schedule\u7b49\u3002\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1\uff0c\u5b9a\u671f\u6267\u884c\u83b7\u53d6\u7f51\u9875\u5b9e\u65f6\u6c47\u7387\u7684\u4ee3\u7801\uff0c\u4ece\u800c\u5b9e\u73b0\u6570\u636e\u7684\u81ea\u52a8\u66f4\u65b0\u3002\u540c\u65f6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u7f16\u5199\u5faa\u73af\u7684\u4ee3\u7801\uff0c\u8ba9\u7a0b\u5e8f\u5728\u540e\u53f0\u4e00\u76f4\u8fd0\u884c\u5e76\u4e0d\u65ad\u83b7\u53d6\u6700\u65b0\u7684\u6c47\u7387\u6570\u636e\uff0c\u4ee5\u4fdd\u6301\u5b9e\u65f6\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u901a\u8fc7Python\u63d0\u53d6\u7f51\u9875\u5b9e\u65f6\u6c47\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528\u8bf7\u6c42\u5e93\uff08\u5982requests\uff09\u53d1\u8d77\u7f51\u7edc\u8bf7\u6c42\uff0c\u518d\u901a\u8fc7\u89e3\u6790\u5e93\uff08\u5982Beau [&hellip;]","protected":false},"author":3,"featured_media":174113,"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\/174101"}],"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=174101"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/174101\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/174113"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=174101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=174101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=174101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}