-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore.py
284 lines (233 loc) · 9.06 KB
/
store.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import pymysql
class Store(object):
"""
Store is a class designed to store extracted data into MySQL and Excel.
Methods:
mode_mysql(db_name, tb_name): Stores data into a MySQL database.
mode_excel(excel_name): Stores data into an Excel file.
"""
def __init__(self, data_list):
"""
Initializes the Store class with a list of data.
Args:
data_list (list): The list of data to store.
"""
self.data_list = data_list
def store_user_mode_mysql(self, db_name, tb_name):
"""
Stores data into a MySQL database.
Args:
db_name (str): The name of the database.
tb_name (str): The name of the table.
"""
try:
# 连接数据库
self.db = pymysql.connect(host='localhost', user='root', passwd='wz131', port=3306)
self.cursor = self.db.cursor()
print('-连接成功-')
# 检查并创建数据库(如果不存在)
self.cursor.execute(f"CREATE DATABASE IF NOT EXISTS {db_name}")
print('-数据库已检查-')
# 选择数据库
self.cursor.execute(f"USE {db_name}")
# 创建数据表
self.__create_user_table(tb_name)
# 插入数据
self.__user_insert(tb_name)
# print('-正在插入数据-')
# 关闭mysql
self.db.close()
print('-连接关闭-')
except pymysql.MySQLError as e:
print(f"连接数据库时出现错误:{e}")
def __create_user_table(self, tb_name):
"""
Creates a table in the MySQL database if it doesn't exist.
Args:
tb_name (str): The name of the table.
"""
try:
create_table_query = f"""
CREATE TABLE IF NOT EXISTS {tb_name} (
MAIN_ID INT AUTO_INCREMENT PRIMARY KEY,
账号url Text,
账号ID Text,
账号昵称 Text,
性别 VARCHAR(50),
关系状况 Text,
个人描述 Text
);
"""
self.cursor.execute(create_table_query)
print('-表已检查-')
except pymysql.MySQLError as e:
print(f"创建数据表时出现错误:{e}")
def __user_insert(self, tb_name):
"""
Inserts data into the MySQL table.
Args:
tb_name (str): The name of the table.
"""
try:
insert_query = f"""
INSERT INTO {tb_name} (账号url, 账号ID, 账号昵称, 性别, 关系状况, 个人描述)
VALUES (%s, %s, %s, %s, %s, %s)
"""
# 执行多条插入,每个元素是一个字典
for post in self.data_list:
self.cursor.execute(insert_query, (
post['账号url'], post['账号ID'], post['账号昵称'],
post['性别'], post['关系状况'], post['个人描述']
))
self.db.commit()
print('-数据插入成功-')
except pymysql.MySQLError as e:
print(f"执行插入操作时出现错误:{e}")
self.db.rollback()
def store_friends_following_followers_mode_mysql(self, db_name, tb_name):
"""
Stores data into a MySQL database.
Args:
db_name (str): The name of the database.
tb_name (str): The name of the table.
"""
try:
# 连接数据库
self.db = pymysql.connect(host='localhost', user='root', passwd='wz131', port=3306)
self.cursor = self.db.cursor()
print('-连接成功-')
# 检查并创建数据库(如果不存在)
self.cursor.execute(f"CREATE DATABASE IF NOT EXISTS {db_name}")
print('-数据库已检查-')
# 选择数据库
self.cursor.execute(f"USE {db_name}")
# 创建数据表
self.__create_friends_following_followers_table(tb_name)
# 插入数据
self.__friends_following_followers_insert(tb_name)
# print('-正在插入数据-')
# 关闭mysql
self.db.close()
print('-连接关闭-')
except pymysql.MySQLError as e:
print(f"连接数据库时出现错误:{e}")
def __create_friends_following_followers_table(self, tb_name):
"""
Creates a table in the MySQL database if it doesn't exist.
Args:
tb_name (str): The name of the table.
"""
try:
create_table_query = f"""
CREATE TABLE IF NOT EXISTS {tb_name} (
MAIN_ID INT AUTO_INCREMENT PRIMARY KEY,
own_url Text,
name Text,
profile_url Text,
profile_image_url Text,
descs Text,
cursors Text
);
"""
self.cursor.execute(create_table_query)
print('-表已检查-')
except pymysql.MySQLError as e:
print(f"创建数据表时出现错误:{e}")
def __friends_following_followers_insert(self, tb_name):
"""
Inserts data into the MySQL table.
Args:
tb_name (str): The name of the table.
"""
try:
insert_query = f"""
INSERT INTO {tb_name} (own_url, name, profile_url, profile_image_url, descs, cursors)
VALUES (%s, %s, %s, %s, %s, %s)
"""
# 执行多条插入,每个元素是一个字典
for post in self.data_list:
self.cursor.execute(insert_query, (
post['own_url'], post['name'], post['profile_url'],
post['profile_image_url'], post['desc'], post['cursor']
))
self.db.commit()
print('-数据插入成功-')
except pymysql.MySQLError as e:
print(f"执行插入操作时出现错误:{e}")
self.db.rollback()
def store_post_mode_mysql(self, db_name, tb_name):
"""
Stores data into a MySQL database.
Args:
db_name (str): The name of the database.
tb_name (str): The name of the table.
"""
try:
# 连接数据库
self.db = pymysql.connect(host='localhost', user='root', passwd='wz131', port=3306)
self.cursor = self.db.cursor()
print('-连接成功-')
# 检查并创建数据库(如果不存在)
self.cursor.execute(f"CREATE DATABASE IF NOT EXISTS {db_name}")
print('-数据库已检查-')
# 选择数据库
self.cursor.execute(f"USE {db_name}")
# 创建数据表
self.__create_post_table(tb_name)
# 插入数据
self.__post_insert(tb_name)
# print('-正在插入数据-')
# 关闭mysql
self.db.close()
print('-连接关闭-')
except pymysql.MySQLError as e:
print(f"连接数据库时出现错误:{e}")
def __create_post_table(self, tb_name):
"""
Creates a table in the MySQL database if it doesn't exist.
Args:
tb_name (str): The name of the table.
"""
try:
create_table_query = f"""
CREATE TABLE IF NOT EXISTS {tb_name} (
MAIN_ID INT AUTO_INCREMENT PRIMARY KEY,
external_id Text,
post_id Text,
post_url Text,
text Text,
creater_id Text,
creater_name Text,
creater_url Text,
creation_time Text,
comments_count int,
reaction_count Text
);
"""
self.cursor.execute(create_table_query)
print('-表已检查-')
except pymysql.MySQLError as e:
print(f"创建数据表时出现错误:{e}")
def __post_insert(self, tb_name):
"""
Inserts data into the MySQL table.
Args:
tb_name (str): The name of the table.
"""
try:
insert_query = f"""
INSERT INTO {tb_name} (external_id, post_id, post_url, text, creater_id, creater_name,creater_url,creation_time,comments_count,reaction_count)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
# 执行多条插入,每个元素是一个字典
for post in self.data_list:
self.cursor.execute(insert_query, (
post['X_id'], post['post_id'], post['post_url'],
post['text'], post['creater_id'], post['creater_name'],
post['creater_url'],post['creation_time'],post['comments_count'],post['reaction_count']
))
self.db.commit()
print('-数据插入成功-')
except pymysql.MySQLError as e:
print(f"执行插入操作时出现错误:{e}")
self.db.rollback()