{"id":998712,"date":"2024-12-27T09:35:27","date_gmt":"2024-12-27T01:35:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/998712.html"},"modified":"2024-12-27T09:35:29","modified_gmt":"2024-12-27T01:35:29","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e8%ae%bf%e9%97%ae%e7%bd%91%e9%a1%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/998712.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528python\u8bbf\u95ee\u7f51\u9875"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074023\/8cc9f512-085c-4bce-a51b-e6a83622ccdc.webp\" alt=\"\u5982\u4f55\u4f7f\u7528python\u8bbf\u95ee\u7f51\u9875\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8bbf\u95ee\u7f51\u9875\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528<code>requests<\/code>\u5e93\u76f4\u63a5\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3001\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\u89e3\u6790\u7f51\u9875\u6570\u636e\u3001\u4f7f\u7528<code>selenium<\/code>\u8fdb\u884c\u52a8\u6001\u7f51\u9875\u7684\u4ea4\u4e92\u3002<\/strong>\u5176\u4e2d\uff0c<code>requests<\/code>\u5e93\u662f\u6700\u7b80\u5355\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u9759\u6001\u7f51\u9875\u7684\u8bbf\u95ee\u3002\u901a\u8fc7<code>requests.get()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\uff0c\u7136\u540e\u7ed3\u5408<code>BeautifulSoup<\/code>\u5e93\u89e3\u6790\u548c\u63d0\u53d6\u7279\u5b9a\u6570\u636e\u3002\u5bf9\u4e8e\u9700\u8981\u6a21\u62df\u7528\u6237\u884c\u4e3a\u7684\u52a8\u6001\u7f51\u9875\uff0c<code>selenium<\/code>\u5219\u662f\u66f4\u4e3a\u5408\u9002\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u63a7\u5236\u6d4f\u89c8\u5668\u5e76\u6267\u884cJavaScript\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>requests<\/code>\u5e93\u8bbf\u95ee\u7f51\u9875<\/p>\n<\/p>\n<p><p><code>requests<\/code>\u5e93\u662fPython\u4e2d\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u652f\u6301GET\u548cPOST\u8bf7\u6c42\uff0c\u5e76\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\u63a5\u53e3\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u57fa\u7840\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>requests<\/code>\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e4b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u53d1\u9001GET\u8bf7\u6c42\u5e76\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    print(&quot;Successfully accessed the webpage&quot;)<\/p>\n<p>    print(response.text)  # \u8f93\u51fa\u7f51\u9875\u5185\u5bb9<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to access webpage. Status code: {response.status_code}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u9996\u5148\u5bfc\u5165<code>requests<\/code>\u5e93\uff0c\u7136\u540e\u6307\u5b9a\u76ee\u6807URL\uff0c\u901a\u8fc7<code>requests.get()<\/code>\u65b9\u6cd5\u53d1\u9001GET\u8bf7\u6c42\uff0c\u6700\u540e\u901a\u8fc7<code>response.text<\/code>\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u8bf7\u6c42\u53c2\u6570\u548c\u5934\u4fe1\u606f<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u9700\u8981\u5728\u8bf7\u6c42\u4e2d\u9644\u52a0\u53c2\u6570\u6216\u4fee\u6539\u5934\u4fe1\u606f\u4ee5\u6a21\u62df\u6d4f\u89c8\u5668\u8bbf\u95ee\u3002<code>requests<\/code>\u5e93\u5141\u8bb8\u901a\u8fc7<code>params<\/code>\u548c<code>headers<\/code>\u53c2\u6570\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;http:\/\/example.com\/search&#39;<\/p>\n<p>params = {&#39;q&#39;: &#39;python&#39;}<\/p>\n<p>headers = {&#39;User-Agent&#39;: &#39;Mozilla\/5.0&#39;}<\/p>\n<p>response = requests.get(url, params=params, headers=headers)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    print(response.url)  # \u6253\u5370\u5b9e\u9645\u8bf7\u6c42\u7684URL<\/p>\n<p>    print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>params<\/code>\u53c2\u6570\u6dfb\u52a0\u67e5\u8be2\u5b57\u7b26\u4e32\uff0c\u5e76\u901a\u8fc7<code>headers<\/code>\u6a21\u62df\u6d4f\u89c8\u5668\u7684User-Agent\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>BeautifulSoup<\/code>\u89e3\u6790\u7f51\u9875<\/p>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u540e\uff0c<code>BeautifulSoup<\/code>\u5e93\u53ef\u4ee5\u5e2e\u52a9\u89e3\u6790\u548c\u63d0\u53d6\u7279\u5b9a\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u57fa\u7840\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5<code>BeautifulSoup<\/code>\u5e93\u53ca\u5176\u4f9d\u8d56\u7684\u89e3\u6790\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u89e3\u6790HTML\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html_content = &#39;&lt;html&gt;&lt;head&gt;&lt;title&gt;Test&lt;\/title&gt;&lt;\/head&gt;&lt;body&gt;&lt;h1&gt;Hello, World!&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;&#39;<\/p>\n<p>soup = BeautifulSoup(html_content, &#39;lxml&#39;)<\/p>\n<p>print(soup.title.text)  # \u8f93\u51fa: Test<\/p>\n<p>print(soup.h1.text)     # \u8f93\u51fa: Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>BeautifulSoup<\/code>\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u6cd5\u6765\u67e5\u627e\u548c\u63d0\u53d6HTML\u5143\u7d20\uff0c\u4f8b\u5982\u901a\u8fc7\u6807\u7b7e\u540d\u3001CSS\u9009\u62e9\u5668\u6216\u5c5e\u6027\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u67e5\u627e\u7279\u5b9a\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>BeautifulSoup<\/code>\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u67e5\u627e\u7279\u5b9a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html_content = &#39;&lt;div class=&quot;content&quot;&gt;&lt;p id=&quot;para1&quot;&gt;Paragraph 1&lt;\/p&gt;&lt;p id=&quot;para2&quot;&gt;Paragraph 2&lt;\/p&gt;&lt;\/div&gt;&#39;<\/p>\n<p>soup = BeautifulSoup(html_content, &#39;lxml&#39;)<\/p>\n<h2><strong>\u901a\u8fc7\u6807\u7b7e\u540d\u67e5\u627e<\/strong><\/h2>\n<p>paragraphs = soup.find_all(&#39;p&#39;)<\/p>\n<p>for para in paragraphs:<\/p>\n<p>    print(para.text)<\/p>\n<h2><strong>\u901a\u8fc7CSS\u9009\u62e9\u5668\u67e5\u627e<\/strong><\/h2>\n<p>para1 = soup.select_one(&#39;#para1&#39;)<\/p>\n<p>print(para1.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>find_all()<\/code>\u65b9\u6cd5\u8fd4\u56de\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\uff0c\u800c<code>select_one()<\/code>\u65b9\u6cd5\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>selenium<\/code>\u8fdb\u884c\u52a8\u6001\u7f51\u9875\u4ea4\u4e92<\/p>\n<\/p>\n<p><p><code>selenium<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316Web\u6d4f\u89c8\u5668\u64cd\u4f5c\u7684\u5de5\u5177\uff0c\u9002\u5408\u5904\u7406\u9700\u8981JavaScript\u6e32\u67d3\u7684\u52a8\u6001\u7f51\u9875\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u57fa\u7840\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5<code>selenium<\/code>\u53ca\u5176\u6d4f\u89c8\u5668\u9a71\u52a8\u7a0b\u5e8f\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>\u4e0b\u8f7dChromeDriver\u5e76\u786e\u4fdd\u5176\u8def\u5f84\u5728\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u542f\u52a8\u6d4f\u89c8\u5668\u5e76\u8bbf\u95ee\u7f51\u9875\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>driver = webdriver.Chrome()<\/p>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p>print(driver.title)  # \u8f93\u51fa\u7f51\u9875\u6807\u9898<\/p>\n<p>driver.quit()  # \u5173\u95ed\u6d4f\u89c8\u5668<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528<code>webdriver.Chrome()<\/code>\u542f\u52a8Chrome\u6d4f\u89c8\u5668\uff0c\u8bbf\u95ee\u6307\u5b9aURL\uff0c\u5e76\u8f93\u51fa\u7f51\u9875\u7684\u6807\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e0e\u7f51\u9875\u5143\u7d20\u4ea4\u4e92<\/strong><\/p>\n<\/p>\n<p><p><code>selenium<\/code>\u5141\u8bb8\u6a21\u62df\u7528\u6237\u64cd\u4f5c\uff0c\u5982\u70b9\u51fb\u6309\u94ae\u3001\u8f93\u5165\u6587\u672c\u7b49\u3002<\/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.common.keys import Keys<\/p>\n<p>driver = webdriver.Chrome()<\/p>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<h2><strong>\u627e\u5230\u641c\u7d22\u6846\u5e76\u8f93\u5165\u6587\u672c<\/strong><\/h2>\n<p>search_box = driver.find_element(By.NAME, &#39;q&#39;)<\/p>\n<p>search_box.send_keys(&#39;python&#39;)<\/p>\n<p>search_box.send_keys(Keys.RETURN)<\/p>\n<h2><strong>\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u4ee5\u4fbf\u9875\u9762\u52a0\u8f7d<\/strong><\/h2>\n<p>driver.implicitly_wait(10)<\/p>\n<h2><strong>\u83b7\u53d6\u641c\u7d22\u7ed3\u679c<\/strong><\/h2>\n<p>results = driver.find_elements(By.CLASS_NAME, &#39;result&#39;)<\/p>\n<p>for result in results:<\/p>\n<p>    print(result.text)<\/p>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u5728\u7f51\u9875\u4e2d\u67e5\u627e\u8f93\u5165\u6846\uff0c\u8f93\u5165\u641c\u7d22\u5173\u952e\u8bcd\u5e76\u83b7\u53d6\u641c\u7d22\u7ed3\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5904\u7406\u7f51\u9875\u8bbf\u95ee\u4e2d\u7684\u5e38\u89c1\u95ee\u9898<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u8fdb\u884c\u7f51\u9875\u8bbf\u95ee\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u5982\u8bf7\u6c42\u88ab\u62d2\u7edd\u3001\u9875\u9762\u52a0\u8f7d\u7f13\u6162\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bbe\u7f6e\u8d85\u65f6\u548c\u91cd\u8bd5\u673a\u5236<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8bf7\u6c42\u7f51\u9875\u65f6\uff0c\u8bbe\u7f6e\u8d85\u65f6\u53ef\u4ee5\u9632\u6b62\u7a0b\u5e8f\u957f\u65f6\u95f4\u7b49\u5f85\u65e0\u54cd\u5e94\u7684\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from requests.adapters import HTTPAdapter<\/p>\n<p>from requests.packages.urllib3.util.retry import Retry<\/p>\n<p>session = requests.Session()<\/p>\n<p>retries = Retry(total=5, backoff_factor=0.1, status_forcelist=[500, 502, 503, 504])<\/p>\n<p>session.mount(&#39;http:\/\/&#39;, HTTPAdapter(max_retries=retries))<\/p>\n<p>try:<\/p>\n<p>    response = session.get(&#39;http:\/\/example.com&#39;, timeout=5)<\/p>\n<p>    print(response.text)<\/p>\n<p>except requests.exceptions.RequestException as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u91cd\u8bd5\u673a\u5236\u548c\u8d85\u65f6\uff0c\u53ef\u4ee5\u63d0\u9ad8\u8bf7\u6c42\u7684\u7a33\u5b9a\u6027\u548c\u6210\u529f\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406JavaScript\u6e32\u67d3\u7684\u5185\u5bb9<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4f9d\u8d56JavaScript\u6e32\u67d3\u7684\u5185\u5bb9\uff0c\u4f7f\u7528<code>selenium<\/code>\u6216<code>requests-html<\/code>\u5e93\u66f4\u4e3a\u5408\u9002\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from requests_html import HTMLSession<\/p>\n<p>session = HTMLSession()<\/p>\n<p>response = session.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p>response.html.render()  # \u6e32\u67d3JavaScript<\/p>\n<p>print(response.html.html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>requests-html<\/code>\u5e93\u7ed3\u5408\u4e86<code>requests<\/code>\u548c\u6d4f\u89c8\u5668\u6e32\u67d3\u7684\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7b80\u5355JavaScript\u6267\u884c\u7684\u7f51\u9875\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u4f18\u5316\u7f51\u9875\u8bbf\u95ee\u7684\u6027\u80fd<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u7f51\u9875\u8bbf\u95ee\u4efb\u52a1\u65f6\uff0c\u4f18\u5316\u6027\u80fd\u662f\u5173\u952e\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u5f02\u6b65\u8bf7\u6c42<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0d\u4f9d\u8d56\u4e8e\u987a\u5e8f\u7684\u8bf7\u6c42\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65\u8bf7\u6c42\u5e93\u5982<code>aiohttp<\/code>\u6765\u63d0\u9ad8\u5e76\u53d1\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import aiohttp<\/p>\n<p>import asyncio<\/p>\n<p>async def fetch(url):<\/p>\n<p>    async with aiohttp.ClientSession() as session:<\/p>\n<p>        async with session.get(url) as response:<\/p>\n<p>            return await response.text()<\/p>\n<p>async def main():<\/p>\n<p>    urls = [&#39;http:\/\/example.com\/page1&#39;, &#39;http:\/\/example.com\/page2&#39;]<\/p>\n<p>    tasks = [fetch(url) for url in urls]<\/p>\n<p>    results = await asyncio.gather(*tasks)<\/p>\n<p>    for result in results:<\/p>\n<p>        print(result)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>aiohttp<\/code>\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6574\u4f53\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f13\u5b58\u8bf7\u6c42\u7ed3\u679c<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u51cf\u5c11\u91cd\u590d\u8bf7\u6c42\u5e26\u6765\u7684\u8d1f\u62c5\uff0c\u53ef\u4ee5\u5c06\u8bf7\u6c42\u7ed3\u679c\u7f13\u5b58\u4e0b\u6765\u3002\u4f7f\u7528<code>requests-cache<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests_cache<\/p>\n<p>requests_cache.install_cache(&#39;web_cache&#39;, expire_after=3600)  # \u7f13\u5b58\u4e00\u5c0f\u65f6<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p>print(response.from_cache)  # \u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6765\u81ea\u7f13\u5b58<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7f13\u5b58\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u5728\u591a\u6b21\u8bbf\u95ee\u76f8\u540c\u7f51\u9875\u65f6\u51cf\u5c11\u7f51\u7edc\u5e26\u5bbd\u7684\u6d88\u8017\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u8bbf\u95ee\u548c\u5904\u7406\u7f51\u9875\u5185\u5bb9\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u9759\u6001\u7f51\u9875\u8bbf\u95ee\uff0c\u8fd8\u662f\u590d\u6742\u7684\u52a8\u6001\u7f51\u9875\u4ea4\u4e92\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u5e93\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u7f51\u9875\u6570\u636e\u6293\u53d6\uff1f<\/strong><br \/>\u4f7f\u7528Python\u6293\u53d6\u7f51\u9875\u6570\u636e\u901a\u5e38\u9700\u8981\u4f9d\u8d56\u4e00\u4e9b\u5e93\uff0c\u6bd4\u5982<code>requests<\/code>\u548c<code>BeautifulSoup<\/code>\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>requests<\/code>\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u6765\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002\u63a5\u7740\uff0c\u5229\u7528<code>BeautifulSoup<\/code>\u89e3\u6790HTML\u6587\u6863\uff0c\u4ece\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u7f51\u9875\u4e0a\u7684\u6587\u672c\u3001\u94fe\u63a5\u3001\u56fe\u7247\u7b49\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u7f51\u9875\u7684\u5f02\u6b65\u52a0\u8f7d\u5185\u5bb9\uff1f<\/strong><br \/>\u8bb8\u591a\u73b0\u4ee3\u7f51\u7ad9\u4f7f\u7528JavaScript\u5f02\u6b65\u52a0\u8f7d\u6570\u636e\uff0c\u8fd9\u5c31\u5bfc\u81f4\u4f7f\u7528\u4f20\u7edf\u7684<code>requests<\/code>\u5e93\u65e0\u6cd5\u83b7\u53d6\u5230\u5b8c\u6574\u7684\u7f51\u9875\u5185\u5bb9\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Selenium<\/code>\u5e93\uff0c\u5b83\u6a21\u62df\u6d4f\u89c8\u5668\u7684\u884c\u4e3a\uff0c\u5141\u8bb8\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u540e\u518d\u6293\u53d6\u6570\u636e\u3002\u6b64\u5916\uff0c<code>Playwright<\/code>\u548c<code>Pyppeteer<\/code>\u4e5f\u662f\u5904\u7406\u5f02\u6b65\u5185\u5bb9\u7684\u4f18\u79c0\u9009\u62e9\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8bbf\u95ee\u7f51\u9875\u65f6\u5982\u4f55\u5904\u7406Cookies\u548c\u4f1a\u8bdd\uff1f<\/strong><br \/>\u5728\u8bbf\u95ee\u9700\u8981\u767b\u5f55\u6216\u4fdd\u6301\u4f1a\u8bdd\u7684\u7f51\u9875\u65f6\uff0c\u5904\u7406Cookies\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f7f\u7528<code>requests<\/code>\u5e93\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u7ba1\u7406\u4f1a\u8bdd\uff0c\u901a\u8fc7<code>requests.Session()<\/code>\u521b\u5efa\u4e00\u4e2a\u4f1a\u8bdd\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u4f1a\u81ea\u52a8\u5904\u7406Cookies\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u591a\u4e2a\u8bf7\u6c42\u4e4b\u95f4\u7ef4\u6301\u767b\u5f55\u72b6\u6001\uff0c\u4ece\u800c\u6293\u53d6\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8bbf\u95ee\u7f51\u9875\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528requests\u5e93\u76f4\u63a5\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3001\u4f7f\u7528Beaut [&hellip;]","protected":false},"author":3,"featured_media":998723,"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\/998712"}],"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=998712"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998712\/revisions"}],"predecessor-version":[{"id":998729,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998712\/revisions\/998729"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998723"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}