{"id":1110831,"date":"2025-01-08T17:23:22","date_gmt":"2025-01-08T09:23:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110831.html"},"modified":"2025-01-08T17:23:24","modified_gmt":"2025-01-08T09:23:24","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%8a%8a%e7%88%ac%e8%99%ab%e6%95%b0%e6%8d%ae%e7%94%9f%e6%88%90csv%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1110831.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u628a\u722c\u866b\u6570\u636e\u751f\u6210csv\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073519\/fbe62846-3fa1-46e4-9b7a-b2da08047a7f.webp\" alt=\"python\u4e2d\u5982\u4f55\u628a\u722c\u866b\u6570\u636e\u751f\u6210csv\u8868\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5c06\u722c\u866b\u6570\u636e\u751f\u6210CSV\u8868\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u83b7\u53d6\u6570\u636e\u3001\u89e3\u6790\u6570\u636e\u3001\u521b\u5efaCSV\u6587\u4ef6\u3001\u5199\u5165\u6570\u636e\u3002<strong>\u4f7f\u7528\u5e93\u5982requests\u3001BeautifulSoup\u3001csv\u3001pandas\u3001\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u6b63\u786e<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u4e2a\u5173\u952e\u6b65\u9aa4\uff1a\u4f7f\u7528csv\u5e93\u521b\u5efa\u548c\u5199\u5165CSV\u6587\u4ef6\u3002csv\u5e93\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u4fbf\u7684\u65b9\u6cd5\u6765\u5904\u7406CSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7writer\u5bf9\u8c61\u8fdb\u884c\u884c\u7684\u5199\u5165\uff0c\u786e\u4fdd\u6570\u636e\u7684\u6bcf\u4e00\u884c\u90fd\u80fd\u6b63\u786e\u5199\u5165CSV\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u8981\u8fdb\u884c\u6570\u636e\u722c\u53d6\u5e76\u5904\u7406\u4e3aCSV\u683c\u5f0f\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u4e00\u4e9bPython\u5e93\uff0c\u5982requests\u3001BeautifulSoup\u3001csv\u3002\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests beautifulsoup4 pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u83b7\u53d6\u548c\u89e3\u6790\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4ece\u76ee\u6807\u7f51\u7ad9\u83b7\u53d6\u6570\u636e\uff0c\u5e76\u4f7f\u7528BeautifulSoup\u8fdb\u884c\u89e3\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4ece\u4e00\u4e2a\u7f51\u9875\u4e2d\u63d0\u53d6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>url = &#39;https:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<p>data = []<\/p>\n<p>for item in soup.find_all(&#39;div&#39;, class_=&#39;item&#39;):<\/p>\n<p>    title = item.find(&#39;h2&#39;).text<\/p>\n<p>    price = item.find(&#39;span&#39;, class_=&#39;price&#39;).text<\/p>\n<p>    data.append([title, price])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528csv\u5e93\u751f\u6210CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u548c\u89e3\u6790\u6570\u636e\u540e\uff0c\u6211\u4eec\u9700\u8981\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u3002csv\u5e93\u662fPython\u5185\u7f6e\u7684\u5e93\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>filename = &#39;data.csv&#39;<\/p>\n<p>with open(filename, 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;Title&#39;, &#39;Price&#39;])  # \u5199\u5165\u8868\u5934<\/p>\n<p>    writer.writerows(data)  # \u5199\u5165\u6570\u636e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528pandas\u5e93\u751f\u6210CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>pandas\u5e93\u4e5f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u548c\u5199\u5165CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame(data, columns=[&#39;Title&#39;, &#39;Price&#39;])<\/p>\n<p>df.to_csv(&#39;data.csv&#39;, index=False, encoding=&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5927\u6570\u636e\u91cf\u548c\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u91cf\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25\u3001\u6570\u636e\u4e0d\u5b8c\u6574\u7b49\u60c5\u51b5\u3002\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u548c\u91cd\u8bd5\u673a\u5236\u6765\u63d0\u9ad8\u722c\u866b\u7684\u5065\u58ee\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import random<\/p>\n<p>def fetch_data(url):<\/p>\n<p>    try:<\/p>\n<p>        response = requests.get(url, timeout=10)<\/p>\n<p>        response.r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se_for_status()<\/p>\n<p>        return response.text<\/p>\n<p>    except requests.RequestException as e:<\/p>\n<p>        print(f&quot;Error fetching {url}: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>def parse_data(html):<\/p>\n<p>    soup = BeautifulSoup(html, &#39;html.parser&#39;)<\/p>\n<p>    data = []<\/p>\n<p>    for item in soup.find_all(&#39;div&#39;, class_=&#39;item&#39;):<\/p>\n<p>        title = item.find(&#39;h2&#39;).text<\/p>\n<p>        price = item.find(&#39;span&#39;, class_=&#39;price&#39;).text<\/p>\n<p>        data.append([title, price])<\/p>\n<p>    return data<\/p>\n<p>data = []<\/p>\n<p>url_list = [&#39;https:\/\/example.com\/page1&#39;, &#39;https:\/\/example.com\/page2&#39;]<\/p>\n<p>for url in url_list:<\/p>\n<p>    html = fetch_data(url)<\/p>\n<p>    if html:<\/p>\n<p>        page_data = parse_data(html)<\/p>\n<p>        data.extend(page_data)<\/p>\n<p>    time.sleep(random.uniform(1, 3))  # \u968f\u673a\u7b49\u5f85\u4ee5\u907f\u514d\u88ab\u5c01IP<\/p>\n<p>df = pd.DataFrame(data, columns=[&#39;Title&#39;, &#39;Price&#39;])<\/p>\n<p>df.to_csv(&#39;data.csv&#39;, index=False, encoding=&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u722c\u866b\u6570\u636e\u751f\u6210CSV\u8868\u7684\u6b65\u9aa4\u5305\u62ec\u83b7\u53d6\u6570\u636e\u3001\u89e3\u6790\u6570\u636e\u3001\u521b\u5efaCSV\u6587\u4ef6\u3001\u5199\u5165\u6570\u636e\u3002\u4f7f\u7528\u5e93\u5982requests\u3001BeautifulSoup\u3001csv\u3001pandas\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u548c\u5bfc\u51fa\u6570\u636e\u3002\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u6e05\u6670\u5730\u7406\u89e3\u6bcf\u4e2a\u6b65\u9aa4\u7684\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\uff0c\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u6b63\u786e\uff0c\u5e76\u5904\u7406\u5927\u6570\u636e\u91cf\u548c\u5f02\u5e38\u60c5\u51b5\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9e\u73b0Python\u4e2d\u7684\u722c\u866b\u6570\u636e\u751f\u6210CSV\u8868\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u7559\u8a00\u8ba8\u8bba\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u722c\u866b\u6293\u53d6\u7684\u6570\u636e\u8f6c\u6362\u4e3aCSV\u683c\u5f0f\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u5c06\u722c\u866b\u6293\u53d6\u7684\u6570\u636e\u8f6c\u6362\u4e3aCSV\u683c\u5f0f\u901a\u5e38\u4f7f\u7528<code>pandas<\/code>\u5e93\u6216\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u3002\u4f7f\u7528<code>pandas<\/code>\u53ef\u4ee5\u7b80\u5316\u6570\u636e\u5904\u7406\u548c\u6587\u4ef6\u8f93\u51fa\u7684\u8fc7\u7a0b\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86<code>pandas<\/code>\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install pandas<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6846\u67b6\uff08DataFrame\uff09\uff0c\u5c06\u6293\u53d6\u7684\u6570\u636e\u5b58\u50a8\u5176\u4e2d\uff0c\u6700\u540e\u8c03\u7528<code>to_csv()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f93\u51fa\u4e3aCSV\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u722c\u866b\u6570\u636e\u65f6\uff0c\u5982\u4f55\u6e05\u6d17\u548c\u683c\u5f0f\u5316\u6570\u636e\u4ee5\u4fbf\u751f\u6210CSV\uff1f<\/strong><\/p>\n<p>\u5728\u751f\u6210CSV\u4e4b\u524d\uff0c\u6e05\u6d17\u548c\u683c\u5f0f\u5316\u6570\u636e\u662f\u975e\u5e38\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u7684\u5404\u79cd\u6570\u636e\u5904\u7406\u51fd\u6570\uff0c\u5982<code>dropna()<\/code>\u53bb\u9664\u7f3a\u5931\u503c\u3001<code>astype()<\/code>\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u3001<code>str.strip()<\/code>\u53bb\u6389\u5b57\u7b26\u4e32\u7684\u524d\u540e\u7a7a\u683c\u7b49\u3002\u8fd9\u4e9b\u6b65\u9aa4\u786e\u4fdd\u751f\u6210\u7684CSV\u6587\u4ef6\u66f4\u4e3a\u6574\u6d01\u3001\u6613\u4e8e\u5206\u6790\u3002\u5728\u6e05\u6d17\u6570\u636e\u65f6\uff0c\u5efa\u8bae\u5148\u6253\u5370\u51fa\u6570\u636e\u7684\u6458\u8981\u4fe1\u606f\uff0c\u4ee5\u4fbf\u786e\u8ba4\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u7684\u722c\u866b\u6570\u636e\u5305\u542b\u5d4c\u5957\u7ed3\u6784\uff0c\u5982\u4f55\u5c06\u5176\u5e73\u6574\u5316\u4ee5\u4fbf\u5b58\u50a8\u4e3aCSV\uff1f<\/strong><\/p>\n<p>\u5f53\u722c\u866b\u6293\u53d6\u7684\u6570\u636e\u5305\u542b\u5d4c\u5957\u7ed3\u6784\uff08\u4f8b\u5982\uff0cJSON\u683c\u5f0f\uff09\uff0c\u9700\u8981\u4f7f\u7528<code>json_normalize()<\/code>\u51fd\u6570\u6765\u5c06\u5d4c\u5957\u6570\u636e\u5c55\u5e73\u3002\u8be5\u51fd\u6570\u80fd\u591f\u5c06\u5d4c\u5957\u5b57\u6bb5\u8f6c\u5316\u4e3a\u6241\u5e73\u7ed3\u6784\uff0c\u4fbf\u4e8e\u5b58\u50a8\u4e3aCSV\u6587\u4ef6\u3002\u5b8c\u6210\u6570\u636e\u5c55\u5e73\u540e\uff0c\u7ee7\u7eed\u4f7f\u7528<code>pandas<\/code>\u7684<code>to_csv()<\/code>\u65b9\u6cd5\u8fdb\u884c\u6587\u4ef6\u8f93\u51fa\u3002\u786e\u4fdd\u5728\u5e73\u6574\u5316\u6570\u636e\u65f6\uff0c\u68c0\u67e5\u5b57\u6bb5\u540d\u79f0\u7684\u91cd\u590d\u6027\uff0c\u5e76\u9002\u5f53\u91cd\u547d\u540d\uff0c\u4ee5\u4fdd\u8bc1\u751f\u6210\u7684CSV\u6587\u4ef6\u4e2d\u5b57\u6bb5\u6e05\u6670\u53ef\u8bfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u722c\u866b\u6570\u636e\u751f\u6210CSV\u8868\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u83b7\u53d6\u6570\u636e\u3001\u89e3\u6790\u6570\u636e\u3001\u521b\u5efaCSV\u6587\u4ef6\u3001\u5199\u5165\u6570\u636e\u3002\u4f7f\u7528\u5e93\u5982re [&hellip;]","protected":false},"author":3,"featured_media":1110834,"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\/1110831"}],"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=1110831"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110831\/revisions"}],"predecessor-version":[{"id":1110837,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110831\/revisions\/1110837"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1110834"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1110831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1110831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1110831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}