{"id":1091942,"date":"2025-01-08T14:16:29","date_gmt":"2025-01-08T06:16:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1091942.html"},"modified":"2025-01-08T14:16:32","modified_gmt":"2025-01-08T06:16:32","slug":"python%e5%a6%82%e4%bd%95%e6%8a%93%e5%8f%96%e8%81%8a%e5%a4%a9%e7%aa%97%e5%8f%a3%e5%86%85%e5%ae%b9-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1091942.html","title":{"rendered":"python\u5982\u4f55\u6293\u53d6\u804a\u5929\u7a97\u53e3\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205218\/4c26ef84-fb30-4725-a180-f6f55d659e81.webp\" alt=\"python\u5982\u4f55\u6293\u53d6\u804a\u5929\u7a97\u53e3\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u6293\u53d6\u804a\u5929\u7a97\u53e3\u5185\u5bb9\u7684\u65b9\u5f0f\u5305\u62ec\uff1a\u5229\u7528\u81ea\u52a8\u5316\u5de5\u5177\u3001\u5229\u7528API\u63a5\u53e3\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3002<\/strong> \u5176\u4e2d\uff0c\u5229\u7528\u81ea\u52a8\u5316\u5de5\u5177\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u901a\u5e38\u4f7f\u7528Selenium\u7b49\u5de5\u5177\u8fdb\u884c\u64cd\u4f5c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Selenium\u6765\u6293\u53d6\u804a\u5929\u7a97\u53e3\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5229\u7528Selenium\u6293\u53d6\u804a\u5929\u7a97\u53e3\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>Selenium \u662f\u4e00\u4e2a\u7528\u4e8eWeb\u5e94\u7528\u7a0b\u5e8f\u6d4b\u8bd5\u7684\u5de5\u5177\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2aAPI\uff0c\u7528\u4e8e\u63a7\u5236\u6d4f\u89c8\u5668\u7684\u884c\u4e3a\uff0c\u4ece\u800c\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u5316\u6d4b\u8bd5\u548c\u6293\u53d6\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u548c\u8bbe\u7f6eSelenium<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Selenium\u5e93\u548c\u6d4f\u89c8\u5668\u9a71\u52a8\u3002\u4ee5Chrome\u6d4f\u89c8\u5668\u4e3a\u4f8b\uff0c\u4f60\u9700\u8981\u5b89\u88c5ChromeDriver\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\u8f7d<a href=\"https:\/\/sites.google.com\/a\/chromium.org\/chromedriver\/\">ChromeDriver<\/a>\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684PATH\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u542f\u52a8\u6d4f\u89c8\u5668\u5e76\u8bbf\u95ee\u804a\u5929\u9875\u9762<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u7f16\u5199Python\u811a\u672c\u6765\u542f\u52a8\u6d4f\u89c8\u5668\u5e76\u8bbf\u95ee\u804a\u5929\u9875\u9762\u3002<\/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<p>from selenium.webdriver.chrome.service import Service<\/p>\n<p>from webdriver_manager.chrome import ChromeDriverManager<\/p>\n<h2><strong>\u542f\u52a8Chrome\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>service = Service(ChromeDriverManager().install())<\/p>\n<p>driver = webdriver.Chrome(service=service)<\/p>\n<h2><strong>\u8bbf\u95ee\u804a\u5929\u9875\u9762<\/strong><\/h2>\n<p>driver.get(&quot;https:\/\/your-chat-application-url.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5b9a\u4f4d\u804a\u5929\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Selenium\u7684\u5b9a\u4f4d\u529f\u80fd\u627e\u5230\u804a\u5929\u7a97\u53e3\u7684\u5143\u7d20\u3002\u901a\u5e38\u4f7f\u7528XPath\u3001CSS\u9009\u62e9\u5668\u7b49\u6765\u5b9a\u4f4d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b49\u5f85\u9875\u9762\u52a0\u8f7d<\/p>\n<p>driver.implicitly_w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t(10)<\/p>\n<h2><strong>\u5b9a\u4f4d\u804a\u5929\u7a97\u53e3<\/strong><\/h2>\n<p>chat_window = driver.find_element(By.XPATH, &#39;\/\/*[@id=&quot;chat-window&quot;]&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u6293\u53d6\u804a\u5929\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u83b7\u53d6\u804a\u5929\u7a97\u53e3\u4e2d\u7684\u5185\u5bb9\uff0c\u901a\u5e38\u804a\u5929\u5185\u5bb9\u662f\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u5b58\u5728\u7684\uff0c\u53ef\u4ee5\u9010\u6761\u83b7\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u804a\u5929\u8bb0\u5f55<\/p>\n<p>messages = chat_window.find_elements(By.CSS_SELECTOR, &quot;.message&quot;)<\/p>\n<h2><strong>\u6253\u5370\u804a\u5929\u8bb0\u5f55<\/strong><\/h2>\n<p>for message in messages:<\/p>\n<p>    print(message.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u804a\u5929\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u6709\u4e9b\u804a\u5929\u5e94\u7528\u7a0b\u5e8f\u4f1a\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9\uff0c\u8fd9\u65f6\u9700\u8981\u4f7f\u7528Selenium\u7684\u7b49\u5f85\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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<h2><strong>\u7b49\u5f85\u804a\u5929\u7a97\u53e3\u52a0\u8f7d\u5b8c\u6210<\/strong><\/h2>\n<p>chat_window = WebDriverWait(driver, 10).until(<\/p>\n<p>    EC.presence_of_element_located((By.XPATH, &#39;\/\/*[@id=&quot;chat-window&quot;]&#39;))<\/p>\n<p>)<\/p>\n<h2><strong>\u6293\u53d6\u5185\u5bb9<\/strong><\/h2>\n<p>messages = chat_window.find_elements(By.CSS_SELECTOR, &quot;.message&quot;)<\/p>\n<p>for message in messages:<\/p>\n<p>    print(message.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528API\u63a5\u53e3\u6293\u53d6\u804a\u5929\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u8bb8\u591a\u804a\u5929\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u4e86API\u63a5\u53e3\uff0c\u53ef\u4ee5\u901a\u8fc7API\u6765\u83b7\u53d6\u804a\u5929\u5185\u5bb9\uff0c\u4f8b\u5982Slack\u3001Discord\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6ce8\u518c\u548c\u83b7\u53d6API\u5bc6\u94a5<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5728\u804a\u5929\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u8005\u5e73\u53f0\u6ce8\u518c\u5e76\u83b7\u53d6API\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8c03\u7528API\u63a5\u53e3<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684requests\u5e93\u6765\u8c03\u7528API\u63a5\u53e3\u5e76\u83b7\u53d6\u804a\u5929\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u66ff\u6362\u4e3a\u5b9e\u9645\u7684API URL\u548c\u5bc6\u94a5<\/strong><\/h2>\n<p>api_url = &quot;https:\/\/api.chat-application.com\/v1\/messages&quot;<\/p>\n<p>api_key = &quot;your-api-key&quot;<\/p>\n<p>headers = {<\/p>\n<p>    &quot;Authorization&quot;: f&quot;Bearer {api_key}&quot;<\/p>\n<p>}<\/p>\n<p>response = requests.get(api_url, headers=headers)<\/p>\n<h2><strong>\u5904\u7406\u54cd\u5e94<\/strong><\/h2>\n<p>if response.status_code == 200:<\/p>\n<p>    messages = response.json()<\/p>\n<p>    for message in messages:<\/p>\n<p>        print(message[&quot;text&quot;])<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to fetch messages:&quot;, response.status_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6ca1\u6709API\u63a5\u53e3\u7684\u804a\u5929\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u901a\u8fc7\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u6765\u83b7\u53d6\u804a\u5929\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML<\/h4>\n<\/p>\n<p><p>BeautifulSoup \u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u7684Python\u5e93\u3002\u9996\u5148\uff0c\u5b89\u88c5BeautifulSoup\u548crequests\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u5e76\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528requests\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528BeautifulSoup\u8fdb\u884c\u89e3\u6790\u3002<\/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 = &quot;https:\/\/your-chat-application-url.com&quot;<\/p>\n<p>response = requests.get(url)<\/p>\n<h2><strong>\u89e3\u6790HTML<\/strong><\/h2>\n<p>soup = BeautifulSoup(response.content, &quot;html.parser&quot;)<\/p>\n<h2><strong>\u5b9a\u4f4d\u804a\u5929\u7a97\u53e3\u5e76\u83b7\u53d6\u5185\u5bb9<\/strong><\/h2>\n<p>chat_window = soup.find(id=&quot;chat-window&quot;)<\/p>\n<p>messages = chat_window.find_all(class_=&quot;message&quot;)<\/p>\n<h2><strong>\u6253\u5370\u804a\u5929\u8bb0\u5f55<\/strong><\/h2>\n<p>for message in messages:<\/p>\n<p>    print(message.get_text())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u7ed3\u5408BeautifulSoup\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u542f\u52a8\u6d4f\u89c8\u5668\u5e76\u8bbf\u95ee\u9875\u9762<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<p>driver.get(&quot;https:\/\/your-chat-application-url.com&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>html = driver.page_source<\/p>\n<h2><strong>\u89e3\u6790HTML<\/strong><\/h2>\n<p>soup = BeautifulSoup(html, &quot;html.parser&quot;)<\/p>\n<p>chat_window = soup.find(id=&quot;chat-window&quot;)<\/p>\n<p>messages = chat_window.find_all(class_=&quot;message&quot;)<\/p>\n<h2><strong>\u6253\u5370\u804a\u5929\u8bb0\u5f55<\/strong><\/h2>\n<p>for message in messages:<\/p>\n<p>    print(message.get_text())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u6293\u53d6\u804a\u5929\u7a97\u53e3\u7684\u5185\u5bb9\u3002<strong>\u5229\u7528Selenium\u8fdb\u884c\u81ea\u52a8\u5316\u64cd\u4f5c<\/strong>\uff0c<strong>\u5229\u7528API\u63a5\u53e3\u8fdb\u884c\u6570\u636e\u83b7\u53d6<\/strong>\uff0c<strong>\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u8fdb\u884c\u6293\u53d6<\/strong>\uff0c\u5404\u6709\u4f18\u7f3a\u70b9\u3002\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u65b9\u6cd5\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u8c03\u6574\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u83b7\u53d6\u5230\u6240\u9700\u7684\u804a\u5929\u5185\u5bb9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6293\u53d6\u804a\u5929\u7a97\u53e3\u5185\u5bb9\uff1f<\/strong><br \/>\u8981\u6293\u53d6\u804a\u5929\u7a97\u53e3\u7684\u5185\u5bb9\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u5e93\uff0c\u5982<code>selenium<\/code>\u6216<code>pyautogui<\/code>\u3002<code>selenium<\/code>\u9002\u5408\u5904\u7406\u7f51\u9875\u804a\u5929\u5e94\u7528\u7a0b\u5e8f\uff0c\u800c<code>pyautogui<\/code>\u5219\u53ef\u4ee5\u7528\u4e8e\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002\u60a8\u9700\u8981\u5148\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\uff0c\u5e76\u7f16\u5199\u4ee3\u7801\u4ee5\u81ea\u52a8\u5316\u6d4f\u89c8\u5668\u6216\u64cd\u4f5c\u7cfb\u7edf\u754c\u9762\u3002<\/p>\n<p><strong>\u6293\u53d6\u804a\u5929\u5185\u5bb9\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6cd5\u5f8b\u548c\u9053\u5fb7\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u6293\u53d6\u804a\u5929\u5185\u5bb9\u4e4b\u524d\uff0c\u52a1\u5fc5\u4e86\u89e3\u76f8\u5173\u7684\u6cd5\u5f8b\u6cd5\u89c4\u3002\u5728\u8bb8\u591a\u56fd\u5bb6\uff0c\u672a\u7ecf\u4ed6\u4eba\u540c\u610f\u6293\u53d6\u548c\u4f7f\u7528\u5176\u804a\u5929\u8bb0\u5f55\u662f\u975e\u6cd5\u7684\u3002\u6b64\u5916\uff0c\u5c0a\u91cd\u4ed6\u4eba\u7684\u9690\u79c1\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u786e\u4fdd\u60a8\u5728\u6293\u53d6\u6570\u636e\u65f6\uff0c\u9075\u5faa\u9002\u5f53\u7684\u9053\u5fb7\u6807\u51c6\uff0c\u5e76\u83b7\u5f97\u5fc5\u8981\u7684\u8bb8\u53ef\u3002<\/p>\n<p><strong>\u6293\u53d6\u804a\u5929\u5185\u5bb9\u540e\uff0c\u5982\u4f55\u5904\u7406\u548c\u5206\u6790\u8fd9\u4e9b\u6570\u636e\uff1f<\/strong><br \/>\u6293\u53d6\u5230\u7684\u804a\u5929\u5185\u5bb9\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\u548c\u5206\u6790\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>pandas<\/code>\u5e93\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u6574\u7406\uff0c\u6216\u8005\u4f7f\u7528<code>nltk<\/code>\u548c<code>spaCy<\/code>\u7b49\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u8fdb\u884c\u6587\u672c\u5206\u6790\u3002\u6b64\u5916\uff0c\u6570\u636e\u53ef\u89c6\u5316\u5e93\u5982<code>matplotlib<\/code>\u548c<code>seaborn<\/code>\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u76f4\u89c2\u5730\u5c55\u793a\u5206\u6790\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u6293\u53d6\u804a\u5929\u7a97\u53e3\u5185\u5bb9\u7684\u65b9\u5f0f\u5305\u62ec\uff1a\u5229\u7528\u81ea\u52a8\u5316\u5de5\u5177\u3001\u5229\u7528API\u63a5\u53e3\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3002 \u5176\u4e2d\uff0c\u5229\u7528\u81ea\u52a8\u5316\u5de5 [&hellip;]","protected":false},"author":3,"featured_media":1091955,"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\/1091942"}],"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=1091942"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091942\/revisions"}],"predecessor-version":[{"id":1091958,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091942\/revisions\/1091958"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1091955"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1091942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1091942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1091942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}