{"id":1178656,"date":"2025-01-15T18:13:37","date_gmt":"2025-01-15T10:13:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1178656.html"},"modified":"2025-01-15T18:13:41","modified_gmt":"2025-01-15T10:13:41","slug":"%e5%a6%82%e4%bd%95%e6%8a%93%e5%8f%96%e7%bd%91%e9%a1%b5p%e6%a0%87%e7%ad%bepython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1178656.html","title":{"rendered":"\u5982\u4f55\u6293\u53d6\u7f51\u9875p\u6807\u7b7epython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113255\/960e5eb0-d24d-4bcb-beb2-4b8530e9bfe3.webp\" alt=\"\u5982\u4f55\u6293\u53d6\u7f51\u9875p\u6807\u7b7epython\" \/><\/p>\n<p><p> <strong>\u6293\u53d6\u7f51\u9875p\u6807\u7b7e\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528BeautifulSoup\u3001Scrapy\u3001Selenium\u7b49\u5de5\u5177\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528BeautifulSoup\u662f\u6700\u7b80\u5355\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u4e4b\u4e00\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528BeautifulSoup\u6765\u6293\u53d6\u7f51\u9875\u4e2d\u7684p\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5e76\u5bfc\u5165\u9700\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5b89\u88c5\u4e86requests\u548cBeautifulSoup\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p>pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">url = &#39;http:\/\/example.com&#39;  # \u66ff\u6362\u4e3a\u4f60\u8981\u6293\u53d6\u7684\u7f51\u9875URL<\/p>\n<p>response = requests.get(url)<\/p>\n<h2><strong>\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\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 webpage with status code {response.status_code}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790\u7f51\u9875\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u89e3\u6790\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u63d0\u53d6\u6240\u6709\u7684p\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">soup = BeautifulSoup(page_content, &#39;html.parser&#39;)<\/p>\n<p>p_tags = soup.find_all(&#39;p&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u63d0\u53d6\u7684p\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5bf9\u63d0\u53d6\u7684p\u6807\u7b7e\u8fdb\u884c\u5404\u79cd\u5904\u7406\uff0c\u4f8b\u5982\u8f93\u51fa\u5185\u5bb9\u3001\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u8fdb\u884c\u6570\u636e\u5206\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for p in p_tags:<\/p>\n<p>    print(p.text)  # \u8f93\u51fap\u6807\u7b7e\u4e2d\u7684\u6587\u672c\u5185\u5bb9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u6293\u53d6\u7f51\u9875\u4e2d\u7684p\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>def fetch_p_tags(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        page_content = response.content<\/p>\n<p>        soup = BeautifulSoup(page_content, &#39;html.parser&#39;)<\/p>\n<p>        p_tags = soup.find_all(&#39;p&#39;)<\/p>\n<p>        return [p.text for p in p_tags]<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Failed to retrieve webpage with status code {response.status_code}&quot;)<\/p>\n<p>        return []<\/p>\n<h2><strong>\u793a\u4f8bURL<\/strong><\/h2>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>p_tags_content = fetch_p_tags(url)<\/p>\n<h2><strong>\u8f93\u51fa\u63d0\u53d6\u7684p\u6807\u7b7e\u5185\u5bb9<\/strong><\/h2>\n<p>for content in p_tags_content:<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u52a8\u6001\u7f51\u9875<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u7f51\u9875\u662f\u901a\u8fc7JavaScript\u52a8\u6001\u751f\u6210\u5185\u5bb9\u7684\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0brequests\u548cBeautifulSoup\u53ef\u80fd\u65e0\u6cd5\u76f4\u63a5\u83b7\u53d6\u5230\u6240\u9700\u7684\u5185\u5bb9\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u52a8\u6001\u7f51\u9875\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u6765\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528Selenium\u5904\u7406\u52a8\u6001\u7f51\u9875<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Selenium\u548c\u6d4f\u89c8\u5668\u9a71\u52a8\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\u8f7d\u9002\u5408\u4f60\u6d4f\u89c8\u5668\u7684\u9a71\u52a8\uff0c\u4f8b\u5982ChromeDriver\uff0c\u5e76\u5c06\u5176\u653e\u5728\u7cfb\u7edfPATH\u4e2d\u6216\u6307\u5b9a\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>def fetch_dynamic_p_tags(url):<\/p>\n<p>    driver = webdriver.Chrome(executable_path=&#39;\/path\/to\/chromedriver&#39;)  # \u66ff\u6362\u4e3a\u5b9e\u9645\u7684chromedriver\u8def\u5f84<\/p>\n<p>    driver.get(url)<\/p>\n<p>    page_content = driver.page_source<\/p>\n<p>    driver.quit()<\/p>\n<p>    soup = BeautifulSoup(page_content, &#39;html.parser&#39;)<\/p>\n<p>    p_tags = soup.find_all(&#39;p&#39;)<\/p>\n<p>    return [p.text for p in p_tags]<\/p>\n<h2><strong>\u793a\u4f8bURL<\/strong><\/h2>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>p_tags_content = fetch_dynamic_p_tags(url)<\/p>\n<h2><strong>\u8f93\u51fa\u63d0\u53d6\u7684p\u6807\u7b7e\u5185\u5bb9<\/strong><\/h2>\n<p>for content in p_tags_content:<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528BeautifulSoup\u548crequests\u5e93\u53ef\u4ee5\u8f7b\u677e\u6293\u53d6\u9759\u6001\u7f51\u9875\u7684p\u6807\u7b7e\uff0c\u800c\u5bf9\u4e8e\u52a8\u6001\u7f51\u9875\uff0c\u53ef\u4ee5\u501f\u52a9Selenium\u6765\u5904\u7406\u3002<\/strong> \u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u6293\u53d6\u7f51\u9875\u4e2d\u7684p\u6807\u7b7e\uff0c\u5e76\u5bf9\u5176\u5185\u5bb9\u8fdb\u884c\u5404\u79cd\u5904\u7406\u548c\u5206\u6790\u3002\u6293\u53d6\u7f51\u9875\u5185\u5bb9\u662f\u8fdb\u884c\u6570\u636e\u91c7\u96c6\u548c\u5206\u6790\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u5c06\u5927\u5927\u63d0\u9ad8\u4f60\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6293\u53d6\u7f51\u9875\u4e2d\u7684\u7279\u5b9ap\u6807\u7b7e\u5185\u5bb9\uff1f<\/strong><\/p>\n<p>\u8981\u6293\u53d6\u7f51\u9875\u4e2d\u7684\u7279\u5b9ap\u6807\u7b7e\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u5e93\uff0c\u5982BeautifulSoup\u548crequests\u3002\u9996\u5148\uff0c\u901a\u8fc7requests\u5e93\u53d1\u9001\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\uff0c\u7136\u540e\u7528BeautifulSoup\u89e3\u6790HTML\uff0c\u6700\u540e\u901a\u8fc7\u9009\u62e9\u5668\u63d0\u53d6\u9700\u8981\u7684p\u6807\u7b7e\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\nfrom bs4 import BeautifulSoup\n\nurl = &#39;http:\/\/example.com&#39;\nresponse = requests.get(url)\nsoup = BeautifulSoup(response.content, &#39;html.parser&#39;)\n\n# \u63d0\u53d6\u6240\u6709p\u6807\u7b7e\np_tags = soup.find_all(&#39;p&#39;)\nfor p in p_tags:\n    print(p.get_text())\n<\/code><\/pre>\n<p><strong>\u6293\u53d6p\u6807\u7b7e\u65f6\u5982\u4f55\u5904\u7406\u7f51\u9875\u4e2d\u7684JavaScript\u751f\u6210\u5185\u5bb9\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u7f51\u9875\u5185\u5bb9\u662f\u901a\u8fc7JavaScript\u52a8\u6001\u751f\u6210\u7684\uff0c\u4f7f\u7528requests\u548cBeautifulSoup\u53ef\u80fd\u65e0\u6cd5\u6293\u53d6\u5230\u8fd9\u4e9b\u5185\u5bb9\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Selenium\u5e93\uff0c\u5b83\u80fd\u591f\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u52a0\u8f7d\u6240\u6709JavaScript\u5185\u5bb9\u3002\u901a\u8fc7Selenium\uff0c\u53ef\u4ee5\u542f\u52a8\u4e00\u4e2a\u6d4f\u89c8\u5668\u5b9e\u4f8b\uff0c\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u540e\u518d\u6293\u53d6p\u6807\u7b7e\u5185\u5bb9\u3002\u4ee5\u4e0b\u662fSelenium\u7684\u7b80\u5355\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\n\ndriver = webdriver.Chrome()\ndriver.get(&#39;http:\/\/example.com&#39;)\np_tags = driver.find_elements_by_tag_name(&#39;p&#39;)\n\nfor p in p_tags:\n    print(p.text)\n\ndriver.quit()\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u9700\u8981\u5904\u7406\u6293\u53d6\u5230\u7684\u5185\u5bb9\u8fdb\u884c\u6e05\u6d17\u6216\u5b58\u50a8\uff1f<\/strong><\/p>\n<p>\u6293\u53d6\u5230\u7684\u5185\u5bb9\u5f80\u5f80\u9700\u8981\u8fdb\u884c\u6e05\u6d17\u548c\u5b58\u50a8\u3002\u6839\u636e\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u683c\u3001HTML\u6807\u7b7e\u7b49\u3002\u5b8c\u6210\u6e05\u6d17\u540e\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6587\u4ef6\u3001\u6570\u636e\u5e93\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\u548c\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u5c06\u5185\u5bb9\u5b58\u50a8\u5230\u6587\u672c\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;) as file:\n    for p in p_tags:\n        file.write(p.get_text() + &#39;\\n&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u6293\u53d6\u7f51\u9875\u4e2d\u7684p\u6807\u7b7e\u5185\u5bb9\uff0c\u5e76\u5bf9\u6570\u636e\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6293\u53d6\u7f51\u9875p\u6807\u7b7e\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528BeautifulSoup\u3001Scrapy\u3001Selenium\u7b49\u5de5\u5177\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1178663,"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\/1178656"}],"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=1178656"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178656\/revisions"}],"predecessor-version":[{"id":1178664,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178656\/revisions\/1178664"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1178663"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1178656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1178656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1178656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}