{"id":1165351,"date":"2025-01-15T15:19:18","date_gmt":"2025-01-15T07:19:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1165351.html"},"modified":"2025-01-15T15:19:20","modified_gmt":"2025-01-15T07:19:20","slug":"python%e5%a6%82%e4%bd%95%e5%be%aa%e7%8e%af%e7%88%ac%e5%8f%96%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1165351.html","title":{"rendered":"python\u5982\u4f55\u5faa\u73af\u722c\u53d6\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25205722\/b540664d-41dc-4b79-bf8c-9ed7f126abba.webp\" alt=\"python\u5982\u4f55\u5faa\u73af\u722c\u53d6\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u5faa\u73af\u722c\u53d6\u6570\u636e\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u7ed3\u5408\u65f6\u95f4\u95f4\u9694\u8fdb\u884c\u722c\u53d6\u3002\u53ef\u4ee5\u5229\u7528Scrapy\u6846\u67b6\u8fdb\u884c\u5faa\u73af\u722c\u53d6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af\u8fdb\u884c\u6570\u636e\u722c\u53d6<\/p>\n<\/p>\n<p><p><strong>for\u5faa\u73af<\/strong>\u662fPython\u4e2d\u6700\u5e38\u89c1\u7684\u5faa\u73af\u7ed3\u6784\u4e4b\u4e00\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u7528\u4e8e\u5faa\u73af\u722c\u53d6\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528for\u5faa\u73af\u722c\u53d6\u591a\u4e2a\u7f51\u9875\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u722c\u53d6\u591a\u4e2a\u7f51\u9875\u7684\u6570\u636e<\/strong><\/h2>\n<p>urls = [&#39;https:\/\/example.com\/page1&#39;, &#39;https:\/\/example.com\/page2&#39;, &#39;https:\/\/example.com\/page3&#39;]<\/p>\n<p>for url in urls:<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<p>        # \u5904\u7406\u9875\u9762\u5185\u5bb9<\/p>\n<p>        print(soup.title.text)<\/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 retrieve {url}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2aURL\u7684\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u8fd9\u4e9bURL\uff0c\u4f9d\u6b21\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u5904\u7406\u54cd\u5e94\u3002<strong>BeautifulSoup<\/strong>\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u7684\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6\u9875\u9762\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528while\u5faa\u73af\u8fdb\u884c\u6570\u636e\u722c\u53d6<\/p>\n<\/p>\n<p><p><strong>while\u5faa\u73af<\/strong>\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u6301\u7eed\u722c\u53d6\u6570\u636e\u7684\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528while\u5faa\u73af\u722c\u53d6\u5206\u9875\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u722c\u53d6\u5206\u9875\u6570\u636e<\/strong><\/h2>\n<p>base_url = &#39;https:\/\/example.com\/page&#39;<\/p>\n<p>page = 1<\/p>\n<p>while True:<\/p>\n<p>    url = f&quot;{base_url}{page}&quot;<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<p>        # \u5904\u7406\u9875\u9762\u5185\u5bb9<\/p>\n<p>        print(soup.title.text)<\/p>\n<p>        page += 1<\/p>\n<p>        # \u5224\u65ad\u662f\u5426\u5230\u8fbe\u6700\u540e\u4e00\u9875\uff08\u8fd9\u91cc\u5047\u8bbe\u5b58\u5728\u4e00\u4e2a\u6761\u4ef6\u5224\u65ad\uff09<\/p>\n<p>        if not soup.find(&#39;a&#39;, text=&#39;Next&#39;):<\/p>\n<p>            break<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Failed to retrieve {url}&quot;)<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528while\u5faa\u73af\u4e0d\u65ad\u5730\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u76f4\u5230\u5230\u8fbe\u6700\u540e\u4e00\u9875\u3002\u901a\u8fc7\u68c0\u67e5\u9875\u9762\u4e2d\u662f\u5426\u5b58\u5728\u201c\u4e0b\u4e00\u9875\u201d\u94fe\u63a5\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u5b9a\u4f55\u65f6\u505c\u6b62\u722c\u53d6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u7ed3\u5408\u65f6\u95f4\u95f4\u9694\u8fdb\u884c\u722c\u53d6<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u88ab\u7f51\u7ad9\u5c4f\u853d\u6216\u8005\u51cf\u5c11\u670d\u52a1\u5668\u538b\u529b\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u52a0\u5165\u65f6\u95f4\u95f4\u9694\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u7ed3\u5408\u65f6\u95f4\u95f4\u9694\u8fdb\u884c\u6570\u636e\u722c\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import time<\/p>\n<h2><strong>\u722c\u53d6\u591a\u4e2a\u7f51\u9875\u7684\u6570\u636e<\/strong><\/h2>\n<p>urls = [&#39;https:\/\/example.com\/page1&#39;, &#39;https:\/\/example.com\/page2&#39;, &#39;https:\/\/example.com\/page3&#39;]<\/p>\n<p>for url in urls:<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<p>        # \u5904\u7406\u9875\u9762\u5185\u5bb9<\/p>\n<p>        print(soup.title.text)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Failed to retrieve {url}&quot;)<\/p>\n<p>    # \u7b49\u5f852\u79d2\u949f<\/p>\n<p>    time.sleep(2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u6bcf\u6b21\u722c\u53d6\u540e\u4f7f\u7528<strong>time.sleep(2)<\/strong>\u6682\u505c2\u79d2\u949f\uff0c\u4ee5\u907f\u514d\u5bf9\u670d\u52a1\u5668\u9020\u6210\u8fc7\u5927\u7684\u538b\u529b\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5229\u7528Scrapy\u6846\u67b6\u8fdb\u884c\u5faa\u73af\u722c\u53d6<\/p>\n<\/p>\n<p><p><strong>Scrapy<\/strong>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Python\u722c\u866b\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u722c\u53d6\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528Scrapy\u8fdb\u884c\u5faa\u73af\u722c\u53d6\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Scrapy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scrapy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Scrapy\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">scrapy startproject myproject<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u722c\u866b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cd myproject<\/p>\n<p>scrapy genspider myspider example.com<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7f16\u8f91\u751f\u6210\u7684<code>myspider.py<\/code>\u6587\u4ef6\uff0c\u6dfb\u52a0\u722c\u53d6\u903b\u8f91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scrapy<\/p>\n<p>class MySpider(scrapy.Spider):<\/p>\n<p>    name = &quot;myspider&quot;<\/p>\n<p>    start_urls = [&#39;https:\/\/example.com\/page1&#39;, &#39;https:\/\/example.com\/page2&#39;, &#39;https:\/\/example.com\/page3&#39;]<\/p>\n<p>    def parse(self, response):<\/p>\n<p>        # \u5904\u7406\u9875\u9762\u5185\u5bb9<\/p>\n<p>        self.log(response.css(&#39;title::text&#39;).get())<\/p>\n<p>        # \u722c\u53d6\u4e0b\u4e00\u9875\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09<\/p>\n<p>        next_page = response.css(&#39;a.next::attr(href)&#39;).get()<\/p>\n<p>        if next_page is not None:<\/p>\n<p>            yield response.follow(next_page, self.parse)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u8fd0\u884c\u722c\u866b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">scrapy crawl myspider<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aScrapy\u722c\u866b\uff0c\u901a\u8fc7\u8bbe\u7f6e<strong>start_urls<\/strong>\u6765\u6307\u5b9a\u8d77\u59cbURL\u3002<strong>parse<\/strong>\u65b9\u6cd5\u7528\u4e8e\u5904\u7406\u54cd\u5e94\uff0c\u5e76\u63d0\u53d6\u9875\u9762\u6570\u636e\u3002\u901a\u8fc7<strong>response.follow<\/strong>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9012\u5f52\u5730\u722c\u53d6\u4e0b\u4e00\u9875\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u52a8\u6001\u7f51\u9875<\/p>\n<\/p>\n<p><p>\u6709\u4e9b\u7f51\u9875\u4f7f\u7528JavaScript\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u9700\u8981\u4f7f\u7528\u50cfSelenium\u8fd9\u6837\u7684\u5de5\u5177\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528Selenium\u722c\u53d6\u52a8\u6001\u7f51\u9875\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Selenium\u548c\u6d4f\u89c8\u5668\u9a71\u52a8\uff08\u4f8b\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>\u63a5\u7740\uff0c\u7f16\u5199\u722c\u53d6\u903b\u8f91\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<h2><strong>\u8bbe\u7f6eChromeDriver\u8def\u5f84<\/strong><\/h2>\n<p>driver_path = &#39;\/path\/to\/chromedriver&#39;<\/p>\n<h2><strong>\u542f\u52a8\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=driver_path)<\/p>\n<h2><strong>\u722c\u53d6\u591a\u4e2a\u7f51\u9875\u7684\u6570\u636e<\/strong><\/h2>\n<p>urls = [&#39;https:\/\/example.com\/page1&#39;, &#39;https:\/\/example.com\/page2&#39;, &#39;https:\/\/example.com\/page3&#39;]<\/p>\n<p>for url in urls:<\/p>\n<p>    driver.get(url)<\/p>\n<p>    time.sleep(2)  # \u7b49\u5f85\u9875\u9762\u52a0\u8f7d<\/p>\n<p>    # \u5904\u7406\u9875\u9762\u5185\u5bb9<\/p>\n<p>    title = driver.find_element(By.TAG_NAME, &#39;title&#39;).text<\/p>\n<p>    print(title)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Selenium\u542f\u52a8\u4e00\u4e2aChrome\u6d4f\u89c8\u5668\u5b9e\u4f8b\uff0c\u5e76\u8bbf\u95ee\u591a\u4e2a\u7f51\u9875\u3002\u901a\u8fc7<strong>find_element<\/strong>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u53d6\u9875\u9762\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u53cd\u722c\u673a\u5236<\/p>\n<\/p>\n<p><p>\u8bb8\u591a\u7f51\u7ad9\u90fd\u6709\u53cd\u722c\u673a\u5236\uff0c\u5982\u9a8c\u8bc1\u7801\u3001IP\u5c01\u7981\u3001\u8bf7\u6c42\u9891\u7387\u9650\u5236\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u5bf9\u7b56\u7565\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u4ee3\u7406<\/strong>\uff1a\u901a\u8fc7\u4f7f\u7528\u4ee3\u7406IP\uff0c\u53ef\u4ee5\u907f\u514d\u540c\u4e00\u4e2aIP\u9891\u7e41\u8bbf\u95ee\u5bfc\u81f4\u88ab\u5c01\u7981\u3002<\/li>\n<li><strong>\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a<\/strong>\uff1a\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u5934\u3001\u4f7f\u7528Selenium\u7b49\u5de5\u5177\uff0c\u53ef\u4ee5\u6a21\u62df\u771f\u5b9e\u7528\u6237\u7684\u6d4f\u89c8\u5668\u884c\u4e3a\u3002<\/li>\n<li><strong>\u5904\u7406\u9a8c\u8bc1\u7801<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528OCR\u6280\u672f\u8bc6\u522b\u9a8c\u8bc1\u7801\uff0c\u6216\u8005\u901a\u8fc7\u4eba\u5de5\u8f93\u5165\u89e3\u51b3\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u5408\u7406\u7684\u722c\u53d6\u95f4\u9694<\/strong>\uff1a\u901a\u8fc7\u8bbe\u7f6e\u722c\u53d6\u95f4\u9694\uff0c\u51cf\u5c11\u5bf9\u670d\u52a1\u5668\u7684\u538b\u529b\uff0c\u907f\u514d\u89e6\u53d1\u53cd\u722c\u673a\u5236\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u4ee3\u7406\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<h2><strong>\u8bbe\u7f6e\u4ee3\u7406<\/strong><\/h2>\n<p>proxies = {<\/p>\n<p>    &#39;http&#39;: &#39;http:\/\/user:pass@proxyserver:port&#39;,<\/p>\n<p>    &#39;https&#39;: &#39;https:\/\/user:pass@proxyserver:port&#39;,<\/p>\n<p>}<\/p>\n<h2><strong>\u722c\u53d6\u591a\u4e2a\u7f51\u9875\u7684\u6570\u636e<\/strong><\/h2>\n<p>urls = [&#39;https:\/\/example.com\/page1&#39;, &#39;https:\/\/example.com\/page2&#39;, &#39;https:\/\/example.com\/page3&#39;]<\/p>\n<p>for url in urls:<\/p>\n<p>    response = requests.get(url, proxies=proxies)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<p>        # \u5904\u7406\u9875\u9762\u5185\u5bb9<\/p>\n<p>        print(soup.title.text)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Failed to retrieve {url}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8bbe\u7f6e<strong>proxies<\/strong>\u53c2\u6570\u4f7f\u7528\u4ee3\u7406IP\u8fdb\u884c\u722c\u53d6\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4fdd\u5b58\u722c\u53d6\u7684\u6570\u636e<\/p>\n<\/p>\n<p><p>\u722c\u53d6\u7684\u6570\u636e\u901a\u5e38\u9700\u8981\u4fdd\u5b58\u5230\u6587\u4ef6\u3001\u6570\u636e\u5e93\u6216\u5176\u4ed6\u5b58\u50a8\u4ecb\u8d28\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6570\u636e\u4fdd\u5b58\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4fdd\u5b58\u5230\u6587\u4ef6<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u4fdd\u5b58\u6570\u636e\u5230\u6587\u672c\u6587\u4ef6\u3001CSV\u6587\u4ef6\u7b49\u3002<\/li>\n<li><strong>\u4fdd\u5b58\u5230\u6570\u636e\u5e93<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528SQLite\u3001MySQL\u3001MongoDB\u7b49\u6570\u636e\u5e93\u4fdd\u5b58\u6570\u636e\u3002<\/li>\n<li><strong>\u4f7f\u7528Scrapy\u7684Item Pipeline<\/strong>\uff1a\u5728Scrapy\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7Item Pipeline\u5c06\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u3001\u6570\u636e\u5e93\u7b49\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4fdd\u5b58\u6570\u636e\u5230CSV\u6587\u4ef6\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>import csv<\/p>\n<h2><strong>\u722c\u53d6\u591a\u4e2a\u7f51\u9875\u7684\u6570\u636e<\/strong><\/h2>\n<p>urls = [&#39;https:\/\/example.com\/page1&#39;, &#39;https:\/\/example.com\/page2&#39;, &#39;https:\/\/example.com\/page3&#39;]<\/p>\n<h2><strong>\u6253\u5f00CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.csv&#39;, mode=&#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerow([&#39;Title&#39;])<\/p>\n<p>    for url in urls:<\/p>\n<p>        response = requests.get(url)<\/p>\n<p>        if response.status_code == 200:<\/p>\n<p>            soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<p>            # \u63d0\u53d6\u9875\u9762\u6570\u636e<\/p>\n<p>            title = soup.title.text<\/p>\n<p>            writer.writerow([title])<\/p>\n<p>        else:<\/p>\n<p>            print(f&quot;Failed to retrieve {url}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<strong>csv<\/strong>\u5e93\u5c06\u722c\u53d6\u7684\u6570\u636e\u4fdd\u5b58\u5230CSV\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u6570\u636e\u6e05\u6d17\u548c\u5904\u7406<\/p>\n<\/p>\n<p><p>\u722c\u53d6\u7684\u6570\u636e\u901a\u5e38\u9700\u8981\u8fdb\u884c\u6e05\u6d17\u548c\u5904\u7406\uff0c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\u548c\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6570\u636e\u6e05\u6d17\u548c\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u53bb\u9664HTML\u6807\u7b7e<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u53bb\u9664HTML\u6807\u7b7e\u3002<\/li>\n<li><strong>\u5904\u7406\u7f3a\u5931\u503c<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u586b\u5145\u3001\u5220\u9664\u7b49\u65b9\u6cd5\u5904\u7406\u7f3a\u5931\u503c\u3002<\/li>\n<li><strong>\u6570\u636e\u8f6c\u6362<\/strong>\uff1a\u53ef\u4ee5\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u9002\u5f53\u7684\u683c\u5f0f\uff0c\u5982\u65e5\u671f\u3001\u6570\u503c\u7b49\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u53bb\u9664HTML\u6807\u7b7e\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>import re<\/p>\n<h2><strong>\u722c\u53d6\u591a\u4e2a\u7f51\u9875\u7684\u6570\u636e<\/strong><\/h2>\n<p>urls = [&#39;https:\/\/example.com\/page1&#39;, &#39;https:\/\/example.com\/page2&#39;, &#39;https:\/\/example.com\/page3&#39;]<\/p>\n<p>for url in urls:<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<p>        # \u63d0\u53d6\u5e76\u6e05\u6d17\u9875\u9762\u6570\u636e<\/p>\n<p>        raw_text = soup.get_text()<\/p>\n<p>        clean_text = re.sub(r&#39;\\s+&#39;, &#39; &#39;, raw_text)<\/p>\n<p>        print(clean_text)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Failed to retrieve {url}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<strong>re.sub<\/strong>\u51fd\u6570\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u767d\u5b57\u7b26\uff0c\u4ece\u800c\u6e05\u6d17\u9875\u9762\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u5e76\u53d1\u722c\u53d6<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u722c\u53d6\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u6216\u5f02\u6b65IO\u8fdb\u884c\u5e76\u53d1\u722c\u53d6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e76\u53d1\u722c\u53d6\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u591a\u7ebf\u7a0b<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u5e93\u8fdb\u884c\u591a\u7ebf\u7a0b\u722c\u53d6\u3002<\/li>\n<li><strong>\u591a\u8fdb\u7a0b<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>multiprocessing<\/code>\u5e93\u8fdb\u884c\u591a\u8fdb\u7a0b\u722c\u53d6\u3002<\/li>\n<li><strong>\u5f02\u6b65IO<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>asyncio<\/code>\u5e93\u8fdb\u884c\u5f02\u6b65IO\u722c\u53d6\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u591a\u7ebf\u7a0b\u8fdb\u884c\u5e76\u53d1\u722c\u53d6\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>import threading<\/p>\n<h2><strong>\u722c\u53d6\u591a\u4e2a\u7f51\u9875\u7684\u6570\u636e<\/strong><\/h2>\n<p>urls = [&#39;https:\/\/example.com\/page1&#39;, &#39;https:\/\/example.com\/page2&#39;, &#39;https:\/\/example.com\/page3&#39;]<\/p>\n<p>def fetch_data(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<p>        print(soup.title.text)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Failed to retrieve {url}&quot;)<\/p>\n<p>threads = []<\/p>\n<p>for url in urls:<\/p>\n<p>    thread = threading.Thread(target=fetch_data, args=(url,))<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>threading.Thread<\/code>\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\uff0c\u5e76\u53d1\u5730\u722c\u53d6\u591a\u4e2a\u7f51\u9875\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u5faa\u73af\u722c\u53d6\u6570\u636e\uff0c\u5e76\u5904\u7406\u5404\u79cd\u4e0d\u540c\u7684\u9700\u6c42\u548c\u6311\u6218\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684for\u5faa\u73af\u3001while\u5faa\u73af\uff0c\u8fd8\u662f\u4f7f\u7528Scrapy\u6846\u67b6\u3001Selenium\u5de5\u5177\u8fdb\u884c\u52a8\u6001\u7f51\u9875\u722c\u53d6\uff0c\u90fd\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u6570\u636e\u722c\u53d6\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u4f8b\u5982\u53cd\u722c\u673a\u5236\u3001\u6570\u636e\u6e05\u6d17\u3001\u5e76\u53d1\u722c\u53d6\u7b49\u3002\u901a\u8fc7\u638c\u63e1\u4e0a\u8ff0\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u60a8\u5c06\u80fd\u591f\u66f4\u597d\u5730\u5e94\u5bf9\u8fd9\u4e9b\u6311\u6218\uff0c\u5e76\u9ad8\u6548\u5730\u5b8c\u6210\u6570\u636e\u722c\u53d6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u5faa\u73af\u722c\u53d6\u6570\u636e\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fdb\u884c\u5faa\u73af\u722c\u53d6\u6570\u636e\u65f6\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u5982<code>requests<\/code>\u548c<code>BeautifulSoup<\/code>\u3002\u63a5\u7740\uff0c\u8bbe\u7f6e\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\uff08\u5982<code>for<\/code>\u6216<code>while<\/code>\uff09\u6765\u591a\u6b21\u53d1\u9001\u8bf7\u6c42\u3002\u5728\u6bcf\u6b21\u8bf7\u6c42\u4e2d\uff0c\u89e3\u6790\u8fd4\u56de\u7684HTML\u5185\u5bb9\uff0c\u63d0\u53d6\u6240\u9700\u6570\u636e\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5217\u8868\u6216\u5b57\u5178\u4e2d\u3002\u786e\u4fdd\u5728\u8bf7\u6c42\u4e4b\u95f4\u8bbe\u7f6e\u9002\u5f53\u7684\u5ef6\u65f6\uff0c\u4ee5\u907f\u514d\u5bf9\u76ee\u6807\u7f51\u7ad9\u9020\u6210\u8fc7\u5927\u538b\u529b\u3002<\/p>\n<p><strong>\u5728\u722c\u53d6\u6570\u636e\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7f51\u7ad9\u7684\u53cd\u722c\u866b\u673a\u5236\uff1f<\/strong><br \/>\u4e3a\u4e86\u5e94\u5bf9\u7f51\u7ad9\u7684\u53cd\u722c\u866b\u673a\u5236\uff0c\u53ef\u4ee5\u91c7\u53d6\u591a\u79cd\u7b56\u7565\uff0c\u4f8b\u5982\u968f\u673a\u66f4\u6362\u8bf7\u6c42\u5934\uff08User-Agent\uff09\uff0c\u4f7f\u7528\u4ee3\u7406IP\u6765\u9690\u85cf\u771f\u5b9eIP\u5730\u5740\uff0c\u4ee5\u53ca\u589e\u52a0\u8bf7\u6c42\u95f4\u9694\u65f6\u95f4\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6a21\u62df\u6d4f\u89c8\u5668\u7684\u5de5\u5177\uff0c\u5982<code>Selenium<\/code>\uff0c\u4ee5\u66f4\u771f\u5b9e\u7684\u65b9\u5f0f\u4e0e\u7f51\u7ad9\u4ea4\u4e92\uff0c\u4ece\u800c\u51cf\u5c11\u88ab\u5c01\u7981\u7684\u98ce\u9669\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u5faa\u73af\u722c\u53d6\u7684\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u5b8c\u6574\u6027\uff1f<\/strong><br \/>\u786e\u4fdd\u6570\u636e\u51c6\u786e\u6027\u548c\u5b8c\u6574\u6027\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5728\u722c\u53d6\u6570\u636e\u524d\uff0c\u63d0\u524d\u68c0\u67e5\u76ee\u6807\u7f51\u9875\u7684\u7ed3\u6784\uff0c\u4ee5\u4fbf\u6b63\u786e\u5b9a\u4f4d\u6240\u9700\u4fe1\u606f\u3002\u540c\u65f6\uff0c\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff0c\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff0c\u907f\u514d\u7a0b\u5e8f\u56e0\u5355\u4e00\u9519\u8bef\u800c\u4e2d\u65ad\u3002\u5b9a\u671f\u5bf9\u722c\u53d6\u7684\u6570\u636e\u8fdb\u884c\u9a8c\u8bc1\uff0c\u786e\u4fdd\u5176\u4e0e\u7f51\u7ad9\u4e0a\u7684\u5b9e\u9645\u5185\u5bb9\u76f8\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5faa\u73af\u722c\u53d6\u6570\u636e\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u7ed3\u5408\u65f6\u95f4\u95f4\u9694\u8fdb\u884c\u722c\u53d6\u3002\u53ef\u4ee5\u5229\u7528Scrapy [&hellip;]","protected":false},"author":3,"featured_media":1165358,"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\/1165351"}],"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=1165351"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165351\/revisions"}],"predecessor-version":[{"id":1165360,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165351\/revisions\/1165360"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1165358"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1165351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1165351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1165351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}