{"id":1073433,"date":"2025-01-08T11:25:38","date_gmt":"2025-01-08T03:25:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073433.html"},"modified":"2025-01-08T11:25:41","modified_gmt":"2025-01-08T03:25:41","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8python%e7%88%ac%e5%8f%96%e6%96%87%e4%bb%b6%e6%95%b0%e6%8d%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1073433.html","title":{"rendered":"\u5982\u4f55\u5229\u7528python\u722c\u53d6\u6587\u4ef6\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103330\/a67c95f1-a6fa-409a-be52-3e1675669686.webp\" alt=\"\u5982\u4f55\u5229\u7528python\u722c\u53d6\u6587\u4ef6\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u5229\u7528Python\u722c\u53d6\u6587\u4ef6\u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5e93\u5982requests\u3001BeautifulSoup\u3001Selenium\u7b49\u3002requests\u5e93\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0cBeautifulSoup\u7528\u4e8e\u89e3\u6790HTML\u5185\u5bb9\uff0cSelenium\u7528\u4e8e\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u8be6\u7ec6\u5730\u89e3\u91ca\uff0c\u4e0b\u9762\u5c06\u9010\u6b65\u5c55\u5f00\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528requests\u5e93\u722c\u53d6\u9759\u6001\u9875\u9762\u6570\u636e<\/h3>\n<\/p>\n<p><p>requests\u5e93\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684HTTP\u8bf7\u6c42\u5e93\uff0c\u9002\u7528\u4e8e\u722c\u53d6\u9759\u6001\u9875\u9762\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5requests\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5requests\u5e93\u3002\u6253\u5f00\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5requests\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u53d1\u9001HTTP\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u76ee\u6807\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &quot;http:\/\/example.com&quot;<\/p>\n<p>response = requests.get(url)<\/p>\n<h2><strong>\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if response.status_code == 200:<\/p>\n<p>    html_content = response.text<\/p>\n<p>    print(html_content)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to retrieve the content&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>requests.get(url)<\/code>\u53d1\u9001\u4e86\u4e00\u4e2aGET\u8bf7\u6c42\uff0c\u5982\u679c\u8bf7\u6c42\u6210\u529f\uff08\u72b6\u6001\u7801200\uff09\uff0c\u5219<code>response.text<\/code>\u5305\u542b\u4e86\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u7684Python\u5e93\u3002\u7ed3\u5408requests\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5BeautifulSoup<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5BeautifulSoup\u53ca\u5176\u4f9d\u8d56\u5e93lxml\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u89e3\u6790HTML\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u5185\u5bb9\uff0c\u5e76\u4ece\u4e2d\u63d0\u53d6\u6240\u9700\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>url = &quot;http:\/\/example.com&quot;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    html_content = response.text<\/p>\n<p>    soup = BeautifulSoup(html_content, &#39;lxml&#39;)<\/p>\n<p>    # \u793a\u4f8b\uff1a\u63d0\u53d6\u6240\u6709\u6807\u9898<\/p>\n<p>    titles = soup.find_all(&#39;h1&#39;)<\/p>\n<p>    for title in titles:<\/p>\n<p>        print(title.get_text())<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to retrieve the content&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>BeautifulSoup(html_content, &#39;lxml&#39;)<\/code>\u5c06HTML\u5185\u5bb9\u89e3\u6790\u4e3aBeautifulSoup\u5bf9\u8c61\uff0c<code>soup.find_all(&#39;h1&#39;)<\/code>\u627e\u5230\u6240\u6709\u7684<code>&lt;h1&gt;<\/code>\u6807\u7b7e\uff0c\u5e76\u901a\u8fc7<code>title.get_text()<\/code>\u63d0\u53d6\u5176\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Selenium\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u7f51\u9875\u5185\u5bb9\u662f\u901a\u8fc7JavaScript\u52a8\u6001\u52a0\u8f7d\u7684\uff0crequests\u548cBeautifulSoup\u65e0\u6cd5\u83b7\u53d6\u8fd9\u4e9b\u5185\u5bb9\u3002Selenium\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316\u6d4f\u89c8\u5668\u64cd\u4f5c\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u8fd9\u7c7b\u52a8\u6001\u7f51\u9875\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Selenium<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Selenium\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd8\u9700\u8981\u4e0b\u8f7d\u6d4f\u89c8\u5668\u9a71\u52a8\u7a0b\u5e8f\uff0c\u4f8b\u5982ChromeDriver\u3002\u4e0b\u8f7d\u540e\uff0c\u5c06\u5176\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528Selenium\u83b7\u53d6\u52a8\u6001\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Selenium\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u4f7f\u7528Chrome\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<p>url = &quot;http:\/\/example.com&quot;<\/p>\n<p>driver.get(url)<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\uff0c\u63d0\u53d6\u6240\u6709\u6807\u9898<\/strong><\/h2>\n<p>import time<\/p>\n<p>time.sleep(5)  # \u7b49\u5f855\u79d2\uff0c\u786e\u4fdd\u9875\u9762\u52a0\u8f7d\u5b8c\u6210<\/p>\n<p>titles = driver.find_elements_by_tag_name(&#39;h1&#39;)<\/p>\n<p>for title in titles:<\/p>\n<p>    print(title.text)<\/p>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>webdriver.Chrome()<\/code>\u542f\u52a8Chrome\u6d4f\u89c8\u5668\uff0c<code>driver.get(url)<\/code>\u6253\u5f00\u76ee\u6807\u7f51\u9875\uff0c\u901a\u8fc7<code>driver.find_elements_by_tag_name(&#39;h1&#39;)<\/code>\u627e\u5230\u6240\u6709<code>&lt;h1&gt;<\/code>\u6807\u7b7e\uff0c\u5e76\u901a\u8fc7<code>title.text<\/code>\u63d0\u53d6\u5176\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u6587\u4ef6\u4e0b\u8f7d<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u4e0b\u8f7d\u7f51\u9875\u4e0a\u7684\u6587\u4ef6\uff0c\u6bd4\u5982PDF\u3001CSV\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u6765\u5904\u7406\u6587\u4ef6\u4e0b\u8f7d\u3002<\/p>\n<\/p>\n<p><h4>1. \u4e0b\u8f7d\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4e0b\u8f7d\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &quot;http:\/\/example.com\/file.pdf&quot;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    with open(&quot;file.pdf&quot;, &quot;wb&quot;) as file:<\/p>\n<p>        file.write(response.content)<\/p>\n<p>    print(&quot;File downloaded successfully&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to download the file&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>requests.get(url)<\/code>\u53d1\u9001\u8bf7\u6c42\u4e0b\u8f7d\u6587\u4ef6\uff0c\u5982\u679c\u8bf7\u6c42\u6210\u529f\uff08\u72b6\u6001\u7801200\uff09\uff0c\u5219\u5c06\u6587\u4ef6\u5185\u5bb9\u5199\u5165\u672c\u5730\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u590d\u6742\u7f51\u9875\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u7f51\u9875\u7ed3\u6784\u590d\u6742\uff0c\u9700\u8981\u7ed3\u5408\u4f7f\u7528requests\u3001BeautifulSoup\u548cSelenium\u6765\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1. \u793a\u4f8b\uff1a\u63d0\u53d6\u8868\u683c\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u63d0\u53d6\u7f51\u9875\u8868\u683c\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 = &quot;http:\/\/example.com\/table&quot;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    html_content = response.text<\/p>\n<p>    soup = BeautifulSoup(html_content, &#39;lxml&#39;)<\/p>\n<p>    # \u627e\u5230\u8868\u683c<\/p>\n<p>    table = soup.find(&#39;table&#39;)<\/p>\n<p>    # \u63d0\u53d6\u8868\u683c\u6570\u636e<\/p>\n<p>    rows = table.find_all(&#39;tr&#39;)<\/p>\n<p>    for row in rows:<\/p>\n<p>        cols = row.find_all(&#39;td&#39;)<\/p>\n<p>        data = [col.get_text() for col in cols]<\/p>\n<p>        print(data)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to retrieve the content&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>soup.find(&#39;table&#39;)<\/code>\u627e\u5230\u8868\u683c\uff0c\u901a\u8fc7<code>table.find_all(&#39;tr&#39;)<\/code>\u627e\u5230\u6240\u6709\u884c\uff0c\u518d\u901a\u8fc7<code>row.find_all(&#39;td&#39;)<\/code>\u63d0\u53d6\u6bcf\u884c\u7684\u5355\u5143\u683c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u7f51\u9875\u4e2d\u7684\u8868\u5355\u4ea4\u4e92<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u6a21\u62df\u586b\u5199\u8868\u5355\u5e76\u63d0\u4ea4\uff0c\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u7684POST\u65b9\u6cd5\u6216Selenium\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528requests\u5e93\u63d0\u4ea4\u8868\u5355<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528requests\u5e93\u63d0\u4ea4\u8868\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &quot;http:\/\/example.com\/form&quot;<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>    &quot;email&quot;: &quot;john@example.com&quot;<\/p>\n<p>}<\/p>\n<p>response = requests.post(url, data=data)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    print(&quot;Form submitted successfully&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to submit the form&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>requests.post(url, data=data)<\/code>\u53d1\u9001POST\u8bf7\u6c42\uff0c\u63d0\u4ea4\u8868\u5355\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528Selenium\u63d0\u4ea4\u8868\u5355<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Selenium\u63d0\u4ea4\u8868\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>driver = webdriver.Chrome()<\/p>\n<p>url = &quot;http:\/\/example.com\/form&quot;<\/p>\n<p>driver.get(url)<\/p>\n<h2><strong>\u586b\u5199\u8868\u5355<\/strong><\/h2>\n<p>name_input = driver.find_element_by_name(&quot;name&quot;)<\/p>\n<p>name_input.send_keys(&quot;John&quot;)<\/p>\n<p>email_input = driver.find_element_by_name(&quot;email&quot;)<\/p>\n<p>email_input.send_keys(&quot;john@example.com&quot;)<\/p>\n<h2><strong>\u63d0\u4ea4\u8868\u5355<\/strong><\/h2>\n<p>submit_button = driver.find_element_by_xpath(&#39;\/\/button[@type=&quot;submit&quot;]&#39;)<\/p>\n<p>submit_button.click()<\/p>\n<p>print(&quot;Form submitted successfully&quot;)<\/p>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>driver.find_element_by_name(&quot;name&quot;)<\/code>\u627e\u5230\u8868\u5355\u8f93\u5165\u6846\uff0c\u5e76\u901a\u8fc7<code>name_input.send_keys(&quot;John&quot;)<\/code>\u586b\u5199\u6570\u636e\uff0c\u6700\u540e\u901a\u8fc7<code>submit_button.click()<\/code>\u63d0\u4ea4\u8868\u5355\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u53cd\u722c\u866b\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u7f51\u7ad9\u4f1a\u8bbe\u7f6e\u53cd\u722c\u866b\u673a\u5236\uff0c\u5982\u9650\u5236\u8bf7\u6c42\u9891\u7387\u3001\u4f7f\u7528CAPTCHA\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u5bf9\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6e\u8bf7\u6c42\u5934<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff0c\u4f2a\u88c5\u6210\u6d4f\u89c8\u5668\u8bf7\u6c42\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &quot;http:\/\/example.com&quot;<\/p>\n<p>headers = {<\/p>\n<p>    &quot;User-Agent&quot;: &quot;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36&quot;<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, headers=headers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u4ee3\u7406IP<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u4ee3\u7406IP\uff0c\u907f\u514d\u88ab\u5c01IP\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &quot;http:\/\/example.com&quot;<\/p>\n<p>proxies = {<\/p>\n<p>    &quot;http&quot;: &quot;http:\/\/123.456.789.000:8080&quot;,<\/p>\n<p>    &quot;https&quot;: &quot;http:\/\/123.456.789.000:8080&quot;<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, proxies=proxies)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u907f\u514d\u9891\u7e41\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>\u907f\u514d\u9891\u7e41\u8bf7\u6c42\uff0c\u8bbe\u7f6e\u5408\u7406\u7684\u5ef6\u8fdf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import requests<\/p>\n<p>url = &quot;http:\/\/example.com&quot;<\/p>\n<p>for _ in range(5):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    time.sleep(5)  # \u7b49\u5f855\u79d2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u4ee5\u4e0a\u6280\u672f\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u722c\u53d6\u5404\u79cd\u7f51\u9875\u6570\u636e\uff0c\u5904\u7406\u4e0d\u540c\u7684\u53cd\u722c\u866b\u673a\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u6570\u636e\u5b58\u50a8\u548c\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u722c\u53d6\u7684\u6570\u636e\u9700\u8981\u8fdb\u884c\u5b58\u50a8\u548c\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u5f0f\uff0c\u5982\u5b58\u50a8\u5230\u6587\u4ef6\u3001\u6570\u636e\u5e93\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b58\u50a8\u5230CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b58\u50a8\u6570\u636e\u5230CSV\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [<\/p>\n<p>    [&quot;Name&quot;, &quot;Email&quot;],<\/p>\n<p>    [&quot;John&quot;, &quot;john@example.com&quot;],<\/p>\n<p>    [&quot;Jane&quot;, &quot;jane@example.com&quot;]<\/p>\n<p>]<\/p>\n<p>with open(&quot;data.csv&quot;, &quot;w&quot;, newline=&quot;&quot;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerows(data)<\/p>\n<p>print(&quot;Data saved to data.csv&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b58\u50a8\u5230\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b58\u50a8\u6570\u636e\u5230SQLite\u6570\u636e\u5e93\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn = sqlite3.connect(&quot;data.db&quot;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>cursor.execute(&quot;&quot;&quot;<\/p>\n<p>CREATE TABLE IF NOT EXISTS users (<\/p>\n<p>    id INTEGER PRIMARY KEY AUTOINCREMENT,<\/p>\n<p>    name TEXT,<\/p>\n<p>    email TEXT<\/p>\n<p>)<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    (&quot;John&quot;, &quot;john@example.com&quot;),<\/p>\n<p>    (&quot;Jane&quot;, &quot;jane@example.com&quot;)<\/p>\n<p>]<\/p>\n<p>cursor.executemany(&quot;INSERT INTO users (name, email) VALUES (?, ?)&quot;, data)<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<p>print(&quot;Data saved to data.db&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b58\u50a8\u548c\u5904\u7406\u722c\u53d6\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u6570\u636e\u6e05\u6d17\u548c\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u722c\u53d6\u7684\u6570\u636e\u53ef\u80fd\u5305\u542b\u566a\u58f0\u548c\u5197\u4f59\uff0c\u9700\u8981\u8fdb\u884c\u6e05\u6d17\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528pandas\u5e93\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = pd.read_csv(&quot;data.csv&quot;)<\/p>\n<h2><strong>\u5220\u9664\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>cleaned_data = data.dropna()<\/p>\n<h2><strong>\u5220\u9664\u91cd\u590d\u503c<\/strong><\/h2>\n<p>cleaned_data = cleaned_data.drop_duplicates()<\/p>\n<p>print(cleaned_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528pandas\u5e93\u8fdb\u884c\u7b80\u5355\u6570\u636e\u5206\u6790\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = pd.read_csv(&quot;data.csv&quot;)<\/p>\n<h2><strong>\u6570\u636e\u7edf\u8ba1<\/strong><\/h2>\n<p>print(data.describe())<\/p>\n<h2><strong>\u6570\u636e\u5206\u7ec4\u6c47\u603b<\/strong><\/h2>\n<p>grouped_data = data.groupby(&quot;category&quot;).sum()<\/p>\n<p>print(grouped_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6570\u636e\u6e05\u6d17\u548c\u5206\u6790\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u7684\u8d28\u91cf\u548c\u4ef7\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u81ea\u52a8\u5316\u8c03\u5ea6\u548c\u76d1\u63a7<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\u5b9e\u73b0\u722c\u866b\u7684\u81ea\u52a8\u5316\u8fd0\u884c\u548c\u76d1\u63a7\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528cron\u8c03\u5ea6\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528cron\u8c03\u5ea6\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cron\"># \u6bcf\u5929\u51cc\u66682\u70b9\u8fd0\u884c\u722c\u866b\u811a\u672c<\/p>\n<p>0 2 * * * \/usr\/bin\/python3 \/path\/to\/your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528Windows\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u8c03\u5ea6\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u3002<\/li>\n<li>\u521b\u5efa\u57fa\u672c\u4efb\u52a1\u3002<\/li>\n<li>\u8bbe\u7f6e\u89e6\u53d1\u5668\u548c\u64cd\u4f5c\uff0c\u9009\u62e9\u722c\u866b\u811a\u672c\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\uff0c\u53ef\u4ee5\u5b9e\u73b0\u722c\u866b\u7684\u5b9a\u65f6\u8fd0\u884c\u548c\u76d1\u63a7\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528requests\u3001BeautifulSoup\u3001Selenium\u7b49\u5e93\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u722c\u53d6\u7f51\u9875\u6570\u636e\uff0c\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u7f51\u9875\u548c\u53cd\u722c\u866b\u673a\u5236\u3002\u722c\u53d6\u7684\u6570\u636e\u53ef\u4ee5\u5b58\u50a8\u5230\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u5e76\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u5206\u6790\u3002\u901a\u8fc7\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\uff0c\u53ef\u4ee5\u5b9e\u73b0\u722c\u866b\u7684\u81ea\u52a8\u5316\u8fd0\u884c\u548c\u76d1\u63a7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684Python\u5e93\u6765\u722c\u53d6\u6587\u4ef6\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u4e2a\u5e93\u53ef\u7528\u4e8e\u722c\u53d6\u6587\u4ef6\u6570\u636e\uff0c\u5982Requests\u3001BeautifulSoup\u548cScrapy\u3002Requests\u5e93\u975e\u5e38\u9002\u5408\u5904\u7406HTTP\u8bf7\u6c42\uff0cBeautifulSoup\u5219\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\uff0cScrapy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6846\u67b6\uff0c\u9002\u5408\u8fdb\u884c\u5927\u89c4\u6a21\u722c\u866b\u9879\u76ee\u3002\u6839\u636e\u9879\u76ee\u7684\u590d\u6742\u5ea6\u548c\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u5c06\u5927\u5927\u63d0\u9ad8\u722c\u866b\u7684\u6548\u7387\u548c\u6548\u679c\u3002<\/p>\n<p><strong>\u5728\u722c\u53d6\u6587\u4ef6\u6570\u636e\u65f6\u5982\u4f55\u5904\u7406\u53cd\u722c\u866b\u673a\u5236\uff1f<\/strong><br \/>\u5f88\u591a\u7f51\u7ad9\u4f1a\u5b9e\u65bd\u53cd\u722c\u866b\u673a\u5236\u6765\u9632\u6b62\u6570\u636e\u88ab\u722c\u53d6\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u4e00\u6311\u6218\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b56\u7565\uff0c\u5982\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff08User-Agent\uff09\u3001\u4f7f\u7528\u4ee3\u7406IP\u3001\u968f\u673a\u8bf7\u6c42\u95f4\u9694\u7b49\u3002\u6b64\u5916\uff0c\u6a21\u62df\u7528\u6237\u884c\u4e3a\u548c\u4f7f\u7528Cookies\u4e5f\u80fd\u6709\u6548\u964d\u4f4e\u88ab\u68c0\u6d4b\u7684\u98ce\u9669\u3002\u786e\u4fdd\u9075\u5faa\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\u89c4\u5b9a\uff0c\u5408\u6cd5\u5408\u89c4\u5730\u8fdb\u884c\u6570\u636e\u722c\u53d6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5b58\u50a8\u722c\u53d6\u5230\u7684\u6587\u4ef6\u6570\u636e\uff1f<\/strong><br \/>\u5728\u6210\u529f\u722c\u53d6\u6587\u4ef6\u6570\u636e\u540e\uff0c\u5b58\u50a8\u65b9\u5f0f\u7684\u9009\u62e9\u4e5f\u5f88\u91cd\u8981\u3002\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u4e3aCSV\u3001JSON\u3001Excel\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u5982MySQL\u3001MongoDB\u7b49\u3002\u9009\u62e9\u5b58\u50a8\u683c\u5f0f\u65f6\uff0c\u8981\u8003\u8651\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u9700\u6c42\u548c\u6613\u7528\u6027\u3002\u5bf9\u4e8e\u5c0f\u578b\u9879\u76ee\uff0cCSV\u548cJSON\u683c\u5f0f\u8f83\u4e3a\u7b80\u5355\u6613\u7528\uff1b\u800c\u5bf9\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u67e5\u8be2\u548c\u5206\u6790\u7684\u9879\u76ee\uff0c\u6570\u636e\u5e93\u5b58\u50a8\u5c06\u66f4\u4e3a\u5408\u9002\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5229\u7528Python\u722c\u53d6\u6587\u4ef6\u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5e93\u5982requests\u3001BeautifulSoup\u3001Selenium\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1073445,"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\/1073433"}],"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=1073433"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073433\/revisions"}],"predecessor-version":[{"id":1073447,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073433\/revisions\/1073447"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073445"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}