{"id":1070135,"date":"2025-01-08T10:55:25","date_gmt":"2025-01-08T02:55:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1070135.html"},"modified":"2025-01-08T10:55:27","modified_gmt":"2025-01-08T02:55:27","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%88%ac%e5%8f%96%e5%85%ac%e5%8f%b8%e5%90%8d%e5%8d%95-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1070135.html","title":{"rendered":"\u5982\u4f55\u7528python\u722c\u53d6\u516c\u53f8\u540d\u5355"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100942\/8da3929d-d88a-4c27-b209-0bfd089dca5a.webp\" alt=\"\u5982\u4f55\u7528python\u722c\u53d6\u516c\u53f8\u540d\u5355\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u722c\u53d6\u516c\u53f8\u540d\u5355\u7684\u51e0\u4e2a\u6b65\u9aa4\u5305\u62ec\uff1a\u9009\u62e9\u722c\u53d6\u76ee\u6807\u3001\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3001\u89e3\u6790HTML\u63d0\u53d6\u6240\u9700\u6570\u636e\u3001\u5904\u7406\u4e0e\u5b58\u50a8\u6570\u636e\u3002\u91cd\u70b9\u5728\u4e8e\u9009\u62e9\u5408\u9002\u7684\u89e3\u6790\u5e93\uff0c\u5982BeautifulSoup\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u9009\u62e9\u722c\u53d6\u76ee\u6807<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u722c\u53d6\u76ee\u6807\u662f\u6574\u4e2a\u722c\u866b\u9879\u76ee\u7684\u7b2c\u4e00\u6b65\u3002\u8fd9\u4e00\u6b65\u5305\u62ec\u786e\u5b9a\u4f60\u8981\u722c\u53d6\u7684\u7f51\u7ad9\u4ee5\u53ca\u4f60\u8981\u722c\u53d6\u7684\u6570\u636e\u3002\u5728\u9009\u62e9\u76ee\u6807\u7f51\u7ad9\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u8be5\u7f51\u7ad9\u662f\u5426\u5141\u8bb8\u722c\u866b\u8bbf\u95ee\uff0c\u5e76\u786e\u4fdd\u4f60\u9075\u5b88\u8be5\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\u4e2d\u7684\u89c4\u5b9a\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u786e\u5b9a\u76ee\u6807\u7f51\u7ad9\u548c\u6570\u636e<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728\u786e\u5b9a\u76ee\u6807\u7f51\u7ad9\u4e4b\u524d\uff0c\u9996\u5148\u8981\u660e\u786e\u4f60\u9700\u8981\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982\u516c\u53f8\u540d\u79f0\u3001\u5730\u5740\u3001\u8054\u7cfb\u65b9\u5f0f\u7b49\u3002<\/li>\n<li>\u6d4f\u89c8\u76ee\u6807\u7f51\u7ad9\uff0c\u627e\u5230\u5305\u542b\u6240\u9700\u6570\u636e\u7684\u7f51\u9875\uff0c\u5e76\u786e\u5b9a\u8fd9\u4e9b\u6570\u636e\u5728\u7f51\u9875\u4e2d\u7684\u7ed3\u6784\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\u89c4\u5b9a\u4e86\u722c\u866b\u53ef\u4ee5\u8bbf\u95ee\u7684\u8303\u56f4\u3002\u8bbf\u95ee\u76ee\u6807\u7f51\u7ad9\u7684\u6839\u76ee\u5f55\u4e0b\u7684robots.txt\u6587\u4ef6\uff08\u4f8b\u5982\uff1a<a href=\"https:\/\/www.example.com\/robots.txt%EF%BC%89%EF%BC%8C%E6%9F%A5%E7%9C%8B%E6%98%AF%E5%90%A6%E5%85%81%E8%AE%B8%E7%88%AC%E5%8F%96%E3%80%82\">https:\/\/www.example.com\/robots.txt\uff09\uff0c\u67e5\u770b\u662f\u5426\u5141\u8bb8\u722c\u53d6\u3002<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u5728\u786e\u5b9a\u4e86\u76ee\u6807\u7f51\u7ad9\u548c\u6570\u636e\u4e4b\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u53d1\u9001HTTP\u8bf7\u6c42\u4ee5\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002\u6211\u4eec\u5c06\u4f7f\u7528Python\u7684requests\u5e93\u6765\u5b8c\u6210\u8fd9\u4e00\u6b65\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5requests\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u9996\u5148\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86requests\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a\n<pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u53d1\u9001HTTP\u8bf7\u6c42<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\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\uff1a\n<pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;https:\/\/www.example.com\/companies&#39;<\/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>    print(html_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. Status code: {response.status_code}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u89e3\u6790HTML\u63d0\u53d6\u6240\u9700\u6570\u636e<\/p>\n<\/p>\n<p><p>\u83b7\u53d6\u4e86\u7f51\u9875\u5185\u5bb9\u4e4b\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u89e3\u6790HTML\u4ee5\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u6211\u4eec\u5c06\u4f7f\u7528BeautifulSoup\u5e93\u6765\u5b8c\u6210\u8fd9\u4e00\u6b65\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5BeautifulSoup\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5982\u679c\u6ca1\u6709\u5b89\u88c5BeautifulSoup\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a\n<pre><code class=\"language-bash\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528BeautifulSoup\u5e93\u89e3\u6790HTML\u5185\u5bb9\u5e76\u63d0\u53d6\u516c\u53f8\u540d\u5355\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a\n<pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<p>company_list = []<\/p>\n<p>for company in soup.find_all(&#39;div&#39;, class_=&#39;company-name&#39;):<\/p>\n<p>    company_name = company.get_text(strip=True)<\/p>\n<p>    company_list.append(company_name)<\/p>\n<p>print(company_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5904\u7406\u4e0e\u5b58\u50a8\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u63d0\u53d6\u4e86\u6240\u9700\u6570\u636e\u4e4b\u540e\uff0c\u6700\u540e\u4e00\u6b65\u662f\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u5e76\u5b58\u50a8\u3002\u4f60\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6587\u4ef6\u3001\u6570\u636e\u5e93\u6216\u8005\u5176\u4ed6\u5b58\u50a8\u7cfb\u7edf\u4e2d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b58\u50a8\u5230\u6587\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5230CSV\u6587\u4ef6\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a\n<pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;company_list.csv&#39;, mode=&#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerow([&#39;Company Name&#39;])<\/p>\n<p>    for company in company_list:<\/p>\n<p>        writer.writerow([company])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5b58\u50a8\u5230\u6570\u636e\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4f7f\u7528SQLite\u6570\u636e\u5e93\uff1a\n<pre><code class=\"language-python\">import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;company_list.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS companies (<\/p>\n<p>                      id INTEGER PRIMARY KEY AUTOINCREMENT,<\/p>\n<p>                      name TEXT NOT NULL)&#39;&#39;&#39;)<\/p>\n<p>for company in company_list:<\/p>\n<p>    cursor.execute(&#39;INSERT INTO companies (name) VALUES (?)&#39;, (company,))<\/p>\n<p>conn.commit()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528Python\u722c\u53d6\u516c\u53f8\u540d\u5355\u5e76\u5b58\u50a8\u5230\u6240\u9700\u7684\u5b58\u50a8\u7cfb\u7edf\u4e2d\u3002\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6839\u636e\u76ee\u6807\u7f51\u7ad9\u7684\u5177\u4f53\u7ed3\u6784\u548c\u6570\u636e\u683c\u5f0f\u8fdb\u884c\u9002\u5f53\u7684\u8c03\u6574\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u722c\u53d6\u516c\u53f8\u540d\u5355\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u722c\u53d6\u516c\u53f8\u540d\u5355\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u81f3\u5173\u91cd\u8981\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ec<code>requests<\/code>\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0c<code>BeautifulSoup<\/code>\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863\uff0c<code>Scrapy<\/code>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u722c\u866b\u6846\u67b6\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u7f51\u7ad9\u3002\u6839\u636e\u4f60\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u7684\u5e93\u6765\u63d0\u9ad8\u722c\u53d6\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u722c\u53d6\u516c\u53f8\u540d\u5355\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6cd5\u5f8b\u6cd5\u89c4\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u7f51\u7edc\u722c\u866b\u65f6\uff0c\u4e86\u89e3\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4e0d\u540c\u56fd\u5bb6\u548c\u5730\u533a\u5bf9\u6570\u636e\u6536\u96c6\u6709\u4e0d\u540c\u7684\u6cd5\u5f8b\u9650\u5236\uff0c\u5c24\u5176\u662f\u6d89\u53ca\u5230\u4e2a\u4eba\u4fe1\u606f\u548c\u5546\u4e1a\u4fe1\u606f\u65f6\u3002\u786e\u4fdd\u9075\u5faa\u7f51\u7ad9\u7684<code>robots.txt<\/code>\u6587\u4ef6\u4e2d\u7684\u722c\u866b\u89c4\u5219\uff0c\u5e76\u5c0a\u91cd\u7f51\u7ad9\u7684\u4f7f\u7528\u6761\u6b3e\uff0c\u4ee5\u907f\u514d\u6cd5\u5f8b\u98ce\u9669\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 \/>\u8bb8\u591a\u7f51\u7ad9\u4e3a\u4e86\u4fdd\u62a4\u81ea\u8eab\u6570\u636e\uff0c\u4f1a\u8bbe\u7f6e\u53cd\u722c\u866b\u673a\u5236\u3002\u5e38\u89c1\u7684\u53cd\u722c\u866b\u6280\u672f\u5305\u62ecIP\u5c01\u7981\u3001\u8bf7\u6c42\u9891\u7387\u9650\u5236\u3001\u9a8c\u8bc1\u7801\u7b49\u3002\u4e3a\u5e94\u5bf9\u8fd9\u4e9b\u95ee\u9898\uff0c\u53ef\u4ee5\u91c7\u7528\u65cb\u8f6c\u4ee3\u7406\u3001\u8bbe\u7f6e\u8bf7\u6c42\u5934\u3001\u589e\u52a0\u8bf7\u6c42\u95f4\u9694\u65f6\u95f4\u7b49\u7b56\u7565\uff0c\u4ee5\u51cf\u5c0f\u88ab\u68c0\u6d4b\u7684\u98ce\u9669\uff0c\u5e76\u63d0\u9ad8\u722c\u53d6\u7684\u6210\u529f\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u722c\u53d6\u516c\u53f8\u540d\u5355\u7684\u51e0\u4e2a\u6b65\u9aa4\u5305\u62ec\uff1a\u9009\u62e9\u722c\u53d6\u76ee\u6807\u3001\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3001\u89e3\u6790HTML\u63d0\u53d6\u6240\u9700\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1070141,"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\/1070135"}],"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=1070135"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070135\/revisions"}],"predecessor-version":[{"id":1070144,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070135\/revisions\/1070144"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1070141"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1070135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1070135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1070135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}