{"id":1097392,"date":"2025-01-08T15:11:26","date_gmt":"2025-01-08T07:11:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1097392.html"},"modified":"2025-01-08T15:11:28","modified_gmt":"2025-01-08T07:11:28","slug":"python%e5%a6%82%e4%bd%95%e5%be%aa%e7%8e%af%e6%9f%a5%e6%89%be%e6%9f%90%e4%b8%aa%e6%96%87%e6%9c%ac%e6%a1%86-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1097392.html","title":{"rendered":"python\u5982\u4f55\u5faa\u73af\u67e5\u627e\u67d0\u4e2a\u6587\u672c\u6846"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212205\/085af5b6-d80f-48d4-b295-776414e345ed.webp\" alt=\"python\u5982\u4f55\u5faa\u73af\u67e5\u627e\u67d0\u4e2a\u6587\u672c\u6846\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u5faa\u73af\u67e5\u627e\u67d0\u4e2a\u6587\u672c\u6846<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<strong>Selenium\u5e93\u3001BeautifulSoup\u5e93\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u7b49\u65b9\u6cd5\u5206\u522b\u5904\u7406\u7f51\u9875\u81ea\u52a8\u5316\u3001HTML\u89e3\u6790\u3001\u5b57\u7b26\u4e32\u5339\u914d\u7b49\u573a\u666f\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5728Python\u4e2d\u5faa\u73af\u67e5\u627e\u67d0\u4e2a\u6587\u672c\u6846\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u8fdb\u884c\u8bb2\u89e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Selenium\u5e93<\/h3>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316\u7f51\u9875\u6d4b\u8bd5\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u4e0e\u6d4f\u89c8\u5668\u8fdb\u884c\u4ea4\u4e92\uff0c\u6bd4\u5982\u70b9\u51fb\u6309\u94ae\u3001\u8f93\u5165\u6587\u672c\u7b49\u3002\u901a\u8fc7Selenium\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u52a8\u5316\u5730\u5faa\u73af\u67e5\u627e\u67d0\u4e2a\u6587\u672c\u6846\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Selenium\u5e93\u548c\u6d4f\u89c8\u5668\u9a71\u52a8<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Selenium\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\uff0c\u5e76\u4e0b\u8f7d\u5bf9\u5e94\u6d4f\u89c8\u5668\u7684\u9a71\u52a8\u3002\u4ee5Chrome\u6d4f\u89c8\u5668\u4e3a\u4f8b\uff0c\u9996\u5148\u5b89\u88c5Selenium\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u4e0b\u8f7dChromeDriver\uff0c\u5e76\u5c06\u5176\u8def\u5f84\u6dfb\u52a0\u5230\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Selenium\u5e93\u5728\u7f51\u9875\u4e2d\u5faa\u73af\u67e5\u627e\u67d0\u4e2a\u6587\u672c\u6846\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>\u521b\u5efaChrome\u6d4f\u89c8\u5668\u5b9e\u4f8b<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u76ee\u6807\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;https:\/\/example.com&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u6587\u672c\u6846\u5143\u7d20<\/strong><\/h2>\n<p>text_boxes = driver.find_elements(By.TAG_NAME, &#39;input&#39;)<\/p>\n<h2><strong>\u5faa\u73af\u67e5\u627e\u7279\u5b9a\u6587\u672c\u6846<\/strong><\/h2>\n<p>for text_box in text_boxes:<\/p>\n<p>    if text_box.get_attribute(&#39;type&#39;) == &#39;text&#39;:<\/p>\n<p>        print(f&#39;\u627e\u5230\u6587\u672c\u6846: {text_box.get_attribute(&quot;name&quot;)}&#39;)<\/p>\n<p>        # \u5728\u6587\u672c\u6846\u4e2d\u8f93\u5165\u5185\u5bb9<\/p>\n<p>        text_box.send_keys(&#39;Hello World&#39;)<\/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\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2aChrome\u6d4f\u89c8\u5668\u5b9e\u4f8b\uff0c\u5e76\u6253\u5f00\u76ee\u6807\u7f51\u9875\u3002\u7136\u540e\u4f7f\u7528<code>find_elements<\/code>\u65b9\u6cd5\u83b7\u53d6\u9875\u9762\u4e2d\u7684\u6240\u6709<code>input<\/code>\u5143\u7d20\uff0c\u5e76\u901a\u8fc7\u5faa\u73af\u67e5\u627e\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u672c\u6846\uff08type\u5c5e\u6027\u4e3atext\uff09\uff0c\u6700\u540e\u5728\u627e\u5230\u7684\u6587\u672c\u6846\u4e2d\u8f93\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001BeautifulSoup\u5e93<\/h3>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684\u5e93\uff0c\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u6570\u636e\u3002\u901a\u8fc7BeautifulSoup\uff0c\u6211\u4eec\u53ef\u4ee5\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u5e76\u5faa\u73af\u67e5\u627e\u67d0\u4e2a\u6587\u672c\u6846\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5BeautifulSoup\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5b89\u88c5BeautifulSoup\u5e93\u53ca\u5176\u4f9d\u8d56\u7684lxml\u89e3\u6790\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528BeautifulSoup\u5e93\u89e3\u6790\u7f51\u9875\u5e76\u5faa\u73af\u67e5\u627e\u67d0\u4e2a\u6587\u672c\u6846\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>import requests<\/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.content<\/p>\n<h2><strong>\u89e3\u6790\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(html_content, &#39;lxml&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u6587\u672c\u6846\u5143\u7d20<\/strong><\/h2>\n<p>text_boxes = soup.find_all(&#39;input&#39;, {&#39;type&#39;: &#39;text&#39;})<\/p>\n<h2><strong>\u5faa\u73af\u67e5\u627e\u7279\u5b9a\u6587\u672c\u6846<\/strong><\/h2>\n<p>for text_box in text_boxes:<\/p>\n<p>    print(f&#39;\u627e\u5230\u6587\u672c\u6846: {text_box.get(&quot;name&quot;)}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u6587\u6863\u3002\u7136\u540e\u4f7f\u7528<code>find_all<\/code>\u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u7c7b\u578b\u4e3atext\u7684<code>input<\/code>\u5143\u7d20\uff0c\u5e76\u901a\u8fc7\u5faa\u73af\u6253\u5370\u627e\u5230\u7684\u6587\u672c\u6846\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u5de5\u5177\uff0c\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6587\u672c\u4e2d\u5faa\u73af\u67e5\u627e\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u672c\u6846\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u6b63\u5219\u8868\u8fbe\u5f0f\u5e93<\/h4>\n<\/p>\n<p><p>Python\u5185\u7f6e\u4e86re\u6a21\u5757\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u7f51\u9875\u5185\u5bb9\u4e2d\u5faa\u73af\u67e5\u627e\u67d0\u4e2a\u6587\u672c\u6846\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>import requests<\/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>\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/strong><\/h2>\n<p>pattern = re.compile(r&#39;&lt;input[^&gt;]*type=&quot;text&quot;[^&gt;]*&gt;&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u6240\u6709\u7b26\u5408\u6a21\u5f0f\u7684\u6587\u672c\u6846<\/strong><\/h2>\n<p>text_boxes = pattern.findall(html_content)<\/p>\n<h2><strong>\u5faa\u73af\u67e5\u627e\u7279\u5b9a\u6587\u672c\u6846<\/strong><\/h2>\n<p>for text_box in text_boxes:<\/p>\n<p>    print(f&#39;\u627e\u5230\u6587\u672c\u6846: {text_box}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u7528\u4e8e\u5339\u914d\u6240\u6709\u7c7b\u578b\u4e3atext\u7684<code>input<\/code>\u5143\u7d20\u3002\u7136\u540e\u4f7f\u7528<code>findall<\/code>\u65b9\u6cd5\u67e5\u627e\u6240\u6709\u7b26\u5408\u6a21\u5f0f\u7684\u6587\u672c\u6846\uff0c\u5e76\u901a\u8fc7\u5faa\u73af\u6253\u5370\u627e\u5230\u7684\u6587\u672c\u6846\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u3001BeautifulSoup\u5e93\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u65b9\u6cd5\u5728Python\u4e2d\u5faa\u73af\u67e5\u627e\u67d0\u4e2a\u6587\u672c\u6846\u3002<strong>Selenium\u9002\u7528\u4e8e\u7f51\u9875\u81ea\u52a8\u5316\u64cd\u4f5c\u3001BeautifulSoup\u9002\u7528\u4e8eHTML\u6587\u6863\u89e3\u6790\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u5339\u914d<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5b9e\u73b0\u5faa\u73af\u67e5\u627e\u6587\u672c\u6846\u7684\u76ee\u6807\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u7f51\u9875\u6570\u636e\u5904\u7406\u548c\u81ea\u52a8\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5faa\u73af\u67e5\u627e\u6587\u672c\u6846\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\uff08\u5982for\u6216while\uff09\u7ed3\u5408\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\uff08\u5982Tkinter\u6216PyQt\uff09\u6765\u67e5\u627e\u5e76\u64cd\u4f5c\u6587\u672c\u6846\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u521b\u5efa\u6587\u672c\u6846\u7684\u5b9e\u4f8b\uff0c\u7136\u540e\u4f7f\u7528\u5faa\u73af\u6765\u904d\u5386\u6587\u672c\u6846\u7684\u5c5e\u6027\u6216\u5185\u5bb9\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u6587\u672c\u6846\u67e5\u627e\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u7528\u7684GUI\u5e93\uff1f<\/strong><br \/>Python\u4e2d\u5e38\u7528\u7684GUI\u5e93\u5305\u62ecTkinter\u3001PyQt\u3001wxPython\u548cKivy\u7b49\u3002Tkinter\u662fPython\u81ea\u5e26\u7684\u5e93\uff0c\u4f7f\u7528\u7b80\u5355\uff0c\u9002\u5408\u521d\u5b66\u8005\uff1b\u800cPyQt\u529f\u80fd\u66f4\u5f3a\u5927\uff0c\u9002\u5408\u5f00\u53d1\u590d\u6742\u7684\u5e94\u7528\u3002\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u5e93\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5b9e\u73b0\u6587\u672c\u6846\u7684\u67e5\u627e\u4e0e\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728\u67e5\u627e\u6587\u672c\u6846\u65f6\uff0c\u5982\u4f55\u5904\u7406\u67e5\u627e\u5931\u8d25\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6587\u672c\u6846\u67e5\u627e\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u6807\u5fd7\u53d8\u91cf\u6765\u8bb0\u5f55\u662f\u5426\u627e\u5230\u76ee\u6807\u6587\u672c\u6846\u3002\u5982\u679c\u5728\u5faa\u73af\u7ed3\u675f\u540e\u4ecd\u672a\u627e\u5230\uff0c\u53ef\u4ee5\u901a\u8fc7\u5f39\u51fa\u63d0\u793a\u6846\u6216\u6253\u5370\u9519\u8bef\u4fe1\u606f\u7684\u65b9\u5f0f\u544a\u77e5\u7528\u6237\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\uff0c\u5e76\u907f\u514d\u7a0b\u5e8f\u7684\u5f02\u5e38\u7ec8\u6b62\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u5faa\u73af\u67e5\u627e\u67d0\u4e2a\u6587\u672c\u6846\uff1a\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u3001BeautifulSoup\u5e93\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1097399,"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\/1097392"}],"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=1097392"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097392\/revisions"}],"predecessor-version":[{"id":1097401,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097392\/revisions\/1097401"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1097399"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1097392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1097392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1097392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}