{"id":1125364,"date":"2025-01-08T19:51:16","date_gmt":"2025-01-08T11:51:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1125364.html"},"modified":"2025-01-08T19:51:16","modified_gmt":"2025-01-08T11:51:16","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e9%a1%b5%e9%9d%a2%e4%b8%8a%e8%be%93%e5%85%a5%e6%a1%86%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1125364.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u9875\u9762\u4e0a\u8f93\u5165\u6846\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090104\/d2c96ccb-5394-4fb4-af52-041d29ee97a0.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u9875\u9762\u4e0a\u8f93\u5165\u6846\u7684\u503c\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u83b7\u53d6\u9875\u9762\u4e0a\u7684\u8f93\u5165\u6846\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u3001Beautiful Soup\u3001\u6216Requests-HTML\u3002<\/strong> \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e09\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u6765\u5c55\u793a\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<strong>Selenium\u5e93\u3001Beautiful Soup\u3001Requests-HTML<\/strong>\u662f\u4e09\u79cd\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u5176\u4e2dSelenium\u5e93\u7279\u522b\u9002\u5408\u5904\u7406\u52a8\u6001\u7f51\u9875\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\u4e2d\u7684\u6bcf\u4e00\u79cd\uff0c\u5e76\u63a2\u8ba8\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Selenium\u5e93<\/h3>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u7528\u4e8eWeb\u5e94\u7528\u7a0b\u5e8f\u6d4b\u8bd5\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u81ea\u52a8\u5316\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u8fd9\u4f7f\u5f97\u5b83\u975e\u5e38\u9002\u5408\u7528\u4e8e\u4ece\u52a8\u6001\u7f51\u9875\u4e2d\u63d0\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Selenium\u548c\u6d4f\u89c8\u5668\u9a71\u52a8<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Selenium\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Selenium\u5e93\u548c\u4e00\u4e2a\u6d4f\u89c8\u5668\u9a71\u52a8\uff08\u4f8b\u5982ChromeDriver\uff09\u3002<\/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\u5e76\u786e\u4fdd\u5176\u8def\u5f84\u6b63\u786e\u914d\u7f6e\u3002\u53ef\u4ee5\u5728 <a href=\"https:\/\/sites.google.com\/a\/chromium.org\/chromedriver\/downloads\">ChromeDriver\u4e0b\u8f7d\u9875\u9762<\/a> \u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528Selenium\u83b7\u53d6\u8f93\u5165\u6846\u7684\u503c<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Selenium\u83b7\u53d6\u9875\u9762\u4e0a\u8f93\u5165\u6846\u503c\u7684\u793a\u4f8b\u4ee3\u7801\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<h2><strong>\u521d\u59cb\u5316\u6d4f\u89c8\u5668\u9a71\u52a8<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;https:\/\/example.com&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u8f93\u5165\u6846\u5143\u7d20<\/strong><\/h2>\n<p>input_element = driver.find_element(By.ID, &#39;input_id&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u8f93\u5165\u6846\u7684\u503c<\/strong><\/h2>\n<p>input_value = input_element.get_attribute(&#39;value&#39;)<\/p>\n<p>print(f&quot;\u8f93\u5165\u6846\u7684\u503c\u662f: {input_value}&quot;)<\/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><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521d\u59cb\u5316\u4e86\u4e00\u4e2aChrome\u6d4f\u89c8\u5668\u9a71\u52a8\uff0c\u5e76\u6253\u5f00\u4e86\u6307\u5b9a\u7684\u7f51\u9875\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u5143\u7d20ID\u67e5\u627e\u8f93\u5165\u6846\u5143\u7d20\uff0c\u5e76\u4f7f\u7528<code>get_attribute(&#39;value&#39;)<\/code>\u65b9\u6cd5\u83b7\u53d6\u8f93\u5165\u6846\u7684\u503c\u3002\u6700\u540e\uff0c\u6253\u5370\u51fa\u83b7\u53d6\u7684\u503c\u5e76\u5173\u95ed\u6d4f\u89c8\u5668\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Beautiful Soup<\/h3>\n<\/p>\n<p><p>Beautiful Soup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684\u5e93\uff0c\u9002\u5408\u4ece\u9759\u6001\u7f51\u9875\u4e2d\u63d0\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Beautiful Soup\u548cRequests\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528Beautiful Soup\u83b7\u53d6\u8f93\u5165\u6846\u7684\u503c<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Beautiful Soup\u83b7\u53d6\u9875\u9762\u4e0a\u8f93\u5165\u6846\u503c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>html_content = response.text<\/p>\n<h2><strong>\u89e3\u6790HTML<\/strong><\/h2>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u8f93\u5165\u6846\u5143\u7d20<\/strong><\/h2>\n<p>input_element = soup.find(&#39;input&#39;, {&#39;id&#39;: &#39;input_id&#39;})<\/p>\n<h2><strong>\u83b7\u53d6\u8f93\u5165\u6846\u7684\u503c<\/strong><\/h2>\n<p>input_value = input_element[&#39;value&#39;]<\/p>\n<p>print(f&quot;\u8f93\u5165\u6846\u7684\u503c\u662f: {input_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528Requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528Beautiful Soup\u89e3\u6790HTML\u6587\u6863\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u5143\u7d20ID\u67e5\u627e\u8f93\u5165\u6846\u5143\u7d20\uff0c\u5e76\u76f4\u63a5\u83b7\u53d6\u5176\u503c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Requests-HTML<\/h3>\n<\/p>\n<p><p>Requests-HTML\u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u5e93\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8e\u4ece\u52a8\u6001\u7f51\u9875\u4e2d\u63d0\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Requests-HTML\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests-html<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528Requests-HTML\u83b7\u53d6\u8f93\u5165\u6846\u7684\u503c<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Requests-HTML\u83b7\u53d6\u9875\u9762\u4e0a\u8f93\u5165\u6846\u503c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from requests_html import HTMLSession<\/p>\n<h2><strong>\u521d\u59cb\u5316HTML\u4f1a\u8bdd<\/strong><\/h2>\n<p>session = HTMLSession()<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com&#39;<\/p>\n<p>response = session.get(url)<\/p>\n<h2><strong>\u6e32\u67d3JavaScript<\/strong><\/h2>\n<p>response.html.render()<\/p>\n<h2><strong>\u67e5\u627e\u8f93\u5165\u6846\u5143\u7d20<\/strong><\/h2>\n<p>input_element = response.html.find(&#39;#input_id&#39;, first=True)<\/p>\n<h2><strong>\u83b7\u53d6\u8f93\u5165\u6846\u7684\u503c<\/strong><\/h2>\n<p>input_value = input_element.attrs[&#39;value&#39;]<\/p>\n<p>print(f&quot;\u8f93\u5165\u6846\u7684\u503c\u662f: {input_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521d\u59cb\u5316\u4e86\u4e00\u4e2aHTML\u4f1a\u8bdd\uff0c\u5e76\u83b7\u53d6\u4e86\u6307\u5b9a\u7f51\u9875\u7684\u5185\u5bb9\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>render()<\/code>\u65b9\u6cd5\u6e32\u67d3JavaScript\uff0c\u4ee5\u786e\u4fdd\u52a8\u6001\u5185\u5bb9\u88ab\u52a0\u8f7d\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u5143\u7d20ID\u67e5\u627e\u8f93\u5165\u6846\u5143\u7d20\uff0c\u5e76\u83b7\u53d6\u5176\u503c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6bd4\u8f83\u4e0e\u603b\u7ed3<\/h3>\n<\/p>\n<p><h4>1. Selenium\u5e93<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u652f\u6301\u52a8\u6001\u7f51\u9875<\/li>\n<li>\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u64cd\u4f5c<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5b89\u88c5\u6d4f\u89c8\u5668\u9a71\u52a8<\/li>\n<li>\u8d44\u6e90\u6d88\u8017\u8f83\u5927<\/li>\n<\/ul>\n<p><h4>2. Beautiful Soup<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u7528<\/li>\n<li>\u9002\u5408\u89e3\u6790\u9759\u6001\u7f51\u9875<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4e0d\u652f\u6301\u52a8\u6001\u7f51\u9875<\/li>\n<\/ul>\n<p><h4>3. Requests-HTML<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u652f\u6301\u52a8\u6001\u7f51\u9875<\/li>\n<li>\u4e0d\u9700\u8981\u6d4f\u89c8\u5668\u9a71\u52a8<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u529f\u80fd\u76f8\u5bf9\u7b80\u5355<\/li>\n<\/ul>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u5de5\u5177\u975e\u5e38\u91cd\u8981\u3002\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u52a8\u6001\u7f51\u9875\uff0c<strong>Selenium\u5e93\u548cRequests-HTML<\/strong>\u90fd\u662f\u4e0d\u9519\u7684\u9009\u62e9\u3002\u5982\u679c\u4f60\u53ea\u9700\u8981\u5904\u7406\u9759\u6001\u7f51\u9875\uff0c<strong>Beautiful Soup<\/strong>\u53ef\u80fd\u662f\u66f4\u7b80\u5355\u7684\u9009\u62e9\u3002\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u5e0c\u671b\u4f60\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u9875\u9762\u4e0a\u8f93\u5165\u6846\u7684\u503c\uff0c\u5e76\u9009\u62e9\u6700\u9002\u5408\u4f60\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u7f51\u9875\u4e2d\u8f93\u5165\u6846\u7684\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982Selenium\u6216BeautifulSoup\u6765\u83b7\u53d6\u7f51\u9875\u4e0a\u7684\u8f93\u5165\u6846\u503c\u3002Selenium\u80fd\u591f\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u4ece\u800c\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\uff0c\u800cBeautifulSoup\u5219\u9002\u7528\u4e8e\u89e3\u6790\u9759\u6001HTML\u9875\u9762\u3002\u4f7f\u7528Selenium\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4f4d\u5143\u7d20\uff0c\u4f8b\u5982\u4f7f\u7528XPath\u6216CSS\u9009\u62e9\u5668\uff0c\u83b7\u53d6\u8f93\u5165\u6846\u7684\u503c\u3002<\/p>\n<p><strong>\u4f7f\u7528Selenium\u65f6\uff0c\u5982\u4f55\u5b9a\u4f4d\u8f93\u5165\u6846\u5143\u7d20\uff1f<\/strong><br \/>\u5728Selenium\u4e2d\uff0c\u5b9a\u4f4d\u8f93\u5165\u6846\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ecID\u3001\u540d\u79f0\u3001\u7c7b\u540d\u3001XPath\u6216CSS\u9009\u62e9\u5668\u3002\u9009\u62e9\u5408\u9002\u7684\u5b9a\u4f4d\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7a33\u5b9a\u6027\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>driver.find_element_by_id(&#39;input_id&#39;)<\/code>\u53ef\u4ee5\u6839\u636eID\u5feb\u901f\u83b7\u53d6\u8f93\u5165\u6846\u7684\u5f15\u7528\u3002<\/p>\n<p><strong>\u83b7\u53d6\u8f93\u5165\u6846\u503c\u540e\uff0c\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u6570\u636e\uff1f<\/strong><br \/>\u4e00\u65e6\u6210\u529f\u83b7\u53d6\u8f93\u5165\u6846\u7684\u503c\uff0c\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u591a\u79cd\u5904\u7406\uff0c\u6bd4\u5982\u6570\u636e\u6e05\u6d17\u3001\u5b58\u50a8\u5230\u6570\u636e\u5e93\u6216\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5206\u6790\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u548c\u6570\u636e\u5904\u7406\u5e93\u5982Pandas\u6765\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\uff0c\u4ece\u800c\u5c06\u8f93\u5165\u6846\u4e2d\u7684\u6570\u636e\u8f6c\u5316\u4e3a\u6709\u7528\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u83b7\u53d6\u9875\u9762\u4e0a\u7684\u8f93\u5165\u6846\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u3001Beautiful Soup\u3001\u6216Reque [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125364"}],"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=1125364"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125364\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1125364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1125364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1125364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}