爬蟲運行后,將獲取到的數據保存到本地或數據庫中是常見的需求。Python 提供了多種方式來保存數據,包括保存為文本文件、CSV 文件、JSON 文件,甚至存儲到數據庫中。以下是幾種常見的數據保存方法,以及對應的代碼示例。
1.?保存為文本文件
將爬取到的數據保存為簡單的文本文件是最基礎的方式,適合存儲少量數據。
def save_to_text(data, filename="output.txt"):with open(filename, "w", encoding="utf-8") as file:for item in data:file.write(str(item) + "\n")print(f"數據已保存到 {filename}")
示例:
data = ["商品1", "商品2", "商品3"]
save_to_text(data)
2.?保存為 CSV 文件
CSV 文件是表格數據的常用格式,適合存儲結構化數據,例如商品詳情。
import csvdef save_to_csv(data, filename="output.csv"):keys = data[0].keys() # 假設數據是字典列表with open(filename, "w", newline="", encoding="utf-8") as file:writer = csv.DictWriter(file, fieldnames=keys)writer.writeheader()writer.writerows(data)print(f"數據已保存到 {filename}")
示例:
data = [{"name": "商品1", "price": "100元", "description": "這是商品1"},{"name": "商品2", "price": "200元", "description": "這是商品2"}
]
save_to_csv(data)
3.?保存為 JSON 文件
JSON 是一種輕量級的數據交換格式,適合存儲復雜的數據結構,例如嵌套字典。
import jsondef save_to_json(data, filename="output.json"):with open(filename, "w", encoding="utf-8") as file:json.dump(data, file, ensure_ascii=False, indent=4)print(f"數據已保存到 {filename}")
示例:
data = [{"name": "商品1", "price": "100元", "description": "這是商品1"},{"name": "商品2", "price": "200元", "description": "這是商品2"}
]
save_to_json(data)
4.?保存到數據庫
對于需要長期存儲和頻繁查詢的數據,保存到數據庫是更好的選擇。以下是保存到 SQLite 數據庫的示例:
import sqlite3def save_to_database(data, db_name="database.db", table_name="products"):conn = sqlite3.connect(db_name)cursor = conn.cursor()# 創建表(如果不存在)cursor.execute(f"""CREATE TABLE IF NOT EXISTS {table_name} (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,price TEXT,description TEXT)""")# 插入數據for item in data:cursor.execute(f"""INSERT INTO {table_name} (name, price, description)VALUES (?, ?, ?)""", (item["name"], item["price"], item["description"]))conn.commit()conn.close()print(f"數據已保存到數據庫 {db_name}")
示例:
data = [{"name": "商品1", "price": "100元", "description": "這是商品1"},{"name": "商品2", "price": "200元", "description": "這是商品2"}
]
save_to_database(data)
5.?保存到 Excel 文件
如果需要將數據保存為 Excel 文件,可以使用 pandas
庫:
import pandas as pddef save_to_excel(data, filename="output.xlsx"):df = pd.DataFrame(data)df.to_excel(filename, index=False)print(f"數據已保存到 {filename}")
示例:
data = [{"name": "商品1", "price": "100元", "description": "這是商品1"},{"name": "商品2", "price": "200元", "description": "這是商品2"}
]
save_to_excel(data)
6.?選擇合適的保存方式
-
文本文件:適合簡單的日志或少量數據。
-
CSV 文件:適合結構化數據,便于后續分析。
-
JSON 文件:適合復雜數據結構,便于數據交換。
-
數據庫:適合大規模數據存儲和復雜查詢。
-
Excel 文件:適合需要在 Excel 中進一步處理的數據。
7.?示例:整合到爬蟲程序中
以下是一個完整的爬蟲程序示例,將爬取到的數據保存為 CSV 文件:
import requests
from bs4 import BeautifulSoupdef get_html(url):headers = {"User-Agent": "Mozilla/5.0"}response = requests.get(url, headers=headers)return response.text if response.status_code == 200 else Nonedef parse_html(html):soup = BeautifulSoup(html, "lxml")products = []items = soup.select(".product-item")for item in items:product = {"name": item.select_one(".product-name").text.strip(),"price": item.select_one(".product-price").text.strip(),"description": item.select_one(".product-description").text.strip()}products.append(product)return productsdef save_to_csv(data, filename="output.csv"):import csvkeys = data[0].keys()with open(filename, "w", newline="", encoding="utf-8") as file:writer = csv.DictWriter(file, fieldnames=keys)writer.writeheader()writer.writerows(data)print(f"數據已保存到 {filename}")def main():url = "https://www.example.com/vip-products"html = get_html(url)if html:products = parse_html(html)if products:save_to_csv(products)else:print("未找到商品信息")else:print("無法獲取頁面內容")if __name__ == "__main__":main()
通過以上方法,你可以根據需求選擇合適的方式保存爬蟲運行后的數據。無論是簡單的文本文件,還是復雜的數據庫存儲,Python 都提供了強大的支持。希望這些示例能幫助你更好地管理和利用爬取到的數據!