Upload 02CommonConcern
Browse files
02CommonConcern20to50/2.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
def process_following_data(source_db_path, target_db_path, min_followers=20, max_followers=50):
|
| 8 |
+
"""
|
| 9 |
+
处理关注者数据,筛选出符合条件的用户并保存到新数据库
|
| 10 |
+
"""
|
| 11 |
+
if not os.path.exists(source_db_path):
|
| 12 |
+
raise FileNotFoundError(f"找不到数据库文件: {source_db_path}")
|
| 13 |
+
|
| 14 |
+
conn = sqlite3.connect(source_db_path)
|
| 15 |
+
|
| 16 |
+
print("正在读取关注关系数据...")
|
| 17 |
+
df_relationships = pd.read_sql_query("""
|
| 18 |
+
SELECT user_id, following_of
|
| 19 |
+
FROM following_relationships
|
| 20 |
+
""", conn)
|
| 21 |
+
|
| 22 |
+
print("正在统计关注者数量...")
|
| 23 |
+
grouped = (
|
| 24 |
+
df_relationships
|
| 25 |
+
.groupby('user_id')['following_of']
|
| 26 |
+
.agg(lambda x: list(set(x)))
|
| 27 |
+
.reset_index()
|
| 28 |
+
)
|
| 29 |
+
grouped['follower_count'] = grouped['following_of'].apply(len)
|
| 30 |
+
|
| 31 |
+
filtered = grouped[
|
| 32 |
+
(grouped['follower_count'] >= min_followers) &
|
| 33 |
+
(grouped['follower_count'] <= max_followers)
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
print("正在获取用户详细信息...")
|
| 37 |
+
target_users = tuple(filtered['user_id'].tolist())
|
| 38 |
+
|
| 39 |
+
if not target_users:
|
| 40 |
+
print("没有找到符合条件的用户")
|
| 41 |
+
conn.close()
|
| 42 |
+
return 0
|
| 43 |
+
|
| 44 |
+
# 将列表转换为字符串
|
| 45 |
+
filtered['following_of'] = filtered['following_of'].apply(json.dumps)
|
| 46 |
+
|
| 47 |
+
users_df = pd.read_sql_query("""
|
| 48 |
+
SELECT *
|
| 49 |
+
FROM users
|
| 50 |
+
WHERE user_id IN ({})
|
| 51 |
+
""".format(','.join(['?' for _ in target_users])), conn, params=target_users)
|
| 52 |
+
|
| 53 |
+
# 合并数据,只保留需要的列
|
| 54 |
+
result_df = pd.merge(
|
| 55 |
+
users_df,
|
| 56 |
+
filtered[['user_id', 'follower_count']],
|
| 57 |
+
on='user_id',
|
| 58 |
+
how='inner'
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
print(f"正在保存符合条件的 {len(result_df)} 个用户信息到新数据库...")
|
| 62 |
+
|
| 63 |
+
# 保存到新数据库
|
| 64 |
+
target_conn = sqlite3.connect(target_db_path)
|
| 65 |
+
|
| 66 |
+
# 确保删除following_of列(如果存在),因为我们会在单独的表中存储它
|
| 67 |
+
if 'following_of' in result_df.columns:
|
| 68 |
+
result_df = result_df.drop('following_of', axis=1)
|
| 69 |
+
|
| 70 |
+
# 创建主表
|
| 71 |
+
result_df.to_sql('users', target_conn, index=False, if_exists='replace')
|
| 72 |
+
|
| 73 |
+
# 创建索引
|
| 74 |
+
target_conn.execute("CREATE INDEX IF NOT EXISTS idx_user_id ON users(user_id)")
|
| 75 |
+
target_conn.execute("CREATE INDEX IF NOT EXISTS idx_followers_count ON users(followers_count)")
|
| 76 |
+
|
| 77 |
+
# 创建并填充关注者表
|
| 78 |
+
target_conn.execute("""
|
| 79 |
+
CREATE TABLE IF NOT EXISTS user_followers (
|
| 80 |
+
user_id TEXT PRIMARY KEY,
|
| 81 |
+
followers TEXT,
|
| 82 |
+
follower_count INTEGER,
|
| 83 |
+
FOREIGN KEY(user_id) REFERENCES users(user_id)
|
| 84 |
+
)
|
| 85 |
+
""")
|
| 86 |
+
|
| 87 |
+
# 准备关注者数据
|
| 88 |
+
followers_data = filtered[['user_id', 'following_of', 'follower_count']]
|
| 89 |
+
followers_data.to_sql('user_followers', target_conn, index=False, if_exists='replace')
|
| 90 |
+
|
| 91 |
+
# 打印预览
|
| 92 |
+
print("\n符合条件的用户数据预览:")
|
| 93 |
+
preview_df = result_df[['user_id', 'name', 'followers_count', 'friends_count']].head()
|
| 94 |
+
print(preview_df)
|
| 95 |
+
|
| 96 |
+
conn.close()
|
| 97 |
+
target_conn.close()
|
| 98 |
+
|
| 99 |
+
return len(result_df)
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
# 设置数据库路径
|
| 103 |
+
source_db = "following_expanded.db"
|
| 104 |
+
target_db = f"filtered_users_{datetime.now().strftime('%Y%m%d_%H%M%S')}.db"
|
| 105 |
+
|
| 106 |
+
try:
|
| 107 |
+
result_count = process_following_data(source_db, target_db)
|
| 108 |
+
print(f"\n处理完成!已将 {result_count} 个用户的信息保存到 {target_db}")
|
| 109 |
+
except Exception as e:
|
| 110 |
+
print(f"处理过程中出现错误: {str(e)}")
|
| 111 |
+
# 打印详细的错误跟踪信息
|
| 112 |
+
import traceback
|
| 113 |
+
print("\n详细错误信息:")
|
| 114 |
+
print(traceback.format_exc())
|
02CommonConcern20to50/requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests>=2.25.0
|
| 2 |
+
urllib3>=2.0.0
|
| 3 |
+
typing>=3.7.4
|
| 4 |
+
sqlite3-api>=2.0.0
|
02CommonConcern20to50/result/filtered_users.db
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:297b29029aaf2b55c5018c0e115d0e7677bf0bc38ebe531248be56af0e344f34
|
| 3 |
+
size 3432448
|