{"id":1086914,"date":"2025-01-08T13:30:14","date_gmt":"2025-01-08T05:30:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1086914.html"},"modified":"2025-01-08T13:30:16","modified_gmt":"2025-01-08T05:30:16","slug":"python%e5%a6%82%e4%bd%95%e7%88%ac%e5%8f%96%e7%99%be%e5%ba%a6%e6%8c%87%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1086914.html","title":{"rendered":"python\u5982\u4f55\u722c\u53d6\u767e\u5ea6\u6307\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200110\/0fc7aabc-cdff-4476-bad7-32ad8bc60331.webp\" alt=\"python\u5982\u4f55\u722c\u53d6\u767e\u5ea6\u6307\u6570\" \/><\/p>\n<p><p> \u767e\u5ea6\u6307\u6570\uff08B<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>du Index\uff09\u662f\u767e\u5ea6\u63d0\u4f9b\u7684\u4e00\u4e2a\u4e13\u4e1a\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u5e2e\u52a9\u7528\u6237\u4e86\u89e3\u5173\u952e\u8bcd\u7684\u8d8b\u52bf\u548c\u6570\u636e\u3002\u8981\u7528Python\u722c\u53d6\u767e\u5ea6\u6307\u6570\uff0c\u901a\u5e38\u9700\u8981\u901a\u8fc7\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u5e76\u5904\u7406JavaScript\u52a0\u8f7d\u6570\u636e\u7684\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u6307\u5357\uff0c\u6559\u4f60\u5982\u4f55\u7528Python\u722c\u53d6\u767e\u5ea6\u6307\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Selenium\u3001\u5904\u7406JavaScript\u52a0\u8f7d\u3001\u6a21\u4eff\u4eba\u7c7b\u884c\u4e3a<\/strong><\/p>\n<\/p>\n<p><p><strong>Selenium<\/strong> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u5904\u7406JavaScript\u52a0\u8f7d\u7684\u9875\u9762\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Selenium\u722c\u53d6\u767e\u5ea6\u6307\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Selenium<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5Selenium\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e0b\u8f7d\u6d4f\u89c8\u5668\u9a71\u52a8<\/strong>\uff1aSelenium\u9700\u8981\u6d4f\u89c8\u5668\u9a71\u52a8\u6765\u63a7\u5236\u6d4f\u89c8\u5668\u3002\u4ee5Chrome\u4e3a\u4f8b\uff0c\u4e0b\u8f7d\u5bf9\u5e94\u7248\u672c\u7684ChromeDriver\uff1a<a href=\"https:\/\/sites.google.com\/a\/chromium.org\/chromedriver\/downloads\">https:\/\/sites.google.com\/a\/chromium.org\/chromedriver\/downloads<\/a><\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5\u5176\u4ed6\u4f9d\u8d56\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p>pip install time<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Selenium\u722c\u53d6\u767e\u5ea6\u6307\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\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>from selenium.webdriver.common.by import By<\/p>\n<p>from selenium.webdriver.support.ui import WebDriverWait<\/p>\n<p>from selenium.webdriver.support import expected_conditions as EC<\/p>\n<p>import time<\/p>\n<p>import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521d\u59cb\u5316WebDriver<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316Chrome WebDriver<\/p>\n<p>options = webdriver.ChromeOptions()<\/p>\n<p>options.add_argument(&#39;--headless&#39;)  # \u65e0\u5934\u6a21\u5f0f<\/p>\n<p>driver = webdriver.Chrome(executable_path=&#39;path\/to\/chromedriver&#39;, options=options)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6253\u5f00\u767e\u5ea6\u6307\u6570\u9875\u9762\u5e76\u641c\u7d22\u5173\u952e\u8bcd<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def search_keyword(keyword):<\/p>\n<p>    driver.get(&quot;https:\/\/index.baidu.com&quot;)<\/p>\n<p>    # \u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210<\/p>\n<p>    wait = WebDriverWait(driver, 10)<\/p>\n<p>    # \u627e\u5230\u641c\u7d22\u6846\u5e76\u8f93\u5165\u5173\u952e\u8bcd<\/p>\n<p>    search_box = wait.until(EC.presence_of_element_located((By.CLASS_NAME, &#39;search-input&#39;)))<\/p>\n<p>    search_box.send_keys(keyword)<\/p>\n<p>    search_box.send_keys(Keys.RETURN)<\/p>\n<p>    # \u7b49\u5f85\u641c\u7d22\u7ed3\u679c\u52a0\u8f7d\u5b8c\u6210<\/p>\n<p>    time.sleep(5)  # \u53ef\u4ee5\u6839\u636e\u7f51\u7edc\u60c5\u51b5\u8c03\u6574\u65f6\u95f4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u83b7\u53d6\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def get_data():<\/p>\n<p>    # \u7b49\u5f85\u6570\u636e\u56fe\u8868\u52a0\u8f7d\u5b8c\u6210<\/p>\n<p>    wait = WebDriverWait(driver, 10)<\/p>\n<p>    chart = wait.until(EC.presence_of_element_located((By.CLASS_NAME, &#39;index-trend-chart&#39;)))<\/p>\n<p>    # \u83b7\u53d6\u6570\u636e<\/p>\n<p>    data_points = chart.find_elements_by_class_name(&#39;index-trend-item&#39;)<\/p>\n<p>    data = []<\/p>\n<p>    for point in data_points:<\/p>\n<p>        date = point.get_attribute(&#39;data-date&#39;)<\/p>\n<p>        value = point.get_attribute(&#39;data-index&#39;)<\/p>\n<p>        data.append({&#39;date&#39;: date, &#39;value&#39;: value})<\/p>\n<p>    return data<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u4fdd\u5b58\u6570\u636e\u5230CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def save_to_csv(data, filename):<\/p>\n<p>    df = pd.DataFrame(data)<\/p>\n<p>    df.to_csv(filename, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6\u3001\u4e3b\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">if __name__ == &quot;__main__&quot;:<\/p>\n<p>    keyword = &quot;Python&quot;<\/p>\n<p>    search_keyword(keyword)<\/p>\n<p>    data = get_data()<\/p>\n<p>    save_to_csv(data, &#39;baidu_index.csv&#39;)<\/p>\n<p>    driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u53cd\u722c\u7b56\u7565<\/strong>\uff1a\u767e\u5ea6\u6307\u6570\u6709\u53cd\u722c\u7b56\u7565\uff0c\u9891\u7e41\u8bbf\u95ee\u53ef\u80fd\u4f1a\u88ab\u5c01\u7981\u3002\u5efa\u8bae\u5728\u722c\u53d6\u8fc7\u7a0b\u4e2d\u52a0\u5165\u968f\u673a\u65f6\u95f4\u95f4\u9694\uff0c\u6a21\u4eff\u4eba\u7c7b\u884c\u4e3a\u3002<\/li>\n<li><strong>\u6d4f\u89c8\u5668\u8bbe\u7f6e<\/strong>\uff1a\u4f7f\u7528\u65e0\u5934\u6a21\u5f0f\uff08headless\uff09\u53ef\u4ee5\u907f\u514d\u6d4f\u89c8\u5668\u7a97\u53e3\u5f39\u51fa\uff0c\u4f46\u8c03\u8bd5\u65f6\u53ef\u4ee5\u5173\u95ed\u65e0\u5934\u6a21\u5f0f\uff0c\u65b9\u4fbf\u89c2\u5bdf\u6d4f\u89c8\u5668\u884c\u4e3a\u3002<\/li>\n<li><strong>\u4ee3\u7406\u548cCookies<\/strong>\uff1a\u5982\u679c\u9891\u7e41\u88ab\u5c01\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4ee3\u7406\u548cCookies\uff0c\u6a21\u62df\u771f\u5b9e\u7528\u6237\u884c\u4e3a\u3002<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u548cSelenium\u722c\u53d6\u767e\u5ea6\u6307\u6570\u6570\u636e\u3002Selenium\u53ef\u4ee5\u5904\u7406JavaScript\u52a0\u8f7d\u7684\u9875\u9762\uff0c\u6a21\u4eff\u4eba\u7c7b\u884c\u4e3a\uff0c\u7ed5\u8fc7\u7b80\u5355\u7684\u53cd\u722c\u7b56\u7565\u3002\u6ce8\u610f\u9002\u5f53\u7684\u65f6\u95f4\u95f4\u9694\u548c\u4ee3\u7406\u4f7f\u7528\uff0c\u4ee5\u63d0\u9ad8\u722c\u53d6\u7684\u7a33\u5b9a\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u767e\u5ea6\u6307\u6570\u6570\u636e\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u83b7\u53d6\u767e\u5ea6\u6307\u6570\u6570\u636e\uff0c\u60a8\u53ef\u4ee5\u5229\u7528\u722c\u866b\u5e93\u5982Requests\u548cBeautifulSoup\u6765\u53d1\u9001\u8bf7\u6c42\u5e76\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3002\u9996\u5148\uff0c\u901a\u8fc7Requests\u5e93\u83b7\u53d6\u7f51\u9875\u7684HTML\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528BeautifulSoup\u89e3\u6790\u6570\u636e\uff0c\u63d0\u53d6\u60a8\u9700\u8981\u7684\u6307\u6570\u4fe1\u606f\u3002\u8bf7\u786e\u4fdd\u9075\u5b88\u767e\u5ea6\u7684\u4f7f\u7528\u6761\u6b3e\uff0c\u5e76\u8003\u8651\u4f7f\u7528API\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<p><strong>\u5728\u722c\u53d6\u767e\u5ea6\u6307\u6570\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6cd5\u5f8b\u548c\u9053\u5fb7\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u722c\u53d6\u4efb\u4f55\u7f51\u7ad9\u7684\u6570\u636e\u65f6\uff0c\u9075\u5faa\u6cd5\u5f8b\u548c\u9053\u5fb7\u6807\u51c6\u975e\u5e38\u91cd\u8981\u3002\u786e\u4fdd\u60a8\u9075\u5b88\u767e\u5ea6\u7684robots.txt\u6587\u4ef6\u4e2d\u7684\u89c4\u5219\uff0c\u907f\u514d\u5bf9\u670d\u52a1\u5668\u9020\u6210\u8fc7\u5927\u8d1f\u62c5\u3002\u6b64\u5916\uff0c\u5c0a\u91cd\u6570\u636e\u9690\u79c1\uff0c\u907f\u514d\u6536\u96c6\u654f\u611f\u4fe1\u606f\uff0c\u5e76\u5728\u4f7f\u7528\u6570\u636e\u65f6\u6ce8\u660e\u6765\u6e90\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u722c\u53d6\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u53cd\u722c\u866b\u673a\u5236\uff1f<\/strong><br \/>\u767e\u5ea6\u548c\u8bb8\u591a\u5176\u4ed6\u7f51\u7ad9\u4f1a\u5b9e\u65bd\u53cd\u722c\u866b\u673a\u5236\u6765\u4fdd\u62a4\u5176\u6570\u636e\u3002\u4e3a\u5e94\u5bf9\u8fd9\u4e9b\u63aa\u65bd\uff0c\u60a8\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528\u968f\u673a\u7684\u8bf7\u6c42\u5934\u4ee5\u6a21\u62df\u6b63\u5e38\u7528\u6237\u884c\u4e3a\uff0c\u8bbe\u7f6e\u8bf7\u6c42\u95f4\u9694\u65f6\u95f4\u4ee5\u907f\u514d\u9891\u7e41\u8bbf\u95ee\uff0c\u751a\u81f3\u4f7f\u7528\u4ee3\u7406IP\u6765\u9690\u85cf\u771f\u5b9eIP\u5730\u5740\u3002\u6709\u6548\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u722c\u53d6\u7684\u6210\u529f\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u767e\u5ea6\u6307\u6570\uff08BAIdu Index\uff09\u662f\u767e\u5ea6\u63d0\u4f9b\u7684\u4e00\u4e2a\u4e13\u4e1a\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u5e2e\u52a9\u7528\u6237\u4e86\u89e3\u5173\u952e\u8bcd\u7684\u8d8b\u52bf\u548c\u6570\u636e\u3002\u8981\u7528Pyt [&hellip;]","protected":false},"author":3,"featured_media":1086918,"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\/1086914"}],"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=1086914"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086914\/revisions"}],"predecessor-version":[{"id":1086920,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086914\/revisions\/1086920"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1086918"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1086914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1086914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1086914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}