{"id":1114257,"date":"2025-01-08T17:55:31","date_gmt":"2025-01-08T09:55:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1114257.html"},"modified":"2025-01-08T17:55:33","modified_gmt":"2025-01-08T09:55:33","slug":"python%e5%a6%82%e4%bd%95%e7%88%ac%e5%8f%96%e4%b8%80%e6%ae%b5%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1114257.html","title":{"rendered":"python\u5982\u4f55\u722c\u53d6\u4e00\u6bb5\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075524\/6a956ccb-4089-4697-9188-3693cec8c85b.webp\" alt=\"python\u5982\u4f55\u722c\u53d6\u4e00\u6bb5\u6570\u5b57\" \/><\/p>\n<p><p> <strong>\u8981\u4f7f\u7528Python\u722c\u53d6\u4e00\u6bb5\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u6765\u53d1\u9001HTTP\u8bf7\u6c42\uff0cBeautifulSoup\u5e93\u6765\u89e3\u6790HTML\u7f51\u9875\u4e2d\u7684\u6570\u636e\uff0cre\u6a21\u5757\u6765\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528requests\u5e93\u6765\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0cBeautifulSoup\u5e93\u6765\u89e3\u6790HTML\u7ed3\u6784\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u7279\u5b9a\u7684\u6570\u5b57\u4fe1\u606f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002<strong>\u5b89\u88c5\u8fd9\u4e9b\u5e93<\/strong>\uff0c<strong>\u7f16\u5199\u722c\u866b\u4ee3\u7801<\/strong>\uff0c<strong>\u89e3\u6790HTML\u7ed3\u6784\uff0c\u63d0\u53d6\u6240\u9700\u6570\u636e<\/strong>\u662f\u8fdb\u884c\u7f51\u9875\u6570\u636e\u722c\u53d6\u7684\u57fa\u672c\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Python\u722c\u53d6\u7f51\u9875\u4e2d\u7684\u4e00\u6bb5\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86requests\u548cBeautifulSoup\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\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><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\u5e76\u83b7\u53d6\u7f51\u9875\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;https:\/\/example.com&#39;  # \u66ff\u6362\u4e3a\u4f60\u8981\u722c\u53d6\u7684\u7f51\u9875URL<\/p>\n<p>response = requests.get(url)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    html_content = response.text<\/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 webpage. Status code: {response.status_code}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790HTML\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u5e93\u89e3\u6790HTML\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4ece\u7f51\u9875\u5185\u5bb9\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u5047\u8bbe\u7f51\u9875\u4e2d\u7684\u6570\u5b57\u5728\u4e00\u4e2a\u7279\u5b9a\u7684HTML\u5143\u7d20\u4e2d\uff0c\u4f8b\u5982 &lt;div class=&quot;number&quot;&gt;12345&lt;\/div&gt;<\/strong><\/h2>\n<p>number_divs = soup.find_all(&#39;div&#39;, class_=&#39;number&#39;)<\/p>\n<p>numbers = [re.search(r&#39;\\d+&#39;, div.text).group() for div in number_divs]<\/p>\n<p>print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u722c\u53d6\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5c06\u722c\u53d6\u5230\u7684\u6570\u5b57\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u4fdd\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6570\u5b57\u4fdd\u5b58\u5230\u4e00\u4e2a\u6587\u4ef6<\/p>\n<p>with open(&#39;numbers.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for number in numbers:<\/p>\n<p>        file.write(number + &#39;\\n&#39;)<\/p>\n<p>print(&quot;Numbers have been saved to numbers.txt&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684Python\u4ee3\u7801\u793a\u4f8b\uff0c\u7528\u4e8e\u722c\u53d6\u7f51\u9875\u4e2d\u7684\u4e00\u6bb5\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import re<\/p>\n<p>def fetch_numbers(url):<\/p>\n<p>    # \u53d1\u9001HTTP\u8bf7\u6c42<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code != 200:<\/p>\n<p>        print(f&quot;Failed to retrieve the webpage. Status code: {response.status_code}&quot;)<\/p>\n<p>        return []<\/p>\n<p>    # \u89e3\u6790HTML\u5185\u5bb9<\/p>\n<p>    soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u5b57<\/p>\n<p>    number_divs = soup.find_all(&#39;div&#39;, class_=&#39;number&#39;)<\/p>\n<p>    numbers = [re.search(r&#39;\\d+&#39;, div.text).group() for div in number_divs]<\/p>\n<p>    return numbers<\/p>\n<p>def save_numbers(numbers, filename):<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        for number in numbers:<\/p>\n<p>            file.write(number + &#39;\\n&#39;)<\/p>\n<p>    print(f&quot;Numbers have been saved to {filename}&quot;)<\/p>\n<h2><strong>\u793a\u4f8bURL\uff0c\u66ff\u6362\u4e3a\u5b9e\u9645\u7684\u7f51\u9875URL<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com&#39;<\/p>\n<p>numbers = fetch_numbers(url)<\/p>\n<p>if numbers:<\/p>\n<p>    save_numbers(numbers, &#39;numbers.txt&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No numbers found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u722c\u53d6\u4e00\u6bb5\u6570\u5b57\u7684\u6b65\u9aa4\u5305\u62ec\uff1a<strong>\u5b89\u88c5\u6240\u9700\u5e93\uff08requests\u3001BeautifulSoup\u3001re\uff09\uff0c\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u89e3\u6790HTML\u7ed3\u6784\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u5b57\uff0c\u5904\u7406\u722c\u53d6\u7684\u6570\u636e<\/strong>\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u51fa\u6240\u9700\u7684\u6570\u5b57\u4fe1\u606f\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u722c\u53d6\u6570\u5b57\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u722c\u53d6\u5176\u4ed6\u7c7b\u578b\u7684\u7f51\u9875\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u7f51\u9875\u6570\u636e\u6293\u53d6\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fdb\u884c\u7f51\u9875\u6570\u636e\u6293\u53d6\u65f6\uff0c\u53ef\u4ee5\u5229\u7528\u5e93\u5982Requests\u548cBeautiful Soup\u6765\u83b7\u53d6\u548c\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3002\u9996\u5148\uff0c\u4f7f\u7528Requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u4ee5\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002\u63a5\u7740\uff0c\u5229\u7528Beautiful Soup\u89e3\u6790HTML\uff0c\u63d0\u53d6\u6240\u9700\u6570\u636e\u3002\u901a\u8fc7CSS\u9009\u62e9\u5668\u6216XPath\uff0c\u53ef\u4ee5\u7cbe\u786e\u5b9a\u4f4d\u5230\u5305\u542b\u6570\u5b57\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u5728\u722c\u53d6\u6570\u5b57\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6cd5\u5f8b\u548c\u9053\u5fb7\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u7f51\u9875\u6570\u636e\u6293\u53d6\u4e4b\u524d\uff0c\u4e86\u89e3\u76f8\u5173\u6cd5\u5f8b\u548c\u9053\u5fb7\u95ee\u9898\u81f3\u5173\u91cd\u8981\u3002\u786e\u4fdd\u9075\u5b88\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\u4e2d\u7684\u722c\u866b\u89c4\u5219\uff0c\u5c0a\u91cd\u7248\u6743\u548c\u9690\u79c1\u6743\u3002\u5728\u6293\u53d6\u6570\u636e\u65f6\uff0c\u907f\u514d\u5bf9\u7f51\u7ad9\u9020\u6210\u8d1f\u62c5\uff0c\u5efa\u8bae\u8bbe\u7f6e\u5408\u7406\u7684\u8bf7\u6c42\u95f4\u9694\uff0c\u4ee5\u51cf\u5c11\u5bf9\u670d\u52a1\u5668\u7684\u538b\u529b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u722c\u53d6\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u53cd\u722c\u866b\u673a\u5236\uff1f<\/strong><br \/>\u53cd\u722c\u866b\u673a\u5236\u53ef\u80fd\u4f1a\u963b\u6b62\u7a0b\u5e8f\u6b63\u5e38\u6293\u53d6\u6570\u636e\u3002\u5f53\u9047\u5230\u8fd9\u79cd\u60c5\u51b5\u65f6\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u66f4\u6539\u8bf7\u6c42\u5934\u90e8\uff0c\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u6216\u8005\u4f7f\u7528\u4ee3\u7406IP\u6765\u9690\u85cf\u771f\u5b9eIP\u5730\u5740\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u968f\u673a\u5316\u8bf7\u6c42\u95f4\u9694\u548c\u4f7f\u7528Cookies\u4e5f\u80fd\u6709\u6548\u964d\u4f4e\u88ab\u8bc6\u522b\u4e3a\u722c\u866b\u7684\u98ce\u9669\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4f7f\u7528Python\u722c\u53d6\u4e00\u6bb5\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u6765\u53d1\u9001HTTP\u8bf7\u6c42\uff0cBeautifulSoup\u5e93\u6765\u89e3 [&hellip;]","protected":false},"author":3,"featured_media":1114266,"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\/1114257"}],"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=1114257"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114257\/revisions"}],"predecessor-version":[{"id":1114267,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114257\/revisions\/1114267"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1114266"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1114257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1114257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1114257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}