{"id":1069164,"date":"2024-12-31T16:49:35","date_gmt":"2024-12-31T08:49:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069164.html"},"modified":"2024-12-31T16:49:38","modified_gmt":"2024-12-31T08:49:38","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e7%88%ac%e5%8f%96%e7%9f%a5%e7%bd%91%e6%96%87%e7%8c%ae%e6%91%98%e8%a6%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069164.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528python\u722c\u53d6\u77e5\u7f51\u6587\u732e\u6458\u8981"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/5a595ffe-f9a9-484e-b8e3-802c121a1d9b.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u4f7f\u7528python\u722c\u53d6\u77e5\u7f51\u6587\u732e\u6458\u8981\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u4f7f\u7528Python\u722c\u53d6\u77e5\u7f51\u6587\u732e\u6458\u8981<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u722c\u53d6\u77e5\u7f51\u6587\u732e\u6458\u8981\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Selenium\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\u3001\u5229\u7528requests\u5e93\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\u3001\u7ed3\u5408BeautifulSoup\u89e3\u6790HTML\u5185\u5bb9<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u7ed3\u5408\u8fd9\u4e9b\u6280\u672f\u6b65\u9aa4\u6765\u5b9e\u73b0\u722c\u53d6\u77e5\u7f51\u6587\u732e\u6458\u8981\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Selenium\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u6a21\u62df\u7528\u6237\u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u5404\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u70b9\u51fb\u3001\u8f93\u5165\u3001\u6eda\u52a8\u7b49\u3002\u4f7f\u7528Selenium\uff0c\u53ef\u4ee5\u907f\u5f00\u4e00\u4e9b\u7b80\u5355\u7684\u53cd\u722c\u673a\u5236\u3002<\/p>\n<\/p>\n<p><p>1.1\u3001\u5b89\u88c5Selenium\u548c\u6d4f\u89c8\u5668\u9a71\u52a8<\/p>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5Selenium\u5e93\u548c\u5bf9\u5e94\u7684\u6d4f\u89c8\u5668\u9a71\u52a8\u3002\u4ee5Chrome\u6d4f\u89c8\u5668\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u4e0b\u8f7d\u4e0eChrome\u6d4f\u89c8\u5668\u7248\u672c\u5339\u914d\u7684ChromeDriver\uff0c\u5e76\u5c06\u5176\u8def\u5f84\u52a0\u5165\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><p>1.2\u3001\u4f7f\u7528Selenium\u6253\u5f00\u77e5\u7f51\u5e76\u8fdb\u884c\u641c\u7d22<\/p>\n<\/p>\n<p><p>\u901a\u8fc7Selenium\u4ee3\u7801\u53ef\u4ee5\u6253\u5f00\u77e5\u7f51\u5e76\u8f93\u5165\u5173\u952e\u8bcd\u8fdb\u884c\u641c\u7d22\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/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>\u542f\u52a8Chrome\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u77e5\u7f51\u4e3b\u9875<\/strong><\/h2>\n<p>driver.get(&quot;https:\/\/www.cnki.net\/&quot;)<\/p>\n<h2><strong>\u627e\u5230\u641c\u7d22\u6846\u5e76\u8f93\u5165\u5173\u952e\u8bcd<\/strong><\/h2>\n<p>search_box = driver.find_element_by_id(&quot;txt_SearchText&quot;)<\/p>\n<p>search_box.send_keys(&quot;<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>&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(5)<\/p>\n<h2><strong>\u83b7\u53d6\u641c\u7d22\u7ed3\u679c\u9875\u9762\u7684HTML<\/strong><\/h2>\n<p>html = driver.page_source<\/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>\u4e8c\u3001\u5229\u7528requests\u5e93\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42<\/p>\n<\/p>\n<p><p>requests\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u6d01\u5b9e\u7528\u7684HTTP\u8bf7\u6c42\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002\u5728\u722c\u53d6\u77e5\u7f51\u6587\u732e\u6458\u8981\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u9875\u9762\u7684\u7f51\u7edc\u8bf7\u6c42\uff0c\u627e\u5230\u5408\u9002\u7684\u8bf7\u6c42\u5730\u5740\u548c\u53c2\u6570\uff0c\u76f4\u63a5\u4f7f\u7528requests\u5e93\u53d1\u9001\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><p>2.1\u3001\u5b89\u88c5requests\u5e93<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2.2\u3001\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u9875\u9762\u5185\u5bb9<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u77e5\u7f51\u641c\u7d22\u7ed3\u679c\u9875\u9762\u7684URL<\/strong><\/h2>\n<p>url = &quot;https:\/\/kns.cnki.net\/kns8\/defaultresult\/index&quot;<\/p>\n<h2><strong>\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff0c\u6a21\u62df\u6d4f\u89c8\u5668\u8bf7\u6c42<\/strong><\/h2>\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<h2><strong>\u53d1\u9001GET\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(url, headers=headers)<\/p>\n<h2><strong>\u83b7\u53d6HTML\u5185\u5bb9<\/strong><\/h2>\n<p>html = response.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u7ed3\u5408BeautifulSoup\u89e3\u6790HTML\u5185\u5bb9<\/p>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684HTML\u89e3\u6790\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4eceHTML\u6587\u6863\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u5728\u722c\u53d6\u77e5\u7f51\u6587\u732e\u6458\u8981\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408BeautifulSoup\u89e3\u6790HTML\u5185\u5bb9\uff0c\u63d0\u53d6\u6240\u9700\u7684\u6587\u732e\u6458\u8981\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>3.1\u3001\u5b89\u88c5BeautifulSoup\u5e93<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3.2\u3001\u89e3\u6790HTML\u5e76\u63d0\u53d6\u6587\u732e\u6458\u8981<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(html, &quot;html.parser&quot;)<\/p>\n<h2><strong>\u63d0\u53d6\u6587\u732e\u6458\u8981<\/strong><\/h2>\n<p>abstracts = soup.find_all(&quot;span&quot;, class_=&quot;abstract-text&quot;)<\/p>\n<p>for abstract in abstracts:<\/p>\n<p>    print(abstract.get_text())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u7ed3\u5408Selenium\u548cBeautifulSoup\u8fdb\u884c\u5b8c\u6574\u7684\u722c\u53d6\u6d41\u7a0b<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408Selenium\u548cBeautifulSoup\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u7a33\u5b9a\u548c\u9ad8\u6548\u7684\u722c\u53d6\u6d41\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff1a<\/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<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u542f\u52a8Chrome\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u77e5\u7f51\u4e3b\u9875<\/strong><\/h2>\n<p>driver.get(&quot;https:\/\/www.cnki.net\/&quot;)<\/p>\n<h2><strong>\u627e\u5230\u641c\u7d22\u6846\u5e76\u8f93\u5165\u5173\u952e\u8bcd<\/strong><\/h2>\n<p>search_box = driver.find_element_by_id(&quot;txt_SearchText&quot;)<\/p>\n<p>search_box.send_keys(&quot;\u673a\u5668\u5b66\u4e60&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(5)<\/p>\n<h2><strong>\u83b7\u53d6\u641c\u7d22\u7ed3\u679c\u9875\u9762\u7684HTML<\/strong><\/h2>\n<p>html = driver.page_source<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(html, &quot;html.parser&quot;)<\/p>\n<h2><strong>\u63d0\u53d6\u6587\u732e\u6458\u8981<\/strong><\/h2>\n<p>abstracts = soup.find_all(&quot;span&quot;, class_=&quot;abstract-text&quot;)<\/p>\n<p>for abstract in abstracts:<\/p>\n<p>    print(abstract.get_text())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u77e5\u7f51\u6587\u732e\u6458\u8981\u7684\u722c\u53d6\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u722c\u53d6\u7f51\u7ad9\u5185\u5bb9\u65f6\u5e94\u9075\u5b88\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\u548c\u7f51\u7ad9\u7684\u4f7f\u7528\u6761\u6b3e\uff0c\u907f\u514d\u5bf9\u7f51\u7ad9\u9020\u6210\u4e0d\u5fc5\u8981\u7684\u8d1f\u62c5<\/strong>\u3002\u6b64\u5916\uff0c\u77e5\u7f51\u4f5c\u4e3a\u4e00\u4e2a\u91cd\u8981\u7684\u5b66\u672f\u8d44\u6e90\u5e73\u53f0\uff0c\u5176\u5185\u5bb9\u5177\u6709\u8f83\u9ad8\u7684\u7248\u6743\u4fdd\u62a4\u610f\u8bc6\uff0c\u5728\u8fdb\u884c\u6570\u636e\u6293\u53d6\u65f6\u5e94\u8c28\u614e\u64cd\u4f5c\uff0c\u5408\u7406\u4f7f\u7528\u6293\u53d6\u5230\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u4f7f\u7528Python\u8fdb\u884c\u77e5\u7f51\u6587\u732e\u6458\u8981\u7684\u722c\u53d6\uff1f<\/strong><br \/>\u8981\u5f00\u59cb\u4f7f\u7528Python\u722c\u53d6\u77e5\u7f51\u6587\u732e\u6458\u8981\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u5e38\u7528\u7684\u5e93\uff0c\u4f8b\u5982<code>requests<\/code>\u548c<code>BeautifulSoup<\/code>\u3002\u4f7f\u7528<code>requests<\/code>\u5e93\u53ef\u4ee5\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u800c<code>BeautifulSoup<\/code>\u5219\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u5e76\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u5df2\u7ecf\u8bbe\u7f6e\u597d\u3002<\/p>\n<p><strong>\u5728\u722c\u53d6\u77e5\u7f51\u6587\u732e\u6458\u8981\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6cd5\u5f8b\u6cd5\u89c4\uff1f<\/strong><br \/>\u722c\u53d6\u77e5\u7f51\u6587\u732e\u6458\u8981\u65f6\uff0c\u52a1\u5fc5\u9075\u5b88\u76f8\u5173\u7684\u6cd5\u5f8b\u6cd5\u89c4\uff0c\u5305\u62ec\u8457\u4f5c\u6743\u6cd5\u548c\u77e5\u8bc6\u4ea7\u6743\u6cd5\u3002\u77e5\u7f51\u7684\u5185\u5bb9\u901a\u5e38\u53d7\u7248\u6743\u4fdd\u62a4\uff0c\u56e0\u6b64\u5728\u8fdb\u884c\u722c\u53d6\u65f6\u5e94\u5f53\u5c0a\u91cd\u5176\u4f7f\u7528\u6761\u6b3e\uff0c\u786e\u4fdd\u4e0d\u4fb5\u72af\u4ed6\u4eba\u7684\u77e5\u8bc6\u4ea7\u6743\u3002\u6b64\u5916\uff0c\u5408\u7406\u63a7\u5236\u722c\u53d6\u9891\u7387\uff0c\u4ee5\u907f\u514d\u5bf9\u7f51\u7ad9\u9020\u6210\u8d1f\u62c5\uff0c\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u77e5\u7f51\u9875\u9762\u4e2d\u7684\u53cd\u722c\u673a\u5236\uff1f<\/strong><br \/>\u77e5\u7f51\u53ef\u80fd\u4f1a\u8bbe\u7f6e\u4e00\u4e9b\u53cd\u722c\u673a\u5236\u6765\u4fdd\u62a4\u5176\u5185\u5bb9\uff0c\u5e38\u89c1\u7684\u63aa\u65bd\u5305\u62ecIP\u9650\u5236\u548c\u9a8c\u8bc1\u7801\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u4e9b\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4ee3\u7406IP\u6765\u5206\u6563\u8bf7\u6c42\u6765\u6e90\uff0c\u6216\u8005\u5b9e\u73b0\u6a21\u62df\u4eba\u5de5\u64cd\u4f5c\u7684\u673a\u5236\uff0c\u4f8b\u5982\u4f7f\u7528Selenium\u5e93\u6765\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u7f51\u9875\u3002\u6b64\u5916\uff0c\u52a0\u5165\u9002\u5f53\u7684\u8bf7\u6c42\u5934\u4fe1\u606f\uff0c\u5982User-Agent\uff0c\u4e5f\u6709\u52a9\u4e8e\u964d\u4f4e\u88ab\u8bc6\u522b\u4e3a\u722c\u866b\u7684\u98ce\u9669\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u4f7f\u7528Python\u722c\u53d6\u77e5\u7f51\u6587\u732e\u6458\u8981 \u4f7f\u7528Python\u722c\u53d6\u77e5\u7f51\u6587\u732e\u6458\u8981\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Selenium\u6a21\u62df\u6d4f\u89c8 [&hellip;]","protected":false},"author":3,"featured_media":1069174,"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\/1069164"}],"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=1069164"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069164\/revisions"}],"predecessor-version":[{"id":1069178,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069164\/revisions\/1069178"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069174"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}