-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
273 lines (225 loc) · 8.94 KB
/
database.py
File metadata and controls
273 lines (225 loc) · 8.94 KB
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
import pymysql
# from wangyi_crawler import NewsCategory as NewsCategory_wy
# from wangyi_crawler import get_news as get_news_wy
# from wangyi_crawler import get_new_detail as get_new_detail_wy
class Database:
def __init__(self):
self.conn = pymysql.connect(
host='localhost',
user='root',
password='wsp0222.',
database='news_db',
charset='utf8mb4'
)
self.cursor = self.conn.cursor()
#################################
# 插入的操作
def insert_single_news(self, news):
"""将新闻数据存入数据库"""
self.cursor.execute(
"INSERT INTO news (sid,cid,title,time,content,link,detail,source,imgurl,tienum,keywords) "
"VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", (
'3',
'8',
# 判空
news['title'] if news['title'] else '',
news['time'] if news['time'] else '',
news['content'] if news['content'] else '',
'',
'',
news['source'] if news['source'] else '',
'',
'',
''
)
)
self.conn.commit()
def save_news(self, news_data):
"""将新闻数据存入数据库"""
for news in news_data:
print(type(news['sid']))
self.cursor.execute(
"INSERT INTO news (sid,cid,title,time,content,link,detail,source,imgurl,tienum,keywords) "
"VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", (
news['sid'],
news['cid'],
news['title'],
news['time'],
news['content'],
news['link'],
news['detail'],
news['source'],
news['imgurl'],
news['tienum'],
news['keywords']
)
)
self.conn.commit()
def save_news_wy(self, news_data):
"""将新闻数据存入数据库"""
for news in news_data:
self.cursor.execute(
"INSERT INTO news (sid,cid,title,time,content,link) "
"VALUES (%s,%s,%s,%s,%s,%s)", (
news['sid'],
news['cid'],
news['title'],
news['time'],
news['content'],
news['link'],
)
)
self.conn.commit()
def save_news_xl(self, news_data):
"""将新闻数据存入数据库"""
for news in news_data:
# 去除title中的引号
news['title'] = news['title'].replace("'", "")
news['title'] = news['title'].replace('"', '')
self.cursor.execute(
"INSERT INTO news (sid,cid,title,time,content,link,detail) "
"VALUES (%s,%s,%s,%s,%s,%s,%s)", (
news['sid'],
news['cid'],
news['title'],
news['time'],
news['content'],
news['link'],
news['detail'],
)
)
self.conn.commit()
def close(self):
"""关闭数据库连接"""
self.cursor.close()
self.conn.close()
#################################
# 查询的操作
def get_news(self):
"""获取新闻数据"""
self.cursor.execute("SELECT * FROM news")
# 返回数组,元素是每个新闻的列表
arr = self.cursor.fetchall()
data = []
for item in arr:
data.append({
'nid': item[0],
'sid': item[1],
'cid': item[2],
'title': item[3],
'time': item[4],
'content': item[5],
'link': item[6],
'detail': item[7],
'source': item[8],
'imgurl': item[9],
'tienum': item[10],
'keywords': item[11]
})
return data
def get_news_wy(self):
"""获取网易新闻数据"""
# 返回数组
self.cursor.execute("SELECT * FROM news where sid=1")
return self.cursor.fetchall()
def get_news_xl(self):
"""获取新浪新闻数据"""
self.cursor.execute("SELECT * FROM news where sid=2")
return self.cursor.fetchall()
def get_news_by_title(self,title):
"""获取新闻数据"""
self.cursor.execute("SELECT * FROM news WHERE title = %s",title)
return self.cursor.fetchall()
def get_news_by_time(self,time):
"""获取新闻数据"""
self.cursor.execute("SELECT * FROM news WHERE time = %s",time)
return self.cursor.fetchall()
#################################
# 删除的操作
def delete_news(self,nid):
"""删除新闻数据"""
self.cursor.execute("DELETE FROM news WHERE nid = %s",nid)
self.conn.commit()
def delete_news_by_title(self,title):
"""删除新闻数据"""
self.cursor.execute("DELETE FROM news WHERE title = %s",title)
self.conn.commit()
def delete_news_by_time(self,time):
"""删除新闻数据"""
self.cursor.execute("DELETE FROM news WHERE time = %s",time)
self.conn.commit()
def delete_news_by_source(self,source):
"""删除新闻数据"""
self.cursor.execute("DELETE FROM news WHERE source = %s",source)
self.conn.commit()
#################################
# 更新的操作
def update_news(self, data_n):
"""执行SQL更新语句示例"""
# 穿进来的是一个字典
data_n['title'] = data_n['title'].replace("'", "")
data_n['title'] = data_n['title'].replace('"', '')
# title两边加引号
sql = "UPDATE news SET title = '%s' WHERE nid = %s" % (data_n['title'], data_n['nid'])
print(sql)
self.cursor.execute(sql)
self.conn.commit()
def update_news_source(self, data_n):
"""执行SQL更新语句示例"""
# 穿进来的是一个字典
data_n['source'] = data_n['source'].replace("'", "")
data_n['source'] = data_n['source'].replace('"', '')
# title两边加引号
sql = "UPDATE news SET source = '%s' WHERE nid = %s" % (data_n['source'], data_n['nid'])
print(sql)
self.cursor.execute(sql)
self.conn.commit()
def update_news_content(self, data_n):
"""执行SQL更新语句示例"""
# 穿进来的是一个字典
data_n['content'] = data_n['content'].replace("'", "")
data_n['content'] = data_n['content'].replace('"', '')
# title两边加引号
sql = "UPDATE news SET content = '%s' WHERE nid = %s" % (data_n['content'], data_n['nid'])
print(sql)
self.cursor.execute(sql)
self.conn.commit()
def update_news_link(self, data_n):
"""执行SQL更新语句示例"""
# 穿进来的是一个字典
data_n['link'] = data_n['link'].replace("'", "")
data_n['link'] = data_n['link'].replace('"', '')
# title两边加引号
sql = "UPDATE news SET link = '%s' WHERE nid = %s" % (data_n['link'], data_n['nid'])
print(sql)
self.cursor.execute(sql)
self.conn.commit()
#################################
# 其他功能
def find_new(self,nid):
"""查找新闻数据"""
self.cursor.execute("SELECT * FROM news WHERE nid = %s",nid)
return self.cursor.fetchall()[0]
def find_like_title_news(self,like_title):
"""查找新闻数据"""
# SELECT * FROM news WHERE title LIKE '%白龙%'
# 有%号
sql_template = "SELECT * FROM news,new_source,new_category WHERE news.sid=new_source.sid and news.cid =new_category.cid and title LIKE '%%%s%%'"
sql = sql_template % like_title
self.cursor.execute(sql)
return self.cursor.fetchall()[0]
def find_most_news(self):
"""查找新闻数据"""
# 使用聚合函数max
sql = "SELECT * FROM news WHERE tienum = (SELECT max(cast(tienum as signed)) FROM news)"
self.cursor.execute(sql)
return self.cursor.fetchall()[0]
url_list = \
{'国内': 'https://temp.163.com/special/00804KVA/cm_guonei_0{}.js?callback=data_callback',
'国际': 'https://temp.163.com/special/00804KVA/cm_guoji_0{}.js?callback=data_callback',
'军事': 'https://temp.163.com/special/00804KVA/cm_war_0{}.js?callback=data_callback',
'航空': 'https://temp.163.com/special/00804KVA/cm_hangkong_0{}.js?callback=data_callback',
# '科技': 'https://tech.163.com/special/00097UHL/tech_datalist_0{}.js?callback=data_callback',
'体育': 'https://temp.163.com/special/00804KVA/cm_sports_0{}.js?callback=data_callback',
'度假': 'https://temp.163.com/special/00804KVA/cm_dujia_0{}.js?callback=data_callback',
}