{"id":1026594,"date":"2024-12-31T10:43:56","date_gmt":"2024-12-31T02:43:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1026594.html"},"modified":"2024-12-31T10:43:59","modified_gmt":"2024-12-31T02:43:59","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%9c%a8%e8%b0%b7%e6%ad%8c%e4%b8%8a%e7%88%ac%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1026594.html","title":{"rendered":"\u5982\u4f55\u7528python\u5728\u8c37\u6b4c\u4e0a\u722c\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/04dd4df6-a0e7-4473-84f6-8a5a41aa145d.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u5728\u8c37\u6b4c\u4e0a\u722c\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5728\u8c37\u6b4c\u4e0a\u722c\u6570\u636e\u6709\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528Selenium\u81ea\u52a8\u5316\u6d4f\u89c8\u5668\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u3001\u4f7f\u7528Google Custom Search API\u3001\u901a\u8fc7Scrapy\u6846\u67b6\u8fdb\u884c\u722c\u53d6\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528Selenium\u81ea\u52a8\u5316\u6d4f\u89c8\u5668\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u6a21\u62df\u7528\u6237\u884c\u4e3a\uff0c\u7ed5\u8fc7\u4e00\u4e9b\u53cd\u722c\u866b\u673a\u5236\u3002\u4f8b\u5982\uff0cSelenium\u53ef\u4ee5\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u8f93\u5165\u5173\u952e\u8bcd\uff0c\u7ffb\u9875\uff0c\u5e76\u6293\u53d6\u641c\u7d22\u7ed3\u679c\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Selenium\u6765\u5728\u8c37\u6b4c\u4e0a\u722c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6e\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Selenium\u548cWebDriver\u3002Selenium\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u63a7\u5236\u6d4f\u89c8\u5668\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u800cWebDriver\u662fSelenium\u7528\u6765\u4e0e\u6d4f\u89c8\u5668\u8fdb\u884c\u4ea4\u4e92\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002\u4e3a\u4e86\u4f7f\u7528Selenium\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u4e00\u4e2a\u652f\u6301\u7684\u6d4f\u89c8\u5668\uff0c\u4f8b\u5982Google Chrome\uff0c\u4ee5\u53ca\u76f8\u5e94\u7684ChromeDriver\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u597dSelenium\u540e\uff0c\u6211\u4eec\u9700\u8981\u4e0b\u8f7d\u9002\u5408\u81ea\u5df1Chrome\u7248\u672c\u7684ChromeDriver\uff0c\u5e76\u5c06\u5176\u8def\u5f84\u914d\u7f6e\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u4f60\u53ef\u4ee5\u4ece<a href=\"https:\/\/sites.google.com\/a\/chromium.org\/chromedriver\/downloads\">ChromeDriver\u5b98\u65b9\u7f51\u7ad9<\/a>\u4e0b\u8f7d\u9002\u5408\u4f60\u7248\u672c\u7684ChromeDriver\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Selenium\u6253\u5f00\u8c37\u6b4c\u5e76\u641c\u7d22\u5173\u952e\u8bcd<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165Selenium\u5e93\uff0c\u5e76\u8bbe\u7f6eChromeDriver\u8def\u5f84\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Selenium\u6253\u5f00\u8c37\u6b4c\u4e3b\u9875\uff0c\u5e76\u8f93\u5165\u641c\u7d22\u5173\u952e\u8bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.common.keys import Keys<\/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<p>driver = webdriver.Chrome(driver_path)<\/p>\n<h2><strong>\u6253\u5f00\u8c37\u6b4c\u4e3b\u9875<\/strong><\/h2>\n<p>driver.get(&quot;http:\/\/www.google.com&quot;)<\/p>\n<h2><strong>\u627e\u5230\u641c\u7d22\u8f93\u5165\u6846<\/strong><\/h2>\n<p>search_box = driver.find_element_by_name(&quot;q&quot;)<\/p>\n<h2><strong>\u8f93\u5165\u641c\u7d22\u5173\u952e\u8bcd\u5e76\u56de\u8f66<\/strong><\/h2>\n<p>search_box.send_keys(&quot;Python web scraping&quot;)<\/p>\n<p>search_box.send_keys(Keys.RETURN)<\/p>\n<h2><strong>\u7b49\u5f85\u9875\u9762\u52a0\u8f7d<\/strong><\/h2>\n<p>time.sleep(3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790\u641c\u7d22\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u5728\u641c\u7d22\u7ed3\u679c\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u9700\u8981\u89e3\u6790\u9875\u9762\u5185\u5bb9\uff0c\u63d0\u53d6\u6211\u4eec\u9700\u8981\u7684\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Selenium\u7684<code>find_elements_by_xpath<\/code>\u65b9\u6cd5\u6765\u5b9a\u4f4d\u641c\u7d22\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u6240\u6709\u641c\u7d22\u7ed3\u679c<\/p>\n<p>results = driver.find_elements_by_xpath(&#39;\/\/div[@class=&quot;g&quot;]&#39;)<\/p>\n<h2><strong>\u904d\u5386\u641c\u7d22\u7ed3\u679c<\/strong><\/h2>\n<p>for result in results:<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u6807\u9898<\/p>\n<p>    title = result.find_element_by_xpath(&#39;.\/\/h3&#39;).text<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u94fe\u63a5<\/p>\n<p>    link = result.find_element_by_xpath(&#39;.\/\/a&#39;).get_attribute(&#39;href&#39;)<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u63cf\u8ff0<\/p>\n<p>    description = result.find_element_by_xpath(&#39;.\/\/span[@class=&quot;st&quot;]&#39;).text<\/p>\n<p>    print(f&#39;Title: {title}&#39;)<\/p>\n<p>    print(f&#39;Link: {link}&#39;)<\/p>\n<p>    print(f&#39;Description: {description}&#39;)<\/p>\n<p>    print(&#39;-&#39; * 80)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5206\u9875<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u591a\u4e2a\u9875\u9762\u7684\u641c\u7d22\u7ed3\u679c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u5206\u9875\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Selenium\u70b9\u51fb\u201c\u4e0b\u4e00\u9875\u201d\u6309\u94ae\uff0c\u7136\u540e\u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\u6293\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u4e0b\u4e00\u9875\u6309\u94ae<\/p>\n<p>next_button = driver.find_element_by_xpath(&#39;\/\/a[@id=&quot;pnnext&quot;]&#39;)<\/p>\n<h2><strong>\u70b9\u51fb\u4e0b\u4e00\u9875\u6309\u94ae<\/strong><\/h2>\n<p>next_button.click()<\/p>\n<h2><strong>\u7b49\u5f85\u9875\u9762\u52a0\u8f7d<\/strong><\/h2>\n<p>time.sleep(3)<\/p>\n<h2><strong>\u91cd\u65b0\u83b7\u53d6\u641c\u7d22\u7ed3\u679c<\/strong><\/h2>\n<p>results = driver.find_elements_by_xpath(&#39;\/\/div[@class=&quot;g&quot;]&#39;)<\/p>\n<h2><strong>\u904d\u5386\u641c\u7d22\u7ed3\u679c<\/strong><\/h2>\n<p>for result in results:<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u6807\u9898<\/p>\n<p>    title = result.find_element_by_xpath(&#39;.\/\/h3&#39;).text<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u94fe\u63a5<\/p>\n<p>    link = result.find_element_by_xpath(&#39;.\/\/a&#39;).get_attribute(&#39;href&#39;)<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u63cf\u8ff0<\/p>\n<p>    description = result.find_element_by_xpath(&#39;.\/\/span[@class=&quot;st&quot;]&#39;).text<\/p>\n<p>    print(f&#39;Title: {title}&#39;)<\/p>\n<p>    print(f&#39;Link: {link}&#39;)<\/p>\n<p>    print(f&#39;Description: {description}&#39;)<\/p>\n<p>    print(&#39;-&#39; * 80)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u5355\u72ec\u4f7f\u7528Selenium\u53ef\u80fd\u4e0d\u591f\u9ad8\u6548\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408BeautifulSoup\u6765\u89e3\u6790HTML\u5185\u5bb9\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u63d0\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u83b7\u53d6\u9875\u9762HTML\u5185\u5bb9<\/strong><\/h2>\n<p>html = driver.page_source<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(html, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u641c\u7d22\u7ed3\u679c<\/strong><\/h2>\n<p>results = soup.find_all(&#39;div&#39;, class_=&#39;g&#39;)<\/p>\n<h2><strong>\u904d\u5386\u641c\u7d22\u7ed3\u679c<\/strong><\/h2>\n<p>for result in results:<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u6807\u9898<\/p>\n<p>    title = result.find(&#39;h3&#39;).text<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u94fe\u63a5<\/p>\n<p>    link = result.find(&#39;a&#39;)[&#39;href&#39;]<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u63cf\u8ff0<\/p>\n<p>    description = result.find(&#39;span&#39;, class_=&#39;st&#39;).text<\/p>\n<p>    print(f&#39;Title: {title}&#39;)<\/p>\n<p>    print(f&#39;Link: {link}&#39;)<\/p>\n<p>    print(f&#39;Description: {description}&#39;)<\/p>\n<p>    print(&#39;-&#39; * 80)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Google Custom Search API<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4e0d\u60f3\u4f7f\u7528Selenium\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Google Custom Search API\u3002\u4f60\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u641c\u7d22\u5f15\u64ce\uff0c\u5e76\u83b7\u53d6API\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u8bbe\u7f6eAPI\u5bc6\u94a5\u548c\u641c\u7d22\u5f15\u64ceID<\/strong><\/h2>\n<p>api_key = &#39;YOUR_API_KEY&#39;<\/p>\n<p>search_engine_id = &#39;YOUR_SEARCH_ENGINE_ID&#39;<\/p>\n<h2><strong>\u8bbe\u7f6e\u641c\u7d22\u5173\u952e\u8bcd<\/strong><\/h2>\n<p>query = &#39;Python web scraping&#39;<\/p>\n<h2><strong>\u6784\u5efaAPI\u8bf7\u6c42URL<\/strong><\/h2>\n<p>url = f&#39;https:\/\/www.googleapis.com\/customsearch\/v1?q={query}&amp;key={api_key}&amp;cx={search_engine_id}&#39;<\/p>\n<h2><strong>\u53d1\u9001API\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(url)<\/p>\n<h2><strong>\u89e3\u6790API\u54cd\u5e94<\/strong><\/h2>\n<p>data = response.json()<\/p>\n<h2><strong>\u904d\u5386\u641c\u7d22\u7ed3\u679c<\/strong><\/h2>\n<p>for item in data[&#39;items&#39;]:<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u6807\u9898<\/p>\n<p>    title = item[&#39;title&#39;]<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u94fe\u63a5<\/p>\n<p>    link = item[&#39;link&#39;]<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u63cf\u8ff0<\/p>\n<p>    description = item[&#39;snippet&#39;]<\/p>\n<p>    print(f&#39;Title: {title}&#39;)<\/p>\n<p>    print(f&#39;Link: {link}&#39;)<\/p>\n<p>    print(f&#39;Description: {description}&#39;)<\/p>\n<p>    print(&#39;-&#39; * 80)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528Scrapy\u6846\u67b6<\/h3>\n<\/p>\n<p><p>Scrapy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u722c\u866b\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u9ad8\u6548\u5730\u6293\u53d6\u548c\u89e3\u6790\u7f51\u9875\u6570\u636e\u3002\u4f7f\u7528Scrapy\uff0c\u4f60\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u5927\u89c4\u6a21\u722c\u53d6\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scrapy<\/p>\n<p>class GoogleSpider(scrapy.Spider):<\/p>\n<p>    name = &quot;google_spider&quot;<\/p>\n<p>    allowed_dom<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns = [&quot;google.com&quot;]<\/p>\n<p>    start_urls = [&#39;https:\/\/www.google.com\/search?q=Python+web+scraping&#39;]<\/p>\n<p>    def parse(self, response):<\/p>\n<p>        # \u83b7\u53d6\u6240\u6709\u641c\u7d22\u7ed3\u679c<\/p>\n<p>        results = response.xpath(&#39;\/\/div[@class=&quot;g&quot;]&#39;)<\/p>\n<p>        for result in results:<\/p>\n<p>            # \u83b7\u53d6\u7ed3\u679c\u6807\u9898<\/p>\n<p>            title = result.xpath(&#39;.\/\/h3\/text()&#39;).get()<\/p>\n<p>            # \u83b7\u53d6\u7ed3\u679c\u94fe\u63a5<\/p>\n<p>            link = result.xpath(&#39;.\/\/a\/@href&#39;).get()<\/p>\n<p>            # \u83b7\u53d6\u7ed3\u679c\u63cf\u8ff0<\/p>\n<p>            description = result.xpath(&#39;.\/\/span[@class=&quot;st&quot;]\/text()&#39;).get()<\/p>\n<p>            yield {<\/p>\n<p>                &#39;title&#39;: title,<\/p>\n<p>                &#39;link&#39;: link,<\/p>\n<p>                &#39;description&#39;: description<\/p>\n<p>            }<\/p>\n<p>        # \u83b7\u53d6\u4e0b\u4e00\u9875\u94fe\u63a5<\/p>\n<p>        next_page = response.xpath(&#39;\/\/a[@id=&quot;pnnext&quot;]\/@href&#39;).get()<\/p>\n<p>        if next_page:<\/p>\n<p>            yield scrapy.Request(response.urljoin(next_page), callback=self.parse)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u53cd\u722c\u866b\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u5728\u722c\u53d6\u8c37\u6b4c\u6570\u636e\u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u53cd\u722c\u866b\u673a\u5236\uff0c\u4f8b\u5982CAPTCHA\u9a8c\u8bc1\u3002\u4e3a\u4e86\u7ed5\u8fc7\u8fd9\u4e9b\u673a\u5236\uff0c\u4f60\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u589e\u52a0\u8bf7\u6c42\u95f4\u9694<\/strong>\uff1a\u5728\u6bcf\u6b21\u8bf7\u6c42\u4e4b\u95f4\u589e\u52a0\u968f\u673a\u7684\u7b49\u5f85\u65f6\u95f4\uff0c\u4ee5\u6a21\u62df\u4eba\u7c7b\u884c\u4e3a\u3002<\/li>\n<li><strong>\u4f7f\u7528\u4ee3\u7406<\/strong>\uff1a\u901a\u8fc7\u4ee3\u7406\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u4ee5\u907f\u514dIP\u88ab\u5c01\u7981\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7528\u6237\u4ee3\u7406<\/strong>\uff1a\u5728\u8bf7\u6c42\u5934\u4e2d\u8bbe\u7f6e\u4e0d\u540c\u7684\u7528\u6237\u4ee3\u7406\uff0c\u4ee5\u6a21\u62df\u4e0d\u540c\u7684\u6d4f\u89c8\u5668\u548c\u8bbe\u5907\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from selenium.webdriver.common.by import By<\/p>\n<p>from selenium.webdriver.chrome.options import Options<\/p>\n<p>import random<\/p>\n<h2><strong>\u8bbe\u7f6eChrome\u9009\u9879<\/strong><\/h2>\n<p>chrome_options = Options()<\/p>\n<p>chrome_options.add_argument(&quot;--headless&quot;)<\/p>\n<p>chrome_options.add_argument(&quot;--disable-gpu&quot;)<\/p>\n<p>chrome_options.add_argument(&quot;user-agent=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<h2><strong>\u8bbe\u7f6e\u4ee3\u7406\u670d\u52a1\u5668<\/strong><\/h2>\n<p>chrome_options.add_argument(&#39;--proxy-server=http:\/\/your-proxy-server:port&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6eChromeDriver\u8def\u5f84<\/strong><\/h2>\n<p>driver = webdriver.Chrome(driver_path, options=chrome_options)<\/p>\n<h2><strong>\u6253\u5f00\u8c37\u6b4c\u4e3b\u9875<\/strong><\/h2>\n<p>driver.get(&quot;http:\/\/www.google.com&quot;)<\/p>\n<h2><strong>\u627e\u5230\u641c\u7d22\u8f93\u5165\u6846<\/strong><\/h2>\n<p>search_box = driver.find_element(By.NAME, &quot;q&quot;)<\/p>\n<h2><strong>\u8f93\u5165\u641c\u7d22\u5173\u952e\u8bcd\u5e76\u56de\u8f66<\/strong><\/h2>\n<p>search_box.send_keys(&quot;Python web scraping&quot;)<\/p>\n<p>search_box.send_keys(Keys.RETURN)<\/p>\n<h2><strong>\u7b49\u5f85\u9875\u9762\u52a0\u8f7d<\/strong><\/h2>\n<p>time.sleep(random.randint(2, 5))<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u641c\u7d22\u7ed3\u679c<\/strong><\/h2>\n<p>results = driver.find_elements(By.XPATH, &#39;\/\/div[@class=&quot;g&quot;]&#39;)<\/p>\n<h2><strong>\u904d\u5386\u641c\u7d22\u7ed3\u679c<\/strong><\/h2>\n<p>for result in results:<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u6807\u9898<\/p>\n<p>    title = result.find_element(By.XPATH, &#39;.\/\/h3&#39;).text<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u94fe\u63a5<\/p>\n<p>    link = result.find_element(By.XPATH, &#39;.\/\/a&#39;).get_attribute(&#39;href&#39;)<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u63cf\u8ff0<\/p>\n<p>    description = result.find_element(By.XPATH, &#39;.\/\/span[@class=&quot;st&quot;]&#39;).text<\/p>\n<p>    print(f&#39;Title: {title}&#39;)<\/p>\n<p>    print(f&#39;Link: {link}&#39;)<\/p>\n<p>    print(f&#39;Description: {description}&#39;)<\/p>\n<p>    print(&#39;-&#39; * 80)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u6570\u636e\u5b58\u50a8\u548c\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u722c\u53d6\u6570\u636e\u540e\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u5904\u7406\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684pandas\u5e93\u5c06\u6570\u636e\u5b58\u50a8\u5230CSV\u6587\u4ef6\u4e2d\uff0c\u6216\u8005\u4f7f\u7528SQLite\u6570\u636e\u5e93\u8fdb\u884c\u5b58\u50a8\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528pandas\u5b58\u50a8\u6570\u636e\u5230CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684DataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(columns=[&#39;Title&#39;, &#39;Link&#39;, &#39;Description&#39;])<\/p>\n<h2><strong>\u904d\u5386\u641c\u7d22\u7ed3\u679c<\/strong><\/h2>\n<p>for result in results:<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u6807\u9898<\/p>\n<p>    title = result.find_element(By.XPATH, &#39;.\/\/h3&#39;).text<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u94fe\u63a5<\/p>\n<p>    link = result.find_element(By.XPATH, &#39;.\/\/a&#39;).get_attribute(&#39;href&#39;)<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u63cf\u8ff0<\/p>\n<p>    description = result.find_element(By.XPATH, &#39;.\/\/span[@class=&quot;st&quot;]&#39;).text<\/p>\n<p>    # \u6dfb\u52a0\u6570\u636e\u5230DataFrame<\/p>\n<p>    df = df.append({&#39;Title&#39;: title, &#39;Link&#39;: link, &#39;Description&#39;: description}, ignore_index=True)<\/p>\n<h2><strong>\u5c06DataFrame\u5b58\u50a8\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;google_search_results.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528SQLite\u6570\u636e\u5e93\u5b58\u50a8\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93\uff08\u5982\u679c\u6570\u636e\u5e93\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u5e93\uff09<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;google_search_results.db&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aCursor\u5bf9\u8c61<\/strong><\/h2>\n<p>cur = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u8868<\/strong><\/h2>\n<p>cur.execute(&#39;&#39;&#39;<\/p>\n<p>    CREATE TABLE IF NOT EXISTS search_results (<\/p>\n<p>        id INTEGER PRIMARY KEY,<\/p>\n<p>        title TEXT,<\/p>\n<p>        link TEXT,<\/p>\n<p>        description TEXT<\/p>\n<p>    )<\/p>\n<p>&#39;&#39;&#39;)<\/p>\n<h2><strong>\u904d\u5386\u641c\u7d22\u7ed3\u679c<\/strong><\/h2>\n<p>for result in results:<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u6807\u9898<\/p>\n<p>    title = result.find_element(By.XPATH, &#39;.\/\/h3&#39;).text<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u94fe\u63a5<\/p>\n<p>    link = result.find_element(By.XPATH, &#39;.\/\/a&#39;).get_attribute(&#39;href&#39;)<\/p>\n<p>    # \u83b7\u53d6\u7ed3\u679c\u63cf\u8ff0<\/p>\n<p>    description = result.find_element(By.XPATH, &#39;.\/\/span[@class=&quot;st&quot;]&#39;).text<\/p>\n<p>    # \u63d2\u5165\u6570\u636e\u5230\u8868\u4e2d<\/p>\n<p>    cur.execute(&#39;&#39;&#39;<\/p>\n<p>        INSERT INTO search_results (title, link, description)<\/p>\n<p>        VALUES (?, ?, ?)<\/p>\n<p>    &#39;&#39;&#39;, (title, link, description))<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5728\u8c37\u6b4c\u4e0a\u722c\u6570\u636e\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002Selenium\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u884c\u4e3a\uff0c\u7ed5\u8fc7\u4e00\u4e9b\u53cd\u722c\u866b\u673a\u5236\uff1bBeautifulSoup\u53ef\u4ee5\u9ad8\u6548\u5730\u89e3\u6790HTML\u5185\u5bb9\uff1bGoogle Custom Search API\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u641c\u7d22\u7ed3\u679c\uff0c\u800c\u4e0d\u9700\u8981\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff1bScrapy\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u722c\u866b\u6846\u67b6\uff0c\u9002\u5408\u5927\u89c4\u6a21\u722c\u53d6\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u591a\u79cd\u6280\u672f\u624b\u6bb5\uff0c\u5904\u7406\u53cd\u722c\u866b\u673a\u5236\uff0c\u786e\u4fdd\u6570\u636e\u722c\u53d6\u7684\u7a33\u5b9a\u6027\u548c\u9ad8\u6548\u6027\u3002\u6700\u540e\uff0c\u5c06\u722c\u53d6\u7684\u6570\u636e\u5b58\u50a8\u5230\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u7f51\u9875\u722c\u53d6\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fdb\u884c\u7f51\u9875\u722c\u53d6\u65f6\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u5e38\u7528\u7684\u5e93\uff0c\u6bd4\u5982Requests\u548cBeautifulSoup\u3002Requests\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u800cBeautifulSoup\u5219\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863\u3002\u63a5\u7740\uff0c\u7f16\u5199\u4ee3\u7801\u6765\u8bf7\u6c42\u7279\u5b9a\u7684URL\uff0c\u83b7\u53d6\u9875\u9762\u7684HTML\u6e90\u4ee3\u7801\uff0c\u5e76\u5229\u7528BeautifulSoup\u63d0\u53d6\u4f60\u9700\u8981\u7684\u6570\u636e\uff0c\u6bd4\u5982\u6807\u9898\u3001\u94fe\u63a5\u6216\u5176\u4ed6\u4fe1\u606f\u3002\u6700\u540e\uff0c\u5904\u7406\u548c\u5b58\u50a8\u6293\u53d6\u5230\u7684\u6570\u636e\uff0c\u53ef\u80fd\u4f1a\u9009\u62e9\u5c06\u5176\u4fdd\u5b58\u5230CSV\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<p><strong>\u5728\u722c\u53d6\u8c37\u6b4c\u641c\u7d22\u7ed3\u679c\u65f6\uff0c\u6709\u54ea\u4e9b\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879\uff1f<\/strong><br \/>\u5f53\u722c\u53d6\u8c37\u6b4c\u641c\u7d22\u7ed3\u679c\u65f6\uff0c\u9700\u8981\u9075\u5faa\u8c37\u6b4c\u7684\u4f7f\u7528\u6761\u6b3e\uff0c\u907f\u514d\u8fc7\u4e8e\u9891\u7e41\u5730\u53d1\u9001\u8bf7\u6c42\uff0c\u4ee5\u514d\u88ab\u89c6\u4e3a\u6076\u610f\u884c\u4e3a\u800c\u88ab\u5c01\u7981IP\u3002\u4f7f\u7528\u4ee3\u7406IP\u548c\u9002\u5f53\u7684\u8bf7\u6c42\u95f4\u9694\u53ef\u4ee5\u5e2e\u52a9\u51cf\u5c11\u88ab\u5c01\u7684\u98ce\u9669\u3002\u6b64\u5916\uff0c\u8c37\u6b4c\u7684\u641c\u7d22\u7ed3\u679c\u9875\u9762\u662f\u52a8\u6001\u751f\u6210\u7684\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528Selenium\u7b49\u5de5\u5177\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\u6765\u83b7\u53d6\u6570\u636e\u3002\u786e\u4fdd\u9075\u5faarobots.txt\u6587\u4ef6\u4e2d\u7684\u89c4\u5219\uff0c\u4e86\u89e3\u54ea\u4e9b\u9875\u9762\u5141\u8bb8\u722c\u53d6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6293\u53d6\u5230\u7684\u6570\u636e\u4ee5\u4fbf\u8fdb\u884c\u5206\u6790\uff1f<\/strong><br \/>\u6293\u53d6\u5230\u7684\u6570\u636e\u901a\u5e38\u662f\u4ee5\u6587\u672c\u5f62\u5f0f\u5b58\u5728\uff0c\u9996\u5148\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u6e05\u6d17\uff0c\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u683c\u3001\u7279\u6b8a\u5b57\u7b26\u53caHTML\u6807\u7b7e\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u5c06\u6570\u636e\u52a0\u8f7d\u5230DataFrame\u4e2d\uff0c\u4fbf\u4e8e\u8fdb\u4e00\u6b65\u5206\u6790\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5982NumPy\u548cMatplotlib\uff0c\u5bf9\u6570\u636e\u8fdb\u884c\u7edf\u8ba1\u548c\u53ef\u89c6\u5316\u5206\u6790\uff0c\u5e2e\u52a9\u4f60\u4ece\u6293\u53d6\u7684\u6570\u636e\u4e2d\u63d0\u53d6\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5728\u8c37\u6b4c\u4e0a\u722c\u6570\u636e\u6709\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528Selenium\u81ea\u52a8\u5316\u6d4f\u89c8\u5668\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790 [&hellip;]","protected":false},"author":3,"featured_media":1026604,"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\/1026594"}],"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=1026594"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026594\/revisions"}],"predecessor-version":[{"id":1026607,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026594\/revisions\/1026607"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1026604"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1026594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1026594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1026594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}