{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "23157c89",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: pandas in c:\\programdata\\anaconda3\\lib\\site-packages (1.4.2)\n",
      "Requirement already satisfied: pymysql in c:\\programdata\\anaconda3\\lib\\site-packages (1.1.2)\n",
      "Requirement already satisfied: sqlalchemy in c:\\programdata\\anaconda3\\lib\\site-packages (1.4.32)\n",
      "Requirement already satisfied: pytz>=2020.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from pandas) (2021.3)\n",
      "Requirement already satisfied: numpy>=1.18.5 in c:\\programdata\\anaconda3\\lib\\site-packages (from pandas) (1.21.5)\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from pandas) (2.8.2)\n",
      "Requirement already satisfied: greenlet!=0.4.17 in c:\\programdata\\anaconda3\\lib\\site-packages (from sqlalchemy) (1.1.1)\n",
      "Requirement already satisfied: six>=1.5 in c:\\programdata\\anaconda3\\lib\\site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install pandas pymysql sqlalchemy -i https://pypi.tuna.tsinghua.edu.cn/simple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "67b87f14",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "✅ MySQL数据库连接成功！字符集已修复支持中文\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sqlalchemy import create_engine\n",
    "\n",
    "# --------------------------\n",
    "# 你的MySQL配置（不用改，保持你自己的）\n",
    "# --------------------------\n",
    "MYSQL_USER = \"root\"       \n",
    "MYSQL_PASSWORD = \"123456\" \n",
    "MYSQL_HOST = \"localhost\"  \n",
    "MYSQL_PORT = \"3306\"       \n",
    "MYSQL_DB = \"test_db\"    \n",
    "\n",
    "# --------------------------\n",
    "# ✅ 修复：强制指定中文字符集 utf8mb4（关键代码）\n",
    "# --------------------------\n",
    "engine = create_engine(\n",
    "    f\"mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DB}?charset=utf8mb4\",\n",
    "    # 追加字符集初始化，彻底解决中文乱码/写入失败\n",
    "    connect_args={\"init_command\": \"SET NAMES utf8mb4\"}\n",
    ")\n",
    "\n",
    "# 测试连接\n",
    "try:\n",
    "    with engine.connect():\n",
    "        print(\"✅ MySQL数据库连接成功！字符集已修复支持中文\")\n",
    "except Exception as e:\n",
    "    print(f\"❌ 连接失败：{e}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7f36e3ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    课程编号        课程名称  学分 授课教师         上课时间\n",
      "0  CS001  Python数据分析   4  张老师  周一/周三 第1-2节\n",
      "1  CS002       数据库原理   3  李老师  周二/周四 第3-4节\n",
      "2  MA001        高等数学   5  王老师  周一/周五 第5-6节\n",
      "3  EN001        大学英语   2  刘老师     周三 第7-8节\n",
      "✅ 任务3.1完成：手动数据已写入MySQL表 course_info_manual\n"
     ]
    }
   ],
   "source": [
    "# 1. 手动构造课程数据（示例）\n",
    "# --------------------------\n",
    "data = {\n",
    "    \"课程编号\": [\"CS001\", \"CS002\", \"MA001\", \"EN001\"],\n",
    "    \"课程名称\": [\"Python数据分析\", \"数据库原理\", \"高等数学\", \"大学英语\"],\n",
    "    \"学分\": [4, 3, 5, 2],\n",
    "    \"授课教师\": [\"张老师\", \"李老师\", \"王老师\", \"刘老师\"],\n",
    "    \"上课时间\": [\"周一/周三 第1-2节\", \"周二/周四 第3-4节\", \"周一/周五 第5-6节\", \"周三 第7-8节\"]\n",
    "}\n",
    "\n",
    "# 转为DataFrame\n",
    "df_manual = pd.DataFrame(data)\n",
    "print(df_manual)\n",
    "# --------------------------\n",
    "# 2. 写入MySQL数据库\n",
    "# --------------------------\n",
    "# 参数说明：\n",
    "# name：要写入的表名（如果不存在会自动创建）\n",
    "# con：数据库连接引擎\n",
    "# if_exists：处理表已存在的情况\n",
    "#   - \"replace\"：删除原表，重建新表写入\n",
    "#   - \"append\"：在原表末尾追加数据\n",
    "#   - \"fail\"：如果表存在就报错\n",
    "# index=False：不把pandas的索引写入数据库\n",
    "\n",
    "df_manual.to_sql(\n",
    "    name=\"course_info_manual\",  # 自定义表名\n",
    "    con=engine,\n",
    "    if_exists=\"replace\",\n",
    "    index=False\n",
    ")\n",
    "\n",
    "print(\"✅ 任务3.1完成：手动数据已写入MySQL表 course_info_manual\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ea47bd4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "📊 从MySQL读取的课程表数据：\n",
      "    课程编号        课程名称  学分 授课教师         上课时间\n",
      "0  CS001  Python数据分析   4  张老师  周一/周三 第1-2节\n",
      "1  CS002       数据库原理   3  李老师  周二/周四 第3-4节\n",
      "2  MA001        高等数学   5  王老师  周一/周五 第5-6节\n",
      "3  EN001        大学英语   2  刘老师     周三 第7-8节\n"
     ]
    }
   ],
   "source": [
    "df_read = pd.read_sql(\n",
    "    sql=\"SELECT * FROM course_info_manual\",  # 读取整张表\n",
    "    con=engine\n",
    ")\n",
    "\n",
    "# 打印读取到的数据\n",
    "print(\"📊 从MySQL读取的课程表数据：\")\n",
    "print(df_read)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "89eecdba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "⚠️ 无表格/读取失败，尝试读取网页文字： html5lib not found, please install it\n",
      "\n",
      "✅ 网页文字读取成功（前500字）：\n",
      "                                              网页文字内容\n",
      "0  百度一下，你就知道新闻hao123地图视频贴吧登录更多产品关于百度About Baidu©2...\n",
      "✅ 网页文字已写入MySQL表：web_data_table_text\n",
      "\n",
      "🎉 全部完成！\n"
     ]
    }
   ],
   "source": [
    "# 1. 导入所有需要的库\n",
    "import pandas as pd\n",
    "from sqlalchemy import create_engine\n",
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "# =====================【只改这里！3个地方】=====================\n",
    "# 1. 要爬的网页地址\n",
    "WEB_URL = \"https://www.baidu.com\"\n",
    "# 2. 你的MySQL数据库名（改成你自己的）\n",
    "MYSQL_DB = \"test_db\"\n",
    "# 3. 要写入MySQL的表名\n",
    "MYSQL_TABLE = \"web_data_table\"\n",
    "# ==============================================================\n",
    "\n",
    "# MySQL固定配置（你刚重置的密码，不用改）\n",
    "MYSQL_USER = \"root\"\n",
    "MYSQL_PWD = \"123456\"\n",
    "MYSQL_HOST = \"localhost\"\n",
    "\n",
    "# 创建MySQL连接\n",
    "engine = create_engine(f\"mysql+pymysql://{MYSQL_USER}:{MYSQL_PWD}@{MYSQL_HOST}:3306/{MYSQL_DB}\")\n",
    "\n",
    "# ---------------------- 方式1：读取网页【表格】（最常用）----------------------\n",
    "try:\n",
    "    # 自动抓取网页所有表格，返回列表，[0]是第一个表格\n",
    "    df_table = pd.read_html(WEB_URL, encoding=\"utf-8\")[0]\n",
    "    print(\"✅ 网页表格读取成功：\")\n",
    "    print(df_table.head())\n",
    "\n",
    "    # 写入MySQL\n",
    "    df_table.to_sql(\n",
    "        name=MYSQL_TABLE,\n",
    "        con=engine,\n",
    "        if_exists=\"replace\",\n",
    "        index=False\n",
    "    )\n",
    "    print(f\"✅ 网页表格已写入MySQL表：{MYSQL_TABLE}\")\n",
    "\n",
    "except Exception as e:\n",
    "    print(\"⚠️ 无表格/读取失败，尝试读取网页文字：\", e)\n",
    "\n",
    "# ---------------------- 方式2：读取网页【文字】转DataFrame ----------------------\n",
    "try:\n",
    "    # 抓取网页文字\n",
    "    resp = requests.get(WEB_URL, timeout=10)\n",
    "    resp.encoding = \"utf-8\"\n",
    "    soup = BeautifulSoup(resp.text, \"html.parser\")\n",
    "    web_text = soup.get_text(strip=True)  # 提取纯文字\n",
    "\n",
    "    # 把文字转成Pandas（1行1列）\n",
    "    df_text = pd.DataFrame({\"网页文字内容\": [web_text[:500]]})  # 取前500字避免太长\n",
    "    print(\"\\n✅ 网页文字读取成功（前500字）：\")\n",
    "    print(df_text)\n",
    "\n",
    "    # 文字写入MySQL（表名加个后缀区分）\n",
    "    df_text.to_sql(\n",
    "        name=f\"{MYSQL_TABLE}_text\",\n",
    "        con=engine,\n",
    "        if_exists=\"replace\",\n",
    "        index=False\n",
    "    )\n",
    "    print(f\"✅ 网页文字已写入MySQL表：{MYSQL_TABLE}_text\")\n",
    "\n",
    "except Exception as e:\n",
    "    print(\"⚠️ 网页文字读取失败：\", e)\n",
    "\n",
    "print(\"\\n🎉 全部完成！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cc8e34f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "✅ 抓取到的天气数据：\n",
      "      日期/星期  天气状况 最高温度 最低温度 风力\n",
      "0   今天04/27    小雨   23   16   \n",
      "1   周二04/28    小雨   17   15   \n",
      "2   周三04/29  多云转阴   22   14   \n",
      "3   周四04/30     阴   26   16   \n",
      "4   周五05/01    小雨   29   17   \n",
      "5   周六05/02     阴   23   17   \n",
      "6   周日05/03    小雨   22   17   \n",
      "7   周一05/04   雨转阴   20   17   \n",
      "8   周二05/05  阴转多云   23   17   \n",
      "9   周三05/06   阴转雨   26   20   \n",
      "10  周四05/07     雨   21   18   \n",
      "11  周五05/08   雨转阴   21   17   \n",
      "12  周六05/09     雨   26   19   \n",
      "13  周日05/10   雨转阴   22   19   \n",
      "14  周一05/11     雨   30   22   \n",
      "\n",
      "📊 从MySQL读回的数据：\n",
      "      日期/星期  天气状况 最高温度 最低温度 风力\n",
      "0   今天04/27    小雨   23   16   \n",
      "1   周二04/28    小雨   17   15   \n",
      "2   周三04/29  多云转阴   22   14   \n",
      "3   周四04/30     阴   26   16   \n",
      "4   周五05/01    小雨   29   17   \n",
      "5   周六05/02     阴   23   17   \n",
      "6   周日05/03    小雨   22   17   \n",
      "7   周一05/04   雨转阴   20   17   \n",
      "8   周二05/05  阴转多云   23   17   \n",
      "9   周三05/06   阴转雨   26   20   \n",
      "10  周四05/07     雨   21   18   \n",
      "11  周五05/08   雨转阴   21   17   \n",
      "12  周六05/09     雨   26   19   \n",
      "13  周日05/10   雨转阴   22   19   \n",
      "14  周一05/11     雨   30   22   \n",
      "\n",
      "🎉 已成功写入MySQL表：jiangjin_weather_15d\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "from sqlalchemy import create_engine\n",
    "\n",
    "# ===================== MySQL配置（用你之前的账号密码）=====================\n",
    "engine = create_engine(\"mysql+pymysql://root:123456@localhost:3306/test_db\")\n",
    "url = \"http://sq.src.weather.com.cn/mweather15d/101040500.shtml\"\n",
    "table_name = \"jiangjin_weather_15d\"\n",
    "# ======================================================================\n",
    "\n",
    "# 1. 请求网页\n",
    "headers = {\n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36\"\n",
    "}\n",
    "response = requests.get(url, headers=headers, timeout=10)\n",
    "response.encoding = \"utf-8\"\n",
    "soup = BeautifulSoup(response.text, \"html.parser\")\n",
    "\n",
    "# 2. 按你F12的结构定位数据\n",
    "weather_data = []\n",
    "\n",
    "# 找到所有<li class=\"h15li\">，对应每一天的数据\n",
    "items = soup.select(\"ul.list-ul > li.h15li\")\n",
    "\n",
    "for item in items:\n",
    "    try:\n",
    "        # ① 日期/星期（在 h15listdaybox 里）\n",
    "        day_box = item.select_one(\".h15listdaybox\")\n",
    "        date_text = day_box.get_text(strip=True) if day_box else \"\"\n",
    "        \n",
    "        # ② 天气状况（在 h15k 里，第二个h15k）\n",
    "        weather_box = item.select(\".h15k\")[1] if len(item.select(\".h15k\")) >= 2 else None\n",
    "        weather = weather_box.get_text(strip=True) if weather_box else \"\"\n",
    "        \n",
    "        # ③ 温度（h15listtem 里的 23/16℃）\n",
    "        temp_box = item.select_one(\".h15listtem\")\n",
    "        temp_text = temp_box.get_text(strip=True) if temp_box else \"\"\n",
    "        high_temp, low_temp = temp_text.split(\"/\") if \"/\" in temp_text else (\"\", \"\")\n",
    "        \n",
    "        # ④ 风力/其他信息（xlt 里）\n",
    "        wind_box = item.select_one(\".xlt\")\n",
    "        wind = wind_box.get_text(strip=True) if wind_box else \"\"\n",
    "\n",
    "        weather_data.append({\n",
    "            \"日期/星期\": date_text,\n",
    "            \"天气状况\": weather,\n",
    "            \"最高温度\": high_temp.replace(\"℃\", \"\"),\n",
    "            \"最低温度\": low_temp.replace(\"℃\", \"\"),\n",
    "            \"风力\": wind\n",
    "        })\n",
    "    except Exception as e:\n",
    "        print(f\"某条数据抓取失败：{e}\")\n",
    "        continue\n",
    "\n",
    "# 3. 转成DataFrame\n",
    "df = pd.DataFrame(weather_data)\n",
    "print(\"✅ 抓取到的天气数据：\")\n",
    "print(df)\n",
    "\n",
    "# 4. 写入MySQL\n",
    "df.to_sql(\n",
    "    name=table_name,\n",
    "    con=engine,\n",
    "    if_exists=\"replace\",\n",
    "    index=False\n",
    ")\n",
    "\n",
    "# 5. 验证读取\n",
    "df_check = pd.read_sql(f\"SELECT * FROM {table_name}\", engine)\n",
    "print(\"\\n📊 从MySQL读回的数据：\")\n",
    "print(df_check)\n",
    "\n",
    "print(f\"\\n🎉 已成功写入MySQL表：{table_name}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ca698ea4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "爬取并清洗后的数据预览：\n",
      "     日期/星期 天气状况  最高温度  最低温度 风力\n",
      "0  今天04/27   今天    23    16   \n",
      "1  周二04/28   周二    17    15   \n",
      "2  周三04/29   周三    22    14   \n",
      "3  周四04/30   周四    26    16   \n",
      "4  周五05/01   周五    29    17   \n",
      "数据已成功写入 MySQL 表 test_db.tianqi！\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "import pandas as pd\n",
    "from sqlalchemy import create_engine\n",
    "# 【修复点1】导入SQLAlchemy标准数据类型（必须！）\n",
    "from sqlalchemy.types import VARCHAR, Integer\n",
    "\n",
    "# --------------------------\n",
    "# 1. 爬取网页数据\n",
    "# --------------------------\n",
    "headers = {\n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36\"\n",
    "}\n",
    "\n",
    "url = \"http://sq.src.weather.com.cn/mweather15d/101040500.shtml\"\n",
    "response = requests.get(url, headers=headers)\n",
    "response.encoding = \"utf-8\"\n",
    "soup = BeautifulSoup(response.text, \"html.parser\")\n",
    "\n",
    "weather_items = soup.select(\"ul.list-ul li.h15li\")\n",
    "\n",
    "# --------------------------\n",
    "# 2. 解析数据\n",
    "# --------------------------\n",
    "data_list = []\n",
    "for item in weather_items:\n",
    "    date_week = item.select_one(\"div.h15listdaybox\").get_text(strip=True) if item.select_one(\"div.h15listdaybox\") else \"\"\n",
    "    weather_status = item.select_one(\"div.h15k p\").get_text(strip=True) if item.select_one(\"div.h15k p\") else \"\"\n",
    "    temp_text = item.select_one(\"div.h15listtem\").get_text(strip=True) if item.select_one(\"div.h15listtem\") else \"\"\n",
    "    high_temp, low_temp = None, None\n",
    "    if temp_text:\n",
    "        temp_parts = temp_text.replace(\"℃\", \"\").split(\"/\")\n",
    "        if len(temp_parts) == 2:\n",
    "            high_temp = temp_parts[0]\n",
    "            low_temp = temp_parts[1]\n",
    "    wind = item.select_one(\"div.xlt\").get_text(strip=True) if item.select_one(\"div.xlt\") else \"\"\n",
    "\n",
    "    data_list.append({\n",
    "        \"日期/星期\": date_week,\n",
    "        \"天气状况\": weather_status,\n",
    "        \"最高温度\": high_temp,\n",
    "        \"最低温度\": low_temp,\n",
    "        \"风力\": wind\n",
    "    })\n",
    "\n",
    "# --------------------------\n",
    "# 3. 数据清洗\n",
    "# --------------------------\n",
    "df = pd.DataFrame(data_list)\n",
    "df[\"最高温度\"] = pd.to_numeric(df[\"最高温度\"], errors=\"coerce\")\n",
    "df[\"最低温度\"] = pd.to_numeric(df[\"最低温度\"], errors=\"coerce\")\n",
    "\n",
    "print(\"爬取并清洗后的数据预览：\")\n",
    "print(df.head())\n",
    "\n",
    "# --------------------------\n",
    "# 4. 写入MySQL（已修复）\n",
    "# --------------------------\n",
    "DB_USER = \"root\"\n",
    "DB_PASSWORD = \"123456\"\n",
    "DB_HOST = \"localhost\"\n",
    "DB_NAME = \"test_db\"\n",
    "TABLE_NAME = \"tianqi\"\n",
    "\n",
    "# 【修复点2】编码用utf8mb4，兼容性更强\n",
    "engine = create_engine(f\"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}/{DB_NAME}?charset=utf8mb4\")\n",
    "\n",
    "# 【修复点3】dtype使用SQLAlchemy类型对象，不再用字符串！\n",
    "df.to_sql(\n",
    "    name=TABLE_NAME,\n",
    "    con=engine,\n",
    "    if_exists=\"replace\",\n",
    "    index=False,\n",
    "    dtype={\n",
    "        \"日期/星期\": VARCHAR(50),   # 修复\n",
    "        \"天气状况\": VARCHAR(20),   # 修复\n",
    "        \"最高温度\": Integer,       # 修复\n",
    "        \"最低温度\": Integer,       # 修复\n",
    "        \"风力\": VARCHAR(50)        # 修复\n",
    "    }\n",
    ")\n",
    "\n",
    "print(f\"数据已成功写入 MySQL 表 {DB_NAME}.{TABLE_NAME}！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3e99fb0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Num1</th>\n",
       "      <th>Num2</th>\n",
       "      <th>Num3</th>\n",
       "      <th>Num4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.0</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>14.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>66.0</td>\n",
       "      <td>7</td>\n",
       "      <td>11</td>\n",
       "      <td>66.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.0</td>\n",
       "      <td>8</td>\n",
       "      <td>12</td>\n",
       "      <td>66.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Num1  Num2  Num3  Num4\n",
       "0   1.0     5     9  13.0\n",
       "1   2.0     6    10  14.0\n",
       "2  66.0     7    11  66.0\n",
       "3   4.0     8    12  66.0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df_na = pd.DataFrame({'Num1':[1, 2, None, 4],\n",
    "                      'Num2':[5, 6, 7, 8],\n",
    "                      'Num3':[9, 10, 11, 12],\n",
    "                      'Num4':[13, 14, np.NaN, np.NaN]})\n",
    "df_na.fillna(value=66.0) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d17ca47f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== 原始数据预览 ===\n",
      "    区    小区名称      标题          房屋信息       关注       地铁 单价 (元 / 平米) Unnamed: 7  \\\n",
      "0  锦江   翡翠城四期  翡翠城四期跃  高楼层 (共 29 层)  2009 年建  2 室 1 厅    85.21 平米         东南   \n",
      "1  锦江  时代豪庭一期  时代豪庭套三  中楼层 (共 38 层)  2009 年建  3 室 1 厅   155.79 平米         东南   \n",
      "2  锦江   卓锦城六期  卓锦城六期紫  中楼层 (共 31 层)  2014 年建  3 室 1 厅    89.33 平米         西南   \n",
      "3  锦江    星城银座  春熙路太古里  高楼层 (共 11 层)  2003 年建  1 室 0 厅    51.07 平米          南   \n",
      "4  锦江    新莲新苑  新莲新苑优质   高楼层 (共 7 层)  2001 年建  3 室 1 厅     77.7 平米         东南   \n",
      "\n",
      "    Unnamed: 8 Unnamed: 9  Unnamed: 10  \n",
      "0  331 人关注 / 5        近地铁     176036.0  \n",
      "1  137 人关注 / 5        NaN      26959.4  \n",
      "2   36 人关注 / 2        NaN      22612.8  \n",
      "3   29 人关注 / 5        近地铁      18014.5  \n",
      "4   14 人关注 / 5        NaN      13513.5  \n",
      "\n",
      "=== 数据基本信息 ===\n",
      "数据总行数：5，总列数：11\n",
      "\n",
      "各列缺失值数量：\n",
      "区              0\n",
      "小区名称           0\n",
      "标题             0\n",
      "房屋信息           0\n",
      "关注             0\n",
      "地铁             0\n",
      "单价 (元 / 平米)    0\n",
      "Unnamed: 7     0\n",
      "Unnamed: 8     0\n",
      "Unnamed: 9     3\n",
      "Unnamed: 10    0\n",
      "dtype: int64\n",
      "\n",
      "=== 含缺失值的行 ===\n",
      "    区    小区名称      标题          房屋信息       关注       地铁 单价 (元 / 平米) Unnamed: 7  \\\n",
      "1  锦江  时代豪庭一期  时代豪庭套三  中楼层 (共 38 层)  2009 年建  3 室 1 厅   155.79 平米         东南   \n",
      "2  锦江   卓锦城六期  卓锦城六期紫  中楼层 (共 31 层)  2014 年建  3 室 1 厅    89.33 平米         西南   \n",
      "4  锦江    新莲新苑  新莲新苑优质   高楼层 (共 7 层)  2001 年建  3 室 1 厅     77.7 平米         东南   \n",
      "\n",
      "    Unnamed: 8 Unnamed: 9  Unnamed: 10  \n",
      "1  137 人关注 / 5        NaN      26959.4  \n",
      "2   36 人关注 / 2        NaN      22612.8  \n",
      "4   14 人关注 / 5        NaN      13513.5  \n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 读取 Excel 文件（注意文件名是 \"secondhandhouse_one.xlsx.xlsx\"，需匹配实际文件名）\n",
    "file_path = r\"C:\\Users\\Administrator\\Desktop\\secondhandhouse_one.xlsx\"  # 路径根据你的文件位置调整\n",
    "df = pd.read_excel(file_path)\n",
    "\n",
    "# 1. 查看数据原始形态（前5行）\n",
    "print(\"=== 原始数据预览 ===\")\n",
    "print(df.head())\n",
    "\n",
    "# 2. 查看数据基本信息（行数、列数、数据类型、缺失值数量）\n",
    "print(\"\\n=== 数据基本信息 ===\")\n",
    "print(f\"数据总行数：{len(df)}，总列数：{len(df.columns)}\")\n",
    "print(\"\\n各列缺失值数量：\")\n",
    "print(df.isnull().sum())  # isnull() 标记缺失值，sum() 统计每列缺失值个数\n",
    "\n",
    "# 3. 查看含缺失值的行（定位具体哪些行有缺失）\n",
    "print(\"\\n=== 含缺失值的行 ===\")\n",
    "missing_rows = df[df.isnull().any(axis=1)]  # any(axis=1) 表示行内有任一缺失值\n",
    "print(missing_rows)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0f8b184",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
