{"id":1143306,"date":"2025-01-08T22:48:57","date_gmt":"2025-01-08T14:48:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1143306.html"},"modified":"2025-01-08T22:48:59","modified_gmt":"2025-01-08T14:48:59","slug":"python%e5%a6%82%e4%bd%95%e7%88%ac%e5%8f%96%e6%a0%87%e7%ad%be%e4%b8%8b%e4%b8%80%e8%a1%8c%e7%9a%84%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1143306.html","title":{"rendered":"python\u5982\u4f55\u722c\u53d6\u6807\u7b7e\u4e0b\u4e00\u884c\u7684\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181012\/14298a63-1354-4604-a5a9-cd5975b60977.webp\" alt=\"python\u5982\u4f55\u722c\u53d6\u6807\u7b7e\u4e0b\u4e00\u884c\u7684\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>\u722c\u53d6\u7279\u5b9a\u6807\u7b7e\u4e0b\u4e00\u884c\u5185\u5bb9\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528BeautifulSoup\u3001XPath\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u6280\u672f\u3002<\/strong> \u5728\u5177\u4f53\u5b9e\u73b0\u4e2d\uff0c<strong>\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u3001\u9009\u62e9\u7279\u5b9a\u6807\u7b7e\u3001\u83b7\u53d6\u6807\u7b7e\u4e0b\u4e00\u884c\u5185\u5bb9<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u5b9e\u73b0Python\u722c\u53d6\u7279\u5b9a\u6807\u7b7e\u4e0b\u4e00\u884c\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5f15\u5165\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Python\u722c\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5e76\u5f15\u5165\u76f8\u5173\u7684\u5e93\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ec<code>requests<\/code>\u3001<code>BeautifulSoup<\/code>\u4ee5\u53ca<code>lxml<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b89\u88c5\u6240\u9700\u5e93<\/p>\n<p>pip install requests beautifulsoup4 lxml<\/p>\n<h2><strong>\u5f15\u5165\u6240\u9700\u5e93<\/strong><\/h2>\n<p>import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bf7\u6c42\u76ee\u6807\u7f51\u9875<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>requests<\/code>\u5e93\u83b7\u53d6\u76ee\u6807\u7f51\u9875\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">url = &#39;http:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<h2><strong>\u786e\u4fdd\u8bf7\u6c42\u6210\u529f<\/strong><\/h2>\n<p>if response.status_code == 200:<\/p>\n<p>    page_content = response.content<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to retrieve the page. Status code: {response.status_code}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790HTML\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>BeautifulSoup<\/code>\u89e3\u6790\u83b7\u53d6\u7684HTML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">soup = BeautifulSoup(page_content, &#39;lxml&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u9009\u62e9\u7279\u5b9a\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u9700\u8981\u722c\u53d6\u7684\u7279\u5b9a\u6807\u7b7e\uff0c\u5e76\u83b7\u53d6\u8be5\u6807\u7b7e\u4e0b\u4e00\u884c\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6211\u4eec\u8981\u722c\u53d6\u7684\u662f &lt;div class=&quot;target&quot;&gt; \u6807\u7b7e\u4e0b\u4e00\u884c\u7684\u5185\u5bb9<\/p>\n<p>target_div = soup.find(&#39;div&#39;, class_=&#39;target&#39;)<\/p>\n<p>if target_div:<\/p>\n<p>    # \u83b7\u53d6\u76ee\u6807\u6807\u7b7e\u7684\u4e0b\u4e00\u884c\u5185\u5bb9\uff0c\u5047\u8bbe\u662f\u7d27\u63a5\u7740\u7684 &lt;p&gt; \u6807\u7b7e<\/p>\n<p>    next_sibling = target_div.find_next_sibling(&#39;p&#39;)<\/p>\n<p>    if next_sibling:<\/p>\n<p>        print(&quot;Content of the next line:&quot;, next_sibling.get_text())<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;No next sibling found.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Target div not found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u590d\u6742\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7f51\u9875\u7ed3\u6784\u53ef\u80fd\u590d\u6742\u591a\u53d8\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8c03\u6574\u722c\u53d6\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u5d4c\u5957\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u76ee\u6807\u6807\u7b7e\u5d4c\u5957\u5728\u5176\u4ed6\u6807\u7b7e\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u5c42\u9009\u62e9\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9009\u62e9\u5d4c\u5957\u7ed3\u6784<\/p>\n<p>nested_div = soup.select_one(&#39;div.outer &gt; div.inner &gt; div.target&#39;)<\/p>\n<p>if nested_div:<\/p>\n<p>    next_sibling = nested_div.find_next_sibling(&#39;p&#39;)<\/p>\n<p>    if next_sibling:<\/p>\n<p>        print(&quot;Content of the next line:&quot;, next_sibling.get_text())<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;No next sibling found.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Nested target div not found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684HTML\u7ed3\u6784\u6216\u4e0d\u89c4\u5219\u6807\u7b7e\uff0c\u53ef\u4ee5\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u627e\u76ee\u6807\u6807\u7b7e<\/strong><\/h2>\n<p>regex = re.compile(r&#39;&lt;div class=&quot;target&quot;&gt;(.*?)&lt;\/div&gt;&#39;, re.DOTALL)<\/p>\n<p>match = regex.search(page_content.decode(&#39;utf-8&#39;))<\/p>\n<p>if match:<\/p>\n<p>    # \u83b7\u53d6\u5339\u914d\u6807\u7b7e\u540e\u7684\u5185\u5bb9<\/p>\n<p>    following_content = match.group(1).find_next_sibling(&#39;p&#39;)<\/p>\n<p>    print(&quot;Content of the next line:&quot;, following_content.get_text())<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No matching content found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u52a8\u6001\u7f51\u9875<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9\u7684\u7f51\u9875\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528Selenium\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\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<h2><strong>\u8bbe\u7f6e\u6d4f\u89c8\u5668\u9a71\u52a8\uff08\u9700\u8981\u4e0b\u8f7d\u6d4f\u89c8\u5668\u9a71\u52a8\uff0c\u5982ChromeDriver\uff09<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=&#39;\/path\/to\/chromedriver&#39;)<\/p>\n<p>driver.get(url)<\/p>\n<h2><strong>\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210<\/strong><\/h2>\n<p>driver.implicitly_wait(10)<\/p>\n<h2><strong>\u83b7\u53d6\u76ee\u6807\u6807\u7b7e\u548c\u5176\u4e0b\u4e00\u884c\u5185\u5bb9<\/strong><\/h2>\n<p>target_element = driver.find_element(By.CLASS_NAME, &#39;target&#39;)<\/p>\n<p>if target_element:<\/p>\n<p>    next_sibling = target_element.find_element(By.XPATH, &#39;.\/following-sibling::p[1]&#39;)<\/p>\n<p>    if next_sibling:<\/p>\n<p>        print(&quot;Content of the next line:&quot;, next_sibling.text)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;No next sibling found.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Target element not found.&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><h3>\u4e03\u3001\u5904\u7406\u5f02\u5e38\u548c\u8fb9\u754c\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u5f02\u5e38\u548c\u8fb9\u754c\u60c5\u51b5\u975e\u5e38\u91cd\u8981\u3002\u9700\u8981\u8003\u8651\u5404\u79cd\u53ef\u80fd\u7684\u9519\u8bef\u60c5\u51b5\uff0c\u5982\u6807\u7b7e\u4e0d\u5b58\u5728\u3001\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    response.raise_for_status()<\/p>\n<p>    page_content = response.content<\/p>\n<p>    soup = BeautifulSoup(page_content, &#39;lxml&#39;)<\/p>\n<p>    target_div = soup.find(&#39;div&#39;, class_=&#39;target&#39;)<\/p>\n<p>    if target_div:<\/p>\n<p>        next_sibling = target_div.find_next_sibling(&#39;p&#39;)<\/p>\n<p>        if next_sibling:<\/p>\n<p>            print(&quot;Content of the next line:&quot;, next_sibling.get_text())<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;No next sibling found.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Target div not found.&quot;)<\/p>\n<p>except requests.RequestException as e:<\/p>\n<p>    print(f&quot;Request failed: {e}&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u6709\u6548\u5730\u722c\u53d6\u7f51\u9875\u4e2d\u7279\u5b9a\u6807\u7b7e\u4e0b\u4e00\u884c\u7684\u5185\u5bb9\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528<code>requests<\/code>\u548c<code>BeautifulSoup<\/code>\u5e93\u8fdb\u884c\u7f51\u9875\u5185\u5bb9\u8bf7\u6c42\u548c\u89e3\u6790\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5904\u7406\u5d4c\u5957\u7ed3\u6784\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u52a8\u6001\u7f51\u9875\u7b49\u590d\u6742\u60c5\u51b5\u7684\u6280\u5de7\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u5904\u7406\u7f51\u9875\u722c\u53d6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p><strong>\u6838\u5fc3\u70b9\u603b\u7ed3\uff1a<\/strong>  <\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u548c\u5f15\u5165\u6240\u9700\u5e93<\/li>\n<li>\u8bf7\u6c42\u76ee\u6807\u7f51\u9875<\/li>\n<li>\u89e3\u6790HTML\u5185\u5bb9<\/li>\n<li>\u9009\u62e9\u7279\u5b9a\u6807\u7b7e<\/li>\n<li>\u5904\u7406\u590d\u6742\u7ed3\u6784<\/li>\n<li>\u5904\u7406\u52a8\u6001\u7f51\u9875<\/li>\n<li>\u5904\u7406\u5f02\u5e38\u548c\u8fb9\u754c\u60c5\u51b5<\/li>\n<\/ol>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u722c\u53d6\u7279\u5b9a\u6807\u7b7e\u7684\u4e0b\u4e00\u884c\u5185\u5bb9\uff1f<\/strong><br \/>\u8981\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u5e93\u8fdb\u884cHTML\u89e3\u6790\u3002\u9996\u5148\uff0c\u901a\u8fc7requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u6587\u6863\uff0c\u5b9a\u4f4d\u5230\u76ee\u6807\u6807\u7b7e\uff0c\u63a5\u7740\u63d0\u53d6\u8be5\u6807\u7b7e\u7684\u4e0b\u4e00\u884c\u5185\u5bb9\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u53d1\u9001\u8bf7\u6c42\u3001\u89e3\u6790HTML\u3001\u67e5\u627e\u6807\u7b7e\u53ca\u63d0\u53d6\u76f8\u5e94\u6587\u672c\u3002<\/p>\n<p><strong>\u722c\u53d6\u7f51\u9875\u65f6\uff0c\u5982\u4f55\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\u901a\u5e38\u901a\u8fc7JavaScript\u751f\u6210\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6bd5\u540e\u518d\u8fdb\u884c\u5185\u5bb9\u63d0\u53d6\u3002Selenium\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5904\u7406AJAX\u8bf7\u6c42\u548cJavaScript\u6e32\u67d3\u7684\u5185\u5bb9\uff0c\u4ece\u800c\u786e\u4fdd\u83b7\u53d6\u5230\u5b8c\u6574\u7684\u7f51\u9875\u6570\u636e\u3002<\/p>\n<p><strong>\u5728\u722c\u53d6\u8fc7\u7a0b\u4e2d\uff0c\u5982\u4f55\u5e94\u5bf9\u53cd\u722c\u866b\u673a\u5236\uff1f<\/strong><br \/>\u7f51\u7ad9\u901a\u5e38\u4f1a\u91c7\u53d6\u53cd\u722c\u866b\u63aa\u65bd\u6765\u4fdd\u62a4\u6570\u636e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5408\u7406\u7684\u8bf7\u6c42\u5934\u3001\u4f7f\u7528\u4ee3\u7406IP\u4ee5\u53ca\u63a7\u5236\u722c\u53d6\u901f\u5ea6\u6765\u51cf\u5c11\u88ab\u68c0\u6d4b\u7684\u98ce\u9669\u3002\u6b64\u5916\uff0c\u968f\u673a\u5316\u8bf7\u6c42\u95f4\u9694\u548c\u4f7f\u7528\u7528\u6237\u4ee3\u7406\u5b57\u7b26\u4e32\u4e5f\u6709\u52a9\u4e8e\u63d0\u9ad8\u722c\u866b\u7684\u9690\u853d\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u722c\u53d6\u7279\u5b9a\u6807\u7b7e\u4e0b\u4e00\u884c\u5185\u5bb9\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528BeautifulSoup\u3001XPath\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u6280\u672f\u3002 \u5728\u5177\u4f53\u5b9e\u73b0\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1143319,"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\/1143306"}],"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=1143306"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143306\/revisions"}],"predecessor-version":[{"id":1143323,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143306\/revisions\/1143323"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1143319"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1143306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1143306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1143306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}