{"id":1079827,"date":"2025-01-08T12:24:28","date_gmt":"2025-01-08T04:24:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1079827.html"},"modified":"2025-01-08T12:24:31","modified_gmt":"2025-01-08T04:24:31","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%87%aa%e5%8a%a8%e6%8a%93%e5%8f%96%e7%bd%91%e9%a1%b5%e7%9a%84%e6%96%87%e7%ab%a0-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1079827.html","title":{"rendered":"\u5982\u4f55\u7528python\u81ea\u52a8\u6293\u53d6\u7f51\u9875\u7684\u6587\u7ae0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182650\/f6971983-610c-45c7-a64f-3caa0b4a459c.webp\" alt=\"\u5982\u4f55\u7528python\u81ea\u52a8\u6293\u53d6\u7f51\u9875\u7684\u6587\u7ae0\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u81ea\u52a8\u6293\u53d6\u7f51\u9875\u7684\u6587\u7ae0\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u5e93\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3001\u5904\u7406\u6293\u53d6\u7684\u6570\u636e\u3001\u4fdd\u5b58\u6293\u53d6\u5230\u7684\u6570\u636e\u3002<\/strong>\u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u5e93\u662f\u5173\u952e\u7684\u4e00\u6b65\u3002Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u5982Requests\u548cBeautifulSoup\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6293\u53d6\u548c\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u5b8c\u6210\u81ea\u52a8\u6293\u53d6\u7f51\u9875\u6587\u7ae0\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u5e93<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u8bb8\u591a\u7528\u4e8e\u7f51\u7edc\u722c\u866b\u548c\u7f51\u9875\u6293\u53d6\u7684\u5e93\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5305\u62ecRequests\u3001BeautifulSoup\u3001Scrapy\u548cSelenium\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u4f7f\u6211\u4eec\u7684\u5de5\u4f5c\u4e8b\u534a\u529f\u500d\u3002<\/p>\n<\/p>\n<p><p><strong>1. Requests\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Requests\u5e93\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684HTTP\u5e93\uff0c\u9002\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9\u3002\u5b83\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u89c2\uff0c\u7f3a\u70b9\u662f\u53ea\u9002\u7528\u4e8e\u9759\u6001\u7f51\u9875\u7684\u6293\u53d6\u3002<\/p>\n<\/p>\n<p><p><strong>2. BeautifulSoup\u5e93<\/strong><\/p>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684\u5e93\uff0c\u80fd\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u5b83\u4e0eRequests\u5e93\u914d\u5408\u4f7f\u7528\u6548\u679c\u6700\u4f73\uff0c\u9002\u7528\u4e8e\u9759\u6001\u7f51\u9875\u7684\u6293\u53d6\u548c\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><p><strong>3. Scrapy\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Scrapy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u722c\u866b\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u6784\u5efa\u590d\u6742\u7684\u7f51\u7edc\u722c\u866b\u548c\u6570\u636e\u63d0\u53d6\u4efb\u52a1\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u6293\u53d6\u9759\u6001\u7f51\u9875\uff0c\u8fd8\u80fd\u5904\u7406\u52a8\u6001\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p><strong>4. Selenium\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316Web\u6d4f\u89c8\u5668\u64cd\u4f5c\u7684\u5e93\uff0c\u9002\u7528\u4e8e\u6293\u53d6\u52a8\u6001\u7f51\u9875\u5185\u5bb9\u3002\u5b83\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u884c\u4e3a\uff0c\u5982\u70b9\u51fb\u6309\u94ae\u3001\u586b\u5199\u8868\u5355\u7b49\uff0c\u4ece\u800c\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u5e93\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3002\u4e0b\u9762\u4ee5Requests\u548cBeautifulSoup\u5e93\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5982\u4f55\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u5b89\u88c5Requests\u548cBeautifulSoup\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Requests\u548cBeautifulSoup\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p>pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u53d1\u9001HTTP\u8bf7\u6c42<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u6293\u53d6\u4e00\u4e2a\u65b0\u95fb\u7f51\u7ad9\u7684\u6587\u7ae0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &quot;https:\/\/example.com\/news&quot;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>html_content = response.content<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u89e3\u6790\u7f51\u9875\u5185\u5bb9<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u5e93\u89e3\u6790\u7f51\u9875\u5185\u5bb9\uff0c\u4ece\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>soup = BeautifulSoup(html_content, &quot;html.parser&quot;)<\/p>\n<p>articles = soup.find_all(&quot;div&quot;, class_=&quot;article&quot;)<\/p>\n<p>for article in articles:<\/p>\n<p>    title = article.find(&quot;h2&quot;).text<\/p>\n<p>    content = article.find(&quot;p&quot;).text<\/p>\n<p>    print(&quot;Title:&quot;, title)<\/p>\n<p>    print(&quot;Content:&quot;, content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u6293\u53d6\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6293\u53d6\u5230\u7f51\u9875\u5185\u5bb9\u540e\uff0c\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u4fdd\u5b58\u5230\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u6293\u53d6\u5230\u7684\u6587\u7ae0\u4fdd\u5b58\u5230\u6587\u672c\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;articles.txt&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    for article in articles:<\/p>\n<p>        title = article.find(&quot;h2&quot;).text<\/p>\n<p>        content = article.find(&quot;p&quot;).text<\/p>\n<p>        file.write(f&quot;Title: {title}\\n&quot;)<\/p>\n<p>        file.write(f&quot;Content: {content}\\n\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u4fdd\u5b58\u5230\u6570\u636e\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u5c06\u6570\u636e\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\uff0c\u4f8b\u5982SQLite\u6570\u636e\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>conn = sqlite3.connect(&quot;articles.db&quot;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>cursor.execute(&quot;CREATE TABLE IF NOT EXISTS articles (title TEXT, content TEXT)&quot;)<\/p>\n<p>for article in articles:<\/p>\n<p>    title = article.find(&quot;h2&quot;).text<\/p>\n<p>    content = article.find(&quot;p&quot;).text<\/p>\n<p>    cursor.execute(&quot;INSERT INTO articles (title, content) VALUES (?, ?)&quot;, (title, content))<\/p>\n<p>conn.commit()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u52a8\u6001\u7f51\u9875<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u52a8\u6001\u7f51\u9875\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528Selenium\u5e93\u6765\u6293\u53d6\u5185\u5bb9\u3002Selenium\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u64cd\u4f5c\uff0c\u4ece\u800c\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u5b89\u88c5Selenium\u5e93\u548cWebDriver<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Selenium\u5e93\u548c\u76f8\u5e94\u7684WebDriver\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Chrome\u6d4f\u89c8\u5668\u7684WebDriver\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\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u7cfb\u7edf\u8def\u5f84\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528Selenium\u6293\u53d6\u52a8\u6001\u7f51\u9875<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Selenium\u6a21\u62df\u7528\u6237\u64cd\u4f5c\uff0c\u6293\u53d6\u52a8\u6001\u7f51\u9875\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u6293\u53d6\u4e00\u4e2a\u52a8\u6001\u52a0\u8f7d\u7684\u65b0\u95fb\u7f51\u7ad9\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>import time<\/p>\n<p>url = &quot;https:\/\/example.com\/news&quot;<\/p>\n<p>driver = webdriver.Chrome()<\/p>\n<p>driver.get(url)<\/p>\n<h2><strong>\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210<\/strong><\/h2>\n<p>time.sleep(5)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u7ae0\u5185\u5bb9<\/strong><\/h2>\n<p>articles = driver.find_elements(By.CLASS_NAME, &quot;article&quot;)<\/p>\n<p>for article in articles:<\/p>\n<p>    title = article.find_element(By.TAG_NAME, &quot;h2&quot;).text<\/p>\n<p>    content = article.find_element(By.TAG_NAME, &quot;p&quot;).text<\/p>\n<p>    print(&quot;Title:&quot;, title)<\/p>\n<p>    print(&quot;Content:&quot;, content)<\/p>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u81ea\u52a8\u6293\u53d6\u7f51\u9875\u7684\u6587\u7ae0\u3002\u65e0\u8bba\u662f\u9759\u6001\u7f51\u9875\u8fd8\u662f\u52a8\u6001\u7f51\u9875\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u5e93\uff0c\u89e3\u6790\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u5904\u7406\u6293\u53d6\u5230\u7684\u6570\u636e\uff0c\u5b8c\u6210\u7f51\u9875\u6293\u53d6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u53cd\u722c\u866b\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5f88\u591a\u7f51\u7ad9\u4e3a\u4e86\u9632\u6b62\u6570\u636e\u88ab\u5927\u91cf\u6293\u53d6\uff0c\u90fd\u4f1a\u8bbe\u7f6e\u53cd\u722c\u866b\u673a\u5236\u3002\u5e38\u89c1\u7684\u53cd\u722c\u866b\u673a\u5236\u5305\u62ec\uff1a<\/p>\n<\/p>\n<p><p><strong>1. User-Agent\u8bbe\u7f6e<\/strong><\/p>\n<\/p>\n<p><p>\u7f51\u7ad9\u4f1a\u68c0\u67e5\u8bf7\u6c42\u5934\u4e2d\u7684User-Agent\u5b57\u6bb5\uff0c\u4ee5\u5224\u65ad\u8bf7\u6c42\u662f\u5426\u6765\u81ea\u4e8e\u6d4f\u89c8\u5668\u3002\u53ef\u4ee5\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\u8bbe\u7f6eUser-Agent\uff0c\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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><p><strong>2. IP\u5c01\u7981<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u4e9b\u7f51\u7ad9\u4f1a\u68c0\u6d4b\u540c\u4e00IP\u5730\u5740\u7684\u8bf7\u6c42\u9891\u7387\uff0c\u5982\u679c\u8bf7\u6c42\u8fc7\u4e8e\u9891\u7e41\uff0c\u53ef\u80fd\u4f1a\u5bf9IP\u8fdb\u884c\u5c01\u7981\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4ee3\u7406IP\u6765\u7ed5\u8fc7\u8fd9\u79cd\u9650\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">proxies = {<\/p>\n<p>    &quot;http&quot;: &quot;http:\/\/your_proxy_ip:port&quot;,<\/p>\n<p>    &quot;https&quot;: &quot;http:\/\/your_proxy_ip:port&quot;<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, headers=headers, proxies=proxies)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u9a8c\u8bc1\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u4e9b\u7f51\u7ad9\u4f1a\u4f7f\u7528\u9a8c\u8bc1\u7801\u6765\u963b\u6b62\u81ea\u52a8\u5316\u6293\u53d6\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528OCR\u6280\u672f\u8bc6\u522b\u9a8c\u8bc1\u7801\uff0c\u6216\u8005\u624b\u52a8\u89e3\u51b3\u9a8c\u8bc1\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p><strong>4. \u52a8\u6001\u5185\u5bb9\u52a0\u8f7d<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u6a21\u62df\u7528\u6237\u64cd\u4f5c\uff0c\u7b49\u5f85\u5185\u5bb9\u52a0\u8f7d\u5b8c\u6210\u540e\u518d\u6293\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f18\u5316\u548c\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u57fa\u672c\u7684\u7f51\u9875\u6293\u53d6\u4efb\u52a1\u540e\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55\u722c\u866b\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><p><strong>1. \u591a\u7ebf\u7a0b\u548c\u5f02\u6b65\u7f16\u7a0b<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u7f16\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u6293\u53d6\u6548\u7387\uff0c\u51cf\u5c11\u7b49\u5f85\u65f6\u95f4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u6293\u53d6\u591a\u4e2a\u9875\u9762\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def fetch_article(url):<\/p>\n<p>    response = requests.get(url, headers=headers)<\/p>\n<p>    soup = BeautifulSoup(response.content, &quot;html.parser&quot;)<\/p>\n<p>    # \u89e3\u6790\u6587\u7ae0\u5185\u5bb9<\/p>\n<p>    ...<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b<\/strong><\/h2>\n<p>threads = []<\/p>\n<p>for url in article_urls:<\/p>\n<p>    thread = threading.Thread(target=fetch_article, args=(url,))<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u6570\u636e\u6e05\u6d17\u548c\u5206\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u6293\u53d6\u5230\u7684\u6570\u636e\u53ef\u80fd\u5305\u542b\u4e00\u4e9b\u65e0\u7528\u4fe1\u606f\uff0c\u9700\u8981\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u5206\u6790\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6709\u7528\u7684\u4fe1\u606f\uff0c\u6216\u8005\u4f7f\u7528Pandas\u5e93\u8fdb\u884c\u6570\u636e\u5206\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>for article in articles:<\/p>\n<p>    content = article.find(&quot;p&quot;).text<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6709\u7528\u4fe1\u606f<\/p>\n<p>    cleaned_content = re.sub(r&quot;\\s+&quot;, &quot; &quot;, content)<\/p>\n<p>    print(cleaned_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u81ea\u52a8\u5316\u8c03\u5ea6<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\uff08\u5982cron\u6216Celery\uff09\u5b9a\u65f6\u8fd0\u884c\u722c\u866b\uff0c\u5b9a\u671f\u6293\u53d6\u6700\u65b0\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Celery\u5b9a\u65f6\u8fd0\u884c\u722c\u866b\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from celery import Celery<\/p>\n<p>app = Celery(&quot;tasks&quot;, broker=&quot;pyamqp:\/\/guest@localhost\/\/&quot;)<\/p>\n<p>@app.task<\/p>\n<p>def fetch_articles():<\/p>\n<p>    # \u6293\u53d6\u6587\u7ae0\u7684\u4ee3\u7801<\/p>\n<p>    ...<\/p>\n<h2><strong>\u5b9a\u65f6\u4efb\u52a1<\/strong><\/h2>\n<p>app.conf.beat_schedule = {<\/p>\n<p>    &quot;fetch-articles-every-hour&quot;: {<\/p>\n<p>        &quot;task&quot;: &quot;tasks.fetch_articles&quot;,<\/p>\n<p>        &quot;schedule&quot;: 3600.0,<\/p>\n<p>    },<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4f18\u5316\u548c\u6269\u5c55\uff0c\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u9ad8\u6548\u3001\u7a33\u5b9a\u7684\u7f51\u9875\u6293\u53d6\u7cfb\u7edf\u3002\u65e0\u8bba\u662f\u6570\u636e\u6293\u53d6\u3001\u6e05\u6d17\u3001\u5206\u6790\uff0c\u8fd8\u662f\u8c03\u5ea6\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u5b9e\u73b0\u81ea\u52a8\u5316\u548c\u9ad8\u6548\u5904\u7406\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\u8fdb\u884c\u7f51\u9875\u6293\u53d6\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u7f51\u9875\u6293\u53d6\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684Python\u5e93\u975e\u5e38\u91cd\u8981\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecRequests\u548cBeautifulSoup\u3002Requests\u7528\u4e8e\u53d1\u9001\u7f51\u7edc\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u800cBeautifulSoup\u5219\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863\u3002\u7ed3\u5408\u8fd9\u4e24\u4e2a\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u548c\u5904\u7406\u7f51\u9875\u4e0a\u7684\u6587\u7ae0\u3002\u6b64\u5916\uff0cScrapy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6846\u67b6\uff0c\u9002\u5408\u9700\u8981\u6293\u53d6\u591a\u4e2a\u7f51\u9875\u6216\u8fdb\u884c\u590d\u6742\u6293\u53d6\u4efb\u52a1\u7684\u7528\u6237\u3002<\/p>\n<p><strong>\u5728\u6293\u53d6\u7f51\u9875\u65f6\uff0c\u5982\u4f55\u5904\u7406\u53cd\u722c\u866b\u673a\u5236\uff1f<\/strong><br \/>\u8bb8\u591a\u7f51\u7ad9\u90fd\u6709\u53cd\u722c\u866b\u673a\u5236\uff0c\u65e8\u5728\u9632\u6b62\u81ea\u52a8\u5316\u6293\u53d6\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u4e00\u95ee\u9898\uff0c\u53ef\u4ee5\u91c7\u53d6\u591a\u79cd\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u8bf7\u6c42\u4e2d\u6dfb\u52a0\u7528\u6237\u4ee3\u7406\uff08User-Agent\uff09\u5934\uff0c\u4ee5\u4f2a\u88c5\u6210\u666e\u901a\u7528\u6237\u6d4f\u89c8\u5668\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528\u968f\u673a\u5ef6\u8fdf\u6765\u6a21\u62df\u4eba\u7c7b\u7684\u884c\u4e3a\uff0c\u907f\u514d\u8fc7\u4e8e\u9891\u7e41\u7684\u8bf7\u6c42\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u4ee3\u7406\u670d\u52a1\u5668\u53ef\u4ee5\u5e2e\u52a9\u9690\u85cf\u771f\u5b9eIP\u5730\u5740\uff0c\u8fdb\u4e00\u6b65\u964d\u4f4e\u88ab\u68c0\u6d4b\u7684\u98ce\u9669\u3002<\/p>\n<p><strong>\u6293\u53d6\u5230\u7684\u6587\u7ae0\u5982\u4f55\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u548c\u5904\u7406\uff1f<\/strong><br \/>\u6293\u53d6\u5230\u7684\u6587\u7ae0\u53ef\u4ee5\u9009\u62e9\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u5b58\u50a8\u3002\u5e38\u89c1\u7684\u5b58\u50a8\u683c\u5f0f\u5305\u62ecCSV\u6587\u4ef6\u3001JSON\u6587\u4ef6\u6216\u6570\u636e\u5e93\uff08\u5982SQLite\u3001MySQL\uff09\u3002\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u5206\u6790\uff0c\u5c06\u6293\u53d6\u7684\u6570\u636e\u8f6c\u5316\u4e3a\u7ed3\u6784\u5316\u7684\u683c\u5f0f\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u4f7f\u7528\u548c\u5171\u4eab\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u81ea\u52a8\u6293\u53d6\u7f51\u9875\u7684\u6587\u7ae0\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u5e93\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3001\u5904\u7406\u6293\u53d6\u7684\u6570\u636e\u3001\u4fdd\u5b58\u6293 [&hellip;]","protected":false},"author":3,"featured_media":1079836,"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\/1079827"}],"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=1079827"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079827\/revisions"}],"predecessor-version":[{"id":1079840,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079827\/revisions\/1079840"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1079836"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1079827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1079827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1079827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}