{"id":1054145,"date":"2024-12-31T14:39:51","date_gmt":"2024-12-31T06:39:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1054145.html"},"modified":"2024-12-31T14:39:53","modified_gmt":"2024-12-31T06:39:53","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%81%9a%e6%9c%80%e7%ae%80%e5%8d%95%e7%9a%84%e7%88%ac%e8%99%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1054145.html","title":{"rendered":"\u5982\u4f55\u7528python\u505a\u6700\u7b80\u5355\u7684\u722c\u866b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/3214927f-8942-4178-b47f-f3b587f77048.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u505a\u6700\u7b80\u5355\u7684\u722c\u866b\" \/><\/p>\n<p><p> <strong>\u7528Python\u505a\u6700\u7b80\u5355\u7684\u722c\u866b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u3001\u63d0\u53d6\u6240\u9700\u6570\u636e<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u4e2a\u5e93\u6765\u5b9e\u73b0\u4e00\u4e2a\u6700\u7b80\u5355\u7684Python\u722c\u866b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/p>\n<p>\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u9700\u8981\u5b89\u88c5requests\u548cBeautifulSoup\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\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>\u4e8c\u3001\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;https:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    print(response.text)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to retrieve the webpage&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>requests.get()<\/code>\u65b9\u6cd5\u53d1\u9001\u4e00\u4e2aGET\u8bf7\u6c42\u5230\u6307\u5b9a\u7684URL\uff0c\u5e76\u68c0\u67e5\u54cd\u5e94\u72b6\u6001\u7801\u662f\u5426\u4e3a200\uff08\u8868\u793a\u8bf7\u6c42\u6210\u529f\uff09\u3002\u5982\u679c\u8bf7\u6c42\u6210\u529f\uff0c\u6211\u4eec\u5c06\u6253\u5370\u51fa\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u89e3\u6790HTML\u5185\u5bb9<\/p>\n<p>\u83b7\u53d6\u5230\u7f51\u9875\u5185\u5bb9\u540e\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528BeautifulSoup\u5e93\u6765\u89e3\u6790HTML\uff0c\u5e76\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html_content = response.text<\/p>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u4ee5\u793a\u4f8b\u7684\u65b9\u5f0f\u67e5\u627e\u6240\u6709\u7684&lt;a&gt;\u6807\u7b7e<\/strong><\/h2>\n<p>links = soup.find_all(&#39;a&#39;)<\/p>\n<p>for link in links:<\/p>\n<p>    print(link.get(&#39;href&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06HTML\u5185\u5bb9\u4f20\u9012\u7ed9BeautifulSoup\u6784\u9020\u51fd\u6570\uff0c\u5e76\u6307\u5b9a\u89e3\u6790\u5668\u4e3a<code>html.parser<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>soup.find_all(&#39;a&#39;)<\/code>\u65b9\u6cd5\u67e5\u627e\u6240\u6709\u7684<code>&lt;a&gt;<\/code>\u6807\u7b7e\uff0c\u5e76\u6253\u5370\u51fa\u5b83\u4eec\u7684href\u5c5e\u6027\uff08\u5373\u94fe\u63a5\u5730\u5740\uff09\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u722c\u53d6\u7684\u7ed3\u679c<\/p>\n<p>\u5728\u5b9e\u9645\u7684\u722c\u866b\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u5bf9\u722c\u53d6\u7684\u7ed3\u679c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u4f8b\u5982\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c06\u63d0\u53d6\u5230\u7684\u94fe\u63a5\u4fdd\u5b58\u5230\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;links.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for link in links:<\/p>\n<p>        href = link.get(&#39;href&#39;)<\/p>\n<p>        if href:<\/p>\n<p>            file.write(href + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Python\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a<code>links.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u4ee5\u5199\u6a21\u5f0f(<code>&#39;w&#39;<\/code>)\u5199\u5165\u63d0\u53d6\u5230\u7684\u94fe\u63a5\u5730\u5740\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u53cd\u722c\u63aa\u65bd<\/p>\n<p>\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u7f51\u7ad9\u53ef\u80fd\u4f1a\u8bbe\u7f6e\u4e00\u4e9b\u53cd\u722c\u63aa\u65bd\uff0c\u5982IP\u5c01\u7981\u3001User-Agent\u68c0\u6d4b\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u5934\u6765\u4f2a\u88c5\u6210\u6d4f\u89c8\u5668\uff0c\u4ee5\u7ed5\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u53cd\u722c\u673a\u5236\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">headers = {<\/p>\n<p>    &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, headers=headers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8bbe\u7f6e<code>headers<\/code>\u53c2\u6570\u6765\u6307\u5b9aUser-Agent\uff0c\u4ece\u800c\u4f2a\u88c5\u6210Chrome\u6d4f\u89c8\u5668\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>IP\u88ab\u5c01\u7981<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528\u4ee3\u7406IP\u6c60\uff0c\u5b9a\u671f\u66f4\u6362IP\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u52a8\u6001\u7f51\u9875\u5185\u5bb9<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528Selenium\u7b49\u5de5\u5177\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u6216\u5206\u6790Ajax\u8bf7\u6c42\u5e76\u76f4\u63a5\u8bf7\u6c42\u6570\u636e\u63a5\u53e3\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u5b58\u50a8<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>\uff1a\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u5982CSV\u3001\u6570\u636e\u5e93\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684Python\u722c\u866b\u793a\u4f8b\u4ee3\u7801\uff0c\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u6574\u5408\u5728\u4e00\u8d77\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u76ee\u6807URL<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com&#39;<\/p>\n<h2><strong>\u8bf7\u6c42\u5934\uff0c\u4f2a\u88c5\u6210\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>headers = {<\/p>\n<p>    &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36&#39;<\/p>\n<p>}<\/p>\n<h2><strong>\u53d1\u9001GET\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(url, headers=headers)<\/p>\n<h2><strong>\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if response.status_code == 200:<\/p>\n<p>    # \u89e3\u6790HTML\u5185\u5bb9<\/p>\n<p>    soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<p>    # \u67e5\u627e\u6240\u6709\u7684&lt;a&gt;\u6807\u7b7e<\/p>\n<p>    links = soup.find_all(&#39;a&#39;)<\/p>\n<p>    # \u5c06\u94fe\u63a5\u4fdd\u5b58\u5230\u6587\u4ef6<\/p>\n<p>    with open(&#39;links.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>        for link in links:<\/p>\n<p>            href = link.get(&#39;href&#39;)<\/p>\n<p>            if href:<\/p>\n<p>                file.write(href + &#39;\\n&#39;)<\/p>\n<p>    print(&#39;Links have been saved to links.txt&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;Failed to retrieve the webpage&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u5b9e\u73b0\u4e00\u4e2a\u6700\u7b80\u5355\u7684Python\u722c\u866b\u3002\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u5bf9\u4ee3\u7801\u8fdb\u884c\u6269\u5c55\u548c\u6539\u8fdb\uff0c\u4f8b\u5982\u5904\u7406\u66f4\u591a\u7684\u7f51\u9875\u3001\u63d0\u53d6\u66f4\u591a\u7684\u6570\u636e\u3001\u5904\u7406\u53cd\u722c\u63aa\u65bd\u7b49\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u7528Python\u505a\u7b80\u5355\u722c\u866b\u9700\u8981\u54ea\u4e9b\u57fa\u672c\u5e93\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fdb\u884c\u7b80\u5355\u7684\u722c\u866b\u65f6\uff0c\u4e3b\u8981\u9700\u8981\u5b89\u88c5\u548c\u4f7f\u7528\u51e0\u4e2a\u5e93\u3002\u6700\u5e38\u7528\u7684\u5e93\u5305\u62ec<code>requests<\/code>\uff0c\u7528\u4e8e\u53d1\u9001\u7f51\u7edc\u8bf7\u6c42\u548c\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff1b<code>BeautifulSoup<\/code>\uff0c\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863\u5e76\u63d0\u53d6\u6240\u9700\u6570\u636e\u3002\u6b64\u5916\uff0c\u5982\u679c\u9700\u8981\u5904\u7406\u66f4\u590d\u6742\u7684\u7f51\u9875\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Scrapy<\/code>\u6846\u67b6\uff0c\u8fd9\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u722c\u866b\u5de5\u5177\uff0c\u9002\u5408\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u6293\u53d6\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5904\u7406\u722c\u866b\u4e2d\u9047\u5230\u7684\u53cd\u722c\u63aa\u65bd\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u722c\u866b\u65f6\uff0c\u7f51\u7ad9\u53ef\u80fd\u4f1a\u91c7\u53d6\u5404\u79cd\u53cd\u722c\u63aa\u65bd\uff0c\u4f8b\u5982IP\u5c01\u9501\u3001\u9a8c\u8bc1\u7801\u7b49\u3002\u4e3a\u5e94\u5bf9\u8fd9\u4e9b\u6311\u6218\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff08User-Agent\uff09\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u8bf7\u6c42\uff0c\u589e\u52a0\u8bf7\u6c42\u7684\u968f\u673a\u6027\uff0c\u4f7f\u7528\u4ee3\u7406\u670d\u52a1\u5668\u5207\u6362IP\u5730\u5740\uff0c\u6216\u662f\u5bf9\u8bf7\u6c42\u9891\u7387\u8fdb\u884c\u63a7\u5236\u3002\u6b64\u5916\uff0c\u9047\u5230\u9a8c\u8bc1\u7801\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u56fe\u50cf\u8bc6\u522b\u6280\u672f\u6216\u624b\u52a8\u5904\u7406\u3002<\/p>\n<p><strong>3. \u4f7f\u7528Python\u722c\u866b\u6293\u53d6\u6570\u636e\u540e\uff0c\u5982\u4f55\u5b58\u50a8\u548c\u5206\u6790\u8fd9\u4e9b\u6570\u636e\uff1f<\/strong><br \/>\u6293\u53d6\u5230\u7684\u6570\u636e\u53ef\u4ee5\u5b58\u50a8\u5728\u591a\u79cd\u683c\u5f0f\u4e2d\uff0c\u5e38\u89c1\u7684\u5305\u62ecCSV\u6587\u4ef6\u3001JSON\u6587\u4ef6\u6216\u6570\u636e\u5e93\uff08\u5982SQLite\u3001MySQL\u7b49\uff09\u3002\u6839\u636e\u6570\u636e\u7684\u6027\u8d28\u548c\u540e\u7eed\u5206\u6790\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u5b58\u50a8\u65b9\u5f0f\u3002\u5bf9\u4e8e\u6570\u636e\u5206\u6790\uff0c\u53ef\u4ee5\u5229\u7528<code>pandas<\/code>\u5e93\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u3001\u5904\u7406\u548c\u5206\u6790\uff0c\u7ed3\u5408<code>matplotlib<\/code>\u6216<code>seaborn<\/code>\u7b49\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u76f4\u89c2\u5730\u7406\u89e3\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u505a\u6700\u7b80\u5355\u7684\u722c\u866b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790H [&hellip;]","protected":false},"author":3,"featured_media":1054154,"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\/1054145"}],"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=1054145"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054145\/revisions"}],"predecessor-version":[{"id":1054158,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054145\/revisions\/1054158"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1054154"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1054145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1054145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1054145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}