{"id":1072303,"date":"2025-01-08T11:15:04","date_gmt":"2025-01-08T03:15:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1072303.html"},"modified":"2025-01-08T11:15:07","modified_gmt":"2025-01-08T03:15:07","slug":"python%e7%88%ac%e8%99%ab%e5%a6%82%e4%bd%95%e7%88%ac%e7%9a%84%e5%90%8c%e6%97%b6%e5%ad%98%e6%95%b0%e6%8d%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1072303.html","title":{"rendered":"python\u722c\u866b\u5982\u4f55\u722c\u7684\u540c\u65f6\u5b58\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102546\/cfe4ef06-1635-48dd-8832-31f733bd7496.webp\" alt=\"python\u722c\u866b\u5982\u4f55\u722c\u7684\u540c\u65f6\u5b58\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u722c\u866b\u5728\u722c\u53d6\u6570\u636e\u7684\u540c\u65f6\u5b58\u50a8\u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u5f02\u6b65\u7f16\u7a0b\u3001\u961f\u5217\u7b49\u6280\u672f\u5b9e\u73b0\u3002\u8fd9\u4e9b\u6280\u672f\u80fd\u591f\u63d0\u5347\u722c\u866b\u7684\u6548\u7387\u3001\u907f\u514d\u963b\u585e\u3001\u63d0\u9ad8\u6570\u636e\u5b58\u50a8\u7684\u5b9e\u65f6\u6027\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u961f\u5217\u7ed3\u5408\u591a\u7ebf\u7a0b\u5904\u7406\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u672c\u722c\u866b\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u6709\u4e00\u4e2a\u57fa\u672c\u7684\u722c\u866b\u7ed3\u6784\u3002Python \u7684 <code>requests<\/code> \u548c <code>BeautifulSoup<\/code> \u662f\u5e38\u7528\u7684\u722c\u866b\u5e93\uff0c<code>pandas<\/code> \u5219\u7528\u4e8e\u6570\u636e\u5b58\u50a8\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u57fa\u672c\u7684\u722c\u866b\u6846\u67b6\u6765\u722c\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import pandas as pd<\/p>\n<p>def fetch_url(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        return response.text<\/p>\n<p>    return None<\/p>\n<p>def parse_html(html):<\/p>\n<p>    soup = BeautifulSoup(html, &#39;html.parser&#39;)<\/p>\n<p>    data = []<\/p>\n<p>    for item in soup.select(&#39;selector&#39;):  # replace &#39;selector&#39; with the actual selector<\/p>\n<p>        data.append(item.get_text())<\/p>\n<p>    return data<\/p>\n<p>def save_data(data):<\/p>\n<p>    df = pd.DataFrame(data, columns=[&#39;column1&#39;])  # replace &#39;column1&#39; with actual column name<\/p>\n<p>    df.to_csv(&#39;data.csv&#39;, mode=&#39;a&#39;, header=False, index=False)<\/p>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>html = fetch_url(url)<\/p>\n<p>if html:<\/p>\n<p>    data = parse_html(html)<\/p>\n<p>    save_data(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u961f\u5217\u4e0e\u591a\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0\u8fb9\u722c\u53d6\u8fb9\u5b58\u50a8\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>queue.Queue<\/code> \u6765\u7ba1\u7406\u4efb\u52a1\uff0c\u5e76\u7ed3\u5408 <code>threading.Thread<\/code> \u6765\u5b9e\u73b0\u591a\u7ebf\u7a0b\u5904\u7406\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u722c\u53d6\u6570\u636e\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u5b58\u50a8\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import queue<\/p>\n<p>import time<\/p>\n<p>class CrawlerThread(threading.Thread):<\/p>\n<p>    def __init__(self, url_queue, data_queue):<\/p>\n<p>        threading.Thread.__init__(self)<\/p>\n<p>        self.url_queue = url_queue<\/p>\n<p>        self.data_queue = data_queue<\/p>\n<p>    def run(self):<\/p>\n<p>        while True:<\/p>\n<p>            url = self.url_queue.get()<\/p>\n<p>            if url is None:<\/p>\n<p>                break<\/p>\n<p>            html = fetch_url(url)<\/p>\n<p>            if html:<\/p>\n<p>                data = parse_html(html)<\/p>\n<p>                self.data_queue.put(data)<\/p>\n<p>            self.url_queue.task_done()<\/p>\n<p>class SaverThread(threading.Thread):<\/p>\n<p>    def __init__(self, data_queue):<\/p>\n<p>        threading.Thread.__init__(self)<\/p>\n<p>        self.data_queue = data_queue<\/p>\n<p>    def run(self):<\/p>\n<p>        while True:<\/p>\n<p>            data = self.data_queue.get()<\/p>\n<p>            if data is None:<\/p>\n<p>                break<\/p>\n<p>            save_data(data)<\/p>\n<p>            self.data_queue.task_done()<\/p>\n<p>url_queue = queue.Queue()<\/p>\n<p>data_queue = queue.Queue()<\/p>\n<p>urls = [&#39;http:\/\/example.com\/page1&#39;, &#39;http:\/\/example.com\/page2&#39;]  # Add your URLs here<\/p>\n<p>for url in urls:<\/p>\n<p>    url_queue.put(url)<\/p>\n<p>crawler_threads = []<\/p>\n<p>for _ in range(4):  # Number of crawler threads<\/p>\n<p>    t = CrawlerThread(url_queue, data_queue)<\/p>\n<p>    t.start()<\/p>\n<p>    crawler_threads.append(t)<\/p>\n<p>saver_thread = SaverThread(data_queue)<\/p>\n<p>saver_thread.start()<\/p>\n<p>url_queue.join()<\/p>\n<p>data_queue.join()<\/p>\n<p>for _ in range(4):<\/p>\n<p>    url_queue.put(None)<\/p>\n<p>for t in crawler_threads:<\/p>\n<p>    t.join()<\/p>\n<p>data_queue.put(None)<\/p>\n<p>saver_thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>Python \u7684 <code>asyncio<\/code> \u548c <code><a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ohttp<\/code> \u53ef\u4ee5\u7528\u4e8e\u5f02\u6b65\u7f16\u7a0b\uff0c\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u722c\u866b\u6548\u7387\u3002\u5f02\u6b65\u7f16\u7a0b\u80fd\u591f\u5728\u7b49\u5f85 I\/O \u64cd\u4f5c\u65f6\u5207\u6362\u4efb\u52a1\uff0c\u4ece\u800c\u907f\u514d\u963b\u585e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>import aiohttp<\/p>\n<p>import pandas as pd<\/p>\n<p>async def fetch_url(session, url):<\/p>\n<p>    async with session.get(url) as response:<\/p>\n<p>        return await response.text()<\/p>\n<p>async def parse_and_save(data_queue):<\/p>\n<p>    while True:<\/p>\n<p>        data = await data_queue.get()<\/p>\n<p>        if data is None:<\/p>\n<p>            break<\/p>\n<p>        save_data(data)<\/p>\n<p>        data_queue.task_done()<\/p>\n<p>async def main(urls):<\/p>\n<p>    data_queue = asyncio.Queue()<\/p>\n<p>    async with aiohttp.ClientSession() as session:<\/p>\n<p>        tasks = []<\/p>\n<p>        for url in urls:<\/p>\n<p>            tasks.append(fetch_url(session, url))<\/p>\n<p>        htmls = await asyncio.gather(*tasks)<\/p>\n<p>        for html in htmls:<\/p>\n<p>            data = parse_html(html)<\/p>\n<p>            await data_queue.put(data)<\/p>\n<p>    await parse_and_save(data_queue)<\/p>\n<p>urls = [&#39;http:\/\/example.com\/page1&#39;, &#39;http:\/\/example.com\/page2&#39;]  # Add your URLs here<\/p>\n<p>asyncio.run(main(urls))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6570\u636e\u5b58\u50a8\u4e0e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u722c\u866b\u8fc7\u7a0b\u4e2d\uff0c\u6570\u636e\u5b58\u50a8\u662f\u4e00\u4e2a\u91cd\u8981\u73af\u8282\u3002\u9664\u4e86\u5b58\u50a8\u5230 CSV \u6587\u4ef6\u5916\uff0c\u8fd8\u53ef\u4ee5\u5b58\u50a8\u5230\u6570\u636e\u5e93\uff08\u5982 SQLite\u3001MySQL\u3001MongoDB\uff09\u4e2d\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b58\u50a8\u5230SQLite<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def save_data_to_db(data):<\/p>\n<p>    conn = sqlite3.connect(&#39;data.db&#39;)<\/p>\n<p>    c = conn.cursor()<\/p>\n<p>    c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS data (column1 TEXT)&#39;&#39;&#39;)<\/p>\n<p>    c.executemany(&#39;INSERT INTO data VALUES (?)&#39;, data)<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b58\u50a8\u5230MySQL<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>def save_data_to_mysql(data):<\/p>\n<p>    conn = pymysql.connect(host=&#39;localhost&#39;, user=&#39;root&#39;, password=&#39;password&#39;, db=&#39;database&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.executemany(&#39;INSERT INTO data (column1) VALUES (%s)&#39;, data)<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5b58\u50a8\u5230MongoDB<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from pymongo import MongoClient<\/p>\n<p>def save_data_to_mongodb(data):<\/p>\n<p>    client = MongoClient(&#39;localhost&#39;, 27017)<\/p>\n<p>    db = client[&#39;database&#39;]<\/p>\n<p>    collection = db[&#39;data&#39;]<\/p>\n<p>    collection.insert_many([{&#39;column1&#39;: item} for item in data])<\/p>\n<p>    client.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6570\u636e\u6e05\u6d17\u4e0e\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u722c\u53d6\u548c\u5b58\u50a8\u5b8c\u6210\u540e\uff0c\u6570\u636e\u6e05\u6d17\u4e0e\u5206\u6790\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u6b65\u9aa4\u3002\u901a\u8fc7\u6570\u636e\u6e05\u6d17\uff0c\u53bb\u9664\u566a\u97f3\u548c\u9519\u8bef\u6570\u636e\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u8d28\u91cf\uff1b\u800c\u6570\u636e\u5206\u6790\u5219\u80fd\u4ece\u6570\u636e\u4e2d\u63d0\u53d6\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def clean_data(file_path):<\/p>\n<p>    df = pd.read_csv(file_path)<\/p>\n<p>    df.dropna(inplace=True)  # Remove missing values<\/p>\n<p>    df.drop_duplicates(inplace=True)  # Remove duplicates<\/p>\n<p>    df.to_csv(&#39;cleaned_data.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def analyze_data(file_path):<\/p>\n<p>    df = pd.read_csv(file_path)<\/p>\n<p>    summary = df.describe()  # Get summary statistics<\/p>\n<p>    print(summary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python \u722c\u866b\u5728\u722c\u53d6\u6570\u636e\u7684\u540c\u65f6\u5b58\u50a8\u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u5f02\u6b65\u7f16\u7a0b\u3001\u961f\u5217\u7b49\u6280\u672f\u5b9e\u73b0\u3002\u8fd9\u4e9b\u6280\u672f\u80fd\u591f\u63d0\u5347\u722c\u866b\u7684\u6548\u7387\u3001\u907f\u514d\u963b\u585e\u3001\u63d0\u9ad8\u6570\u636e\u5b58\u50a8\u7684\u5b9e\u65f6\u6027\u3002\u901a\u8fc7\u5408\u7406\u7684\u722c\u866b\u8bbe\u8ba1\u548c\u6570\u636e\u5b58\u50a8\u7b56\u7565\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6293\u53d6\u548c\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u4e3a\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u548c\u5e94\u7528\u63d0\u4f9b\u53ef\u9760\u7684\u6570\u636e\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u722c\u866b\u4e2d\u5b9e\u73b0\u6570\u636e\u5b58\u50a8\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u7f51\u7edc\u722c\u866b\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b58\u50a8\u6293\u53d6\u5230\u7684\u6570\u636e\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528CSV\u6587\u4ef6\u3001JSON\u6587\u4ef6\u6216\u6570\u636e\u5e93\uff08\u5982SQLite\u3001MySQL\u7b49\uff09\u3002\u9009\u62e9\u5b58\u50a8\u65b9\u5f0f\u65f6\uff0c\u9700\u8981\u8003\u8651\u6570\u636e\u7684\u7ed3\u6784\u3001\u540e\u7eed\u5904\u7406\u7684\u65b9\u4fbf\u6027\u4ee5\u53ca\u6570\u636e\u91cf\u7684\u5927\u5c0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u5b58\u50a8\u4e3aCSV\u683c\u5f0f\uff0c\u800c\u4f7f\u7528SQLAlchemy\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u4e0e\u6570\u636e\u5e93\u7684\u4ea4\u4e92\u3002<\/p>\n<p><strong>\u5728\u722c\u866b\u8fc7\u7a0b\u4e2d\u5982\u4f55\u5904\u7406\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u6570\u636e\u5b58\u50a8\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u591a\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u6bd4\u5982\u6587\u4ef6\u5199\u5165\u6743\u9650\u4e0d\u8db3\u6216\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u4e9b\u95ee\u9898\uff0c\u53ef\u4ee5\u5728\u5b58\u50a8\u6570\u636e\u65f6\u52a0\u5165\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u6bd4\u5982\u4f7f\u7528try-except\u8bed\u53e5\u3002\u8fd9\u4e0d\u4ec5\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff0c\u8fd8\u53ef\u4ee5\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\uff0c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\u548c\u4fee\u590d\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u8fb9\u722c\u53d6\u8fb9\u5b58\u50a8\u6570\u636e\u800c\u4e0d\u5f71\u54cd\u722c\u866b\u6027\u80fd\uff1f<\/strong><br \/>\u7edd\u5bf9\u53ef\u4ee5\u3002\u4e3a\u4e86\u4fdd\u6301\u722c\u866b\u7684\u9ad8\u6548\u6027\uff0c\u53ef\u4ee5\u91c7\u7528\u5f02\u6b65\u7f16\u7a0b\u6216\u591a\u7ebf\u7a0b\u6280\u672f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>asyncio<\/code>\u5e93\u7ed3\u5408<code>aiohttp<\/code>\u8fdb\u884c\u5f02\u6b65\u722c\u53d6\uff0c\u540c\u65f6\u5c06\u6570\u636e\u5b58\u50a8\u64cd\u4f5c\u653e\u5728\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u8fdb\u884c\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u5730\u51cf\u5c11\u722c\u53d6\u4e0e\u5b58\u50a8\u4e4b\u95f4\u7684\u5ef6\u8fdf\uff0c\u63d0\u9ad8\u6574\u4f53\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u722c\u866b\u5728\u722c\u53d6\u6570\u636e\u7684\u540c\u65f6\u5b58\u50a8\u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u3001\u5f02\u6b65\u7f16\u7a0b\u3001\u961f\u5217\u7b49\u6280\u672f\u5b9e\u73b0\u3002\u8fd9\u4e9b\u6280\u672f\u80fd\u591f\u63d0\u5347\u722c [&hellip;]","protected":false},"author":3,"featured_media":1072314,"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\/1072303"}],"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=1072303"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072303\/revisions"}],"predecessor-version":[{"id":1072320,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072303\/revisions\/1072320"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1072314"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1072303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1072303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1072303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}