{"id":1178875,"date":"2025-01-15T18:16:18","date_gmt":"2025-01-15T10:16:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1178875.html"},"modified":"2025-01-15T18:16:20","modified_gmt":"2025-01-15T10:16:20","slug":"python%e7%88%ac%e8%99%ab%e5%a6%82%e4%bd%95%e5%86%99%e5%85%a5%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1178875.html","title":{"rendered":"python\u722c\u866b\u5982\u4f55\u5199\u5165\u6587\u6863"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113424\/fd5ba64a-cd89-4e3c-a872-67030f3e5767.webp\" alt=\"python\u722c\u866b\u5982\u4f55\u5199\u5165\u6587\u6863\" \/><\/p>\n<p><p> <strong>Python\u722c\u866b\u5199\u5165\u6587\u6863\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\uff1a\u5c06\u6570\u636e\u5199\u5165\u6587\u672c\u6587\u4ef6\u3001\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u3001\u5c06\u6570\u636e\u5199\u5165Excel\u6587\u4ef6\u3001\u5c06\u6570\u636e\u5199\u5165\u6570\u636e\u5e93\u7b49\u3002\u6700\u5e38\u89c1\u7684\u662f\u5c06\u6570\u636e\u5199\u5165\u6587\u672c\u6587\u4ef6\u548cCSV\u6587\u4ef6\u3002<\/strong>\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Python\u722c\u866b\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6587\u672c\u6587\u4ef6\u5199\u5165<\/h3>\n<\/p>\n<p><p>\u6587\u672c\u6587\u4ef6\u662f\u6700\u7b80\u5355\u7684\u4e00\u79cd\u6587\u4ef6\u683c\u5f0f\uff0c\u9002\u7528\u4e8e\u5b58\u50a8\u7b80\u5355\u7684\u3001\u7ed3\u6784\u5316\u7684\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e<code>open()<\/code>\u51fd\u6570\u521b\u5efa\u548c\u5199\u5165\u6587\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u7840\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u53d1\u9001HTTP\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/example.com&#39;)<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u6570\u636e<\/strong><\/h2>\n<p>data = soup.find_all(&#39;p&#39;)<\/p>\n<h2><strong>\u5199\u5165\u6587\u672c\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    for item in data:<\/p>\n<p>        file.write(item.get_text() + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5206\u6bb5\u5199\u5165<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728\u722c\u53d6\u7684\u8fc7\u7a0b\u4e2d\u5206\u6bb5\u5199\u5165\u6587\u4ef6\uff0c\u4ee5\u9632\u6b62\u6570\u636e\u4e22\u5931\u6216\u5185\u5b58\u8017\u5c3d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;a&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    for item in data:<\/p>\n<p>        file.write(item.get_text() + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001CSV\u6587\u4ef6\u5199\u5165<\/h3>\n<\/p>\n<p><p>CSV\uff08Comma Separated Values\uff09\u6587\u4ef6\u662f\u4e00\u79cd\u7b80\u5355\u7684\u8868\u683c\u6587\u4ef6\u683c\u5f0f\uff0c\u9002\u7528\u4e8e\u5b58\u50a8\u8868\u683c\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>csv<\/code>\u6a21\u5757\u6765\u521b\u5efa\u548c\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u7840\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u5199\u5165CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as csvfile:<\/p>\n<p>    writer = csv.writer(csvfile)<\/p>\n<p>    # \u5199\u5165\u8868\u5934<\/p>\n<p>    writer.writerow([&#39;Column1&#39;, &#39;Column2&#39;, &#39;Column3&#39;])<\/p>\n<p>    # \u5199\u5165\u6570\u636e\u884c<\/p>\n<p>    writer.writerow([&#39;Data1&#39;, &#39;Data2&#39;, &#39;Data3&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4ece\u722c\u866b\u5199\u5165CSV<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import csv<\/p>\n<h2><strong>\u53d1\u9001HTTP\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/example.com&#39;)<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u6570\u636e<\/strong><\/h2>\n<p>data = soup.find_all(&#39;p&#39;)<\/p>\n<h2><strong>\u5199\u5165CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as csvfile:<\/p>\n<p>    writer = csv.writer(csvfile)<\/p>\n<p>    # \u5199\u5165\u8868\u5934<\/p>\n<p>    writer.writerow([&#39;Paragraph&#39;])<\/p>\n<p>    for item in data:<\/p>\n<p>        writer.writerow([item.get_text()])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001Excel\u6587\u4ef6\u5199\u5165<\/h3>\n<\/p>\n<p><p>Excel\u6587\u4ef6\u683c\u5f0f\u590d\u6742\uff0c\u4f46\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u590d\u6742\u8868\u683c\u6570\u636e\u7684\u60c5\u51b5\uff0c\u4f7f\u7528Excel\u6587\u4ef6\u683c\u5f0f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u53ef\u4ee5\u4f7f\u7528<code>openpyxl<\/code>\u6a21\u5757\u6765\u521b\u5efa\u548c\u5199\u5165Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u7840\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import Workbook<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7c3f<\/strong><\/h2>\n<p>wb = Workbook()<\/p>\n<h2><strong>\u83b7\u53d6\u6d3b\u52a8\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws = wb.active<\/p>\n<h2><strong>\u5199\u5165\u8868\u5934<\/strong><\/h2>\n<p>ws.append([&#39;Column1&#39;, &#39;Column2&#39;, &#39;Column3&#39;])<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e\u884c<\/strong><\/h2>\n<p>ws.append([&#39;Data1&#39;, &#39;Data2&#39;, &#39;Data3&#39;])<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>wb.save(&#39;output.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4ece\u722c\u866b\u5199\u5165Excel<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>from openpyxl import Workbook<\/p>\n<h2><strong>\u53d1\u9001HTTP\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/example.com&#39;)<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u6570\u636e<\/strong><\/h2>\n<p>data = soup.find_all(&#39;p&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7c3f<\/strong><\/h2>\n<p>wb = Workbook()<\/p>\n<h2><strong>\u83b7\u53d6\u6d3b\u52a8\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws = wb.active<\/p>\n<h2><strong>\u5199\u5165\u8868\u5934<\/strong><\/h2>\n<p>ws.append([&#39;Paragraph&#39;])<\/p>\n<p>for item in data:<\/p>\n<p>    ws.append([item.get_text()])<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>wb.save(&#39;output.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6570\u636e\u5e93\u5199\u5165<\/h3>\n<\/p>\n<p><p>\u5c06\u6570\u636e\u5199\u5165\u6570\u636e\u5e93\u662f\u4e00\u79cd\u66f4\u52a0\u7ed3\u6784\u5316\u548c\u6301\u4e45\u5316\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9891\u7e41\u67e5\u8be2\u548c\u66f4\u65b0\u7684\u6570\u636e\u3002\u5e38\u7528\u7684\u6570\u636e\u5e93\u5305\u62ecSQLite\u3001MySQL\u3001PostgreSQL\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001SQLite\u6570\u636e\u5e93\u5199\u5165<\/h4>\n<\/p>\n<p><p>SQLite\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5d4c\u5165\u5f0f\u6570\u636e\u5e93\uff0c\u9002\u5408\u5c0f\u578b\u9879\u76ee\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>sqlite3<\/code>\u6a21\u5757\u6765\u521b\u5efa\u548c\u5199\u5165SQLite\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>c = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS my_table<\/p>\n<p>             (id INTEGER PRIMARY KEY, column1 TEXT, column2 TEXT)&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>c.execute(&quot;INSERT INTO my_table (column1, column2) VALUES (&#39;Data1&#39;, &#39;Data2&#39;)&quot;)<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4ece\u722c\u866b\u5199\u5165SQLite<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import sqlite3<\/p>\n<h2><strong>\u53d1\u9001HTTP\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/example.com&#39;)<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u6570\u636e<\/strong><\/h2>\n<p>data = soup.find_all(&#39;p&#39;)<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>c = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS paragraphs<\/p>\n<p>             (id INTEGER PRIMARY KEY, content TEXT)&#39;&#39;&#39;)<\/p>\n<p>for item in data:<\/p>\n<p>    # \u63d2\u5165\u6570\u636e<\/p>\n<p>    c.execute(&quot;INSERT INTO paragraphs (content) VALUES (?)&quot;, (item.get_text(),))<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001MySQL\u6570\u636e\u5e93\u5199\u5165<\/h3>\n<\/p>\n<p><p>MySQL\u662f\u4e00\u79cd\u6d41\u884c\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u9002\u7528\u4e8e\u5927\u578b\u9879\u76ee\u3002\u53ef\u4ee5\u4f7f\u7528<code>mysql-connector-python<\/code>\u5e93\u6765\u8fde\u63a5\u548c\u64cd\u4f5cMySQL\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5MySQL\u8fde\u63a5\u5668<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mysql-connector-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u57fa\u7840\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<h2><strong>\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = mysql.connector.connect(<\/p>\n<p>    host=&quot;localhost&quot;,<\/p>\n<p>    user=&quot;yourusername&quot;,<\/p>\n<p>    password=&quot;yourpassword&quot;,<\/p>\n<p>    database=&quot;yourdatabase&quot;<\/p>\n<p>)<\/p>\n<p>c = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS my_table<\/p>\n<p>             (id INT AUTO_INCREMENT PRIMARY KEY, column1 VARCHAR(255), column2 VARCHAR(255))&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>c.execute(&quot;INSERT INTO my_table (column1, column2) VALUES (%s, %s)&quot;, (&#39;Data1&#39;, &#39;Data2&#39;))<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4ece\u722c\u866b\u5199\u5165MySQL<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import mysql.connector<\/p>\n<h2><strong>\u53d1\u9001HTTP\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/example.com&#39;)<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u6570\u636e<\/strong><\/h2>\n<p>data = soup.find_all(&#39;p&#39;)<\/p>\n<h2><strong>\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = mysql.connector.connect(<\/p>\n<p>    host=&quot;localhost&quot;,<\/p>\n<p>    user=&quot;yourusername&quot;,<\/p>\n<p>    password=&quot;yourpassword&quot;,<\/p>\n<p>    database=&quot;yourdatabase&quot;<\/p>\n<p>)<\/p>\n<p>c = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS paragraphs<\/p>\n<p>             (id INT AUTO_INCREMENT PRIMARY KEY, content TEXT)&#39;&#39;&#39;)<\/p>\n<p>for item in data:<\/p>\n<p>    # \u63d2\u5165\u6570\u636e<\/p>\n<p>    c.execute(&quot;INSERT INTO paragraphs (content) VALUES (%s)&quot;, (item.get_text(),))<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001PostgreSQL\u6570\u636e\u5e93\u5199\u5165<\/h3>\n<\/p>\n<p><p>PostgreSQL\u662f\u4e00\u79cd\u529f\u80fd\u5f3a\u5927\u7684\u5f00\u6e90\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7cfb\u7edf\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u9700\u6c42\u3002\u53ef\u4ee5\u4f7f\u7528<code>psycopg2<\/code>\u5e93\u6765\u8fde\u63a5\u548c\u64cd\u4f5cPostgreSQL\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5PostgreSQL\u8fde\u63a5\u5668<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install psycopg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u57fa\u7840\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<h2><strong>\u8fde\u63a5\u5230PostgreSQL\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = psycopg2.connect(<\/p>\n<p>    dbname=&quot;yourdatabase&quot;,<\/p>\n<p>    user=&quot;yourusername&quot;,<\/p>\n<p>    password=&quot;yourpassword&quot;,<\/p>\n<p>    host=&quot;localhost&quot;<\/p>\n<p>)<\/p>\n<p>c = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS my_table<\/p>\n<p>             (id SERIAL PRIMARY KEY, column1 VARCHAR(255), column2 VARCHAR(255))&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>c.execute(&quot;INSERT INTO my_table (column1, column2) VALUES (%s, %s)&quot;, (&#39;Data1&#39;, &#39;Data2&#39;))<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4ece\u722c\u866b\u5199\u5165PostgreSQL<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import psycopg2<\/p>\n<h2><strong>\u53d1\u9001HTTP\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/example.com&#39;)<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u6570\u636e<\/strong><\/h2>\n<p>data = soup.find_all(&#39;p&#39;)<\/p>\n<h2><strong>\u8fde\u63a5\u5230PostgreSQL\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = psycopg2.connect(<\/p>\n<p>    dbname=&quot;yourdatabase&quot;,<\/p>\n<p>    user=&quot;yourusername&quot;,<\/p>\n<p>    password=&quot;yourpassword&quot;,<\/p>\n<p>    host=&quot;localhost&quot;<\/p>\n<p>)<\/p>\n<p>c = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS paragraphs<\/p>\n<p>             (id SERIAL PRIMARY KEY, content TEXT)&#39;&#39;&#39;)<\/p>\n<p>for item in data:<\/p>\n<p>    # \u63d2\u5165\u6570\u636e<\/p>\n<p>    c.execute(&quot;INSERT INTO paragraphs (content) VALUES (%s)&quot;, (item.get_text(),))<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Python\u722c\u866b\u5199\u5165\u6587\u6863\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u9009\u62e9\uff0c\u5305\u62ec\u6587\u672c\u6587\u4ef6\u3001CSV\u6587\u4ef6\u3001Excel\u6587\u4ef6\u548c\u6570\u636e\u5e93\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u590d\u6742\u5ea6\u3001\u5b58\u50a8\u9700\u6c42\u4ee5\u53ca\u67e5\u8be2\u9700\u6c42\u7b49\u56e0\u7d20\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u7ba1\u7406\u548c\u5229\u7528\u722c\u53d6\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u722c\u866b\u4e2d\u9009\u62e9\u5408\u9002\u7684\u6587\u6863\u683c\u5f0f\u8fdb\u884c\u6570\u636e\u5b58\u50a8\uff1f<\/strong><br \/>\u5728Python\u722c\u866b\u4e2d\uff0c\u5e38\u89c1\u7684\u6587\u6863\u683c\u5f0f\u5305\u62ecTXT\u3001CSV\u3001JSON\u548cExcel\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\u4e3b\u8981\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u7ed3\u6784\u548c\u540e\u7eed\u4f7f\u7528\u7684\u9700\u6c42\u3002\u5982\u679c\u6570\u636e\u662f\u8868\u683c\u5f62\u5f0f\uff0cCSV\u6216Excel\u683c\u5f0f\u4f1a\u66f4\u4e3a\u5408\u9002\uff1b\u5982\u679c\u6570\u636e\u7ed3\u6784\u8f83\u4e3a\u590d\u6742\uff0c\u4f7f\u7528JSON\u683c\u5f0f\u5219\u66f4\u4e3a\u7075\u6d3b\u3002TXT\u6587\u4ef6\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6587\u672c\u6570\u636e\u5b58\u50a8\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u722c\u866b\u5199\u5165\u6587\u6863\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002\u6b64\u5916\uff0cPandas\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u6570\u636e\u5e76\u5c06\u5176\u5199\u5165CSV\u6216Excel\u6587\u4ef6\uff0c\u4f7f\u7528<code>json<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u5c06\u6570\u636e\u5b58\u50a8\u4e3aJSON\u683c\u5f0f\u3002\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u4f7f\u7528<code>csv<\/code>\u5e93\u4e5f\u80fd\u63d0\u4f9b\u9ad8\u6548\u7684\u5b58\u50a8\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u5728\u722c\u866b\u5199\u5165\u6587\u6863\u65f6\u6570\u636e\u4e0d\u4e22\u5931\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u6570\u636e\u5728\u5199\u5165\u8fc7\u7a0b\u4e2d\u4e0d\u4e22\u5931\uff0c\u5efa\u8bae\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u6f5c\u5728\u7684\u9519\u8bef\u3002\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\u3002\u540c\u65f6\uff0c\u5b9a\u671f\u4fdd\u5b58\u6570\u636e\u4ee5\u53ca\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08<code>with<\/code>\u8bed\u53e5\uff09\u6765\u5904\u7406\u6587\u4ef6\u64cd\u4f5c\uff0c\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u5199\u5165\u5b8c\u6210\u540e\u5f97\u5230\u6b63\u786e\u5173\u95ed\uff0c\u4ece\u800c\u907f\u514d\u6570\u636e\u4e22\u5931\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u722c\u866b\u5199\u5165\u6587\u6863\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\uff1a\u5c06\u6570\u636e\u5199\u5165\u6587\u672c\u6587\u4ef6\u3001\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u3001\u5c06\u6570\u636e\u5199\u5165Excel\u6587\u4ef6 [&hellip;]","protected":false},"author":3,"featured_media":1178883,"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\/1178875"}],"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=1178875"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178875\/revisions"}],"predecessor-version":[{"id":1178884,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178875\/revisions\/1178884"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1178883"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1178875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1178875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1178875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}