forked from zjfGit/Scrapy-Spider-based-on-Python3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqlUtils.py
196 lines (176 loc) · 4.82 KB
/
mysqlUtils.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
import pymysql
import pymysql.cursors
import os
def dbhandle_online():
host = '192.168.1.235'
user = 'root'
passwd = 'douguo2015'
charset = 'utf8'
conn = pymysql.connect(
host=host,
user=user,
passwd=passwd,
charset=charset,
use_unicode=False
)
return conn
def dbhandle_local():
host = '192.168.1.235'
user = 'root'
passwd = 'douguo2015'
charset = 'utf8'
conn = pymysql.connect(
host=host,
user=user,
passwd=passwd,
charset=charset,
use_unicode=True
# use_unicode=False
)
return conn
def dbhandle_geturl(gid):
host = '192.168.1.235'
user = 'root'
passwd = 'douguo2015'
charset = 'utf8'
conn = pymysql.connect(
host=host,
user=user,
passwd=passwd,
charset=charset,
use_unicode=False
)
cursor = conn.cursor()
sql = 'select url,spider_name,site,gid,module from dg_spider.dg_spider_post where status=0 and gid=%s limit 1' % gid
try:
cursor.execute(sql)
result = cursor.fetchone()
conn.commit()
except Exception as e:
print("***** exception")
print(e)
conn.rollback()
if result is None:
os._exit(0)
else:
url = result[0]
spider_name = result[1]
site = result[2]
gid = result[3]
module = result[4]
return url.decode(), spider_name.decode(), site.decode(), gid.decode(), module.decode()
def dbhandle_insert_content(url, title, content, user_id, has_img):
host = '192.168.1.235'
user = 'root'
passwd = 'douguo2015'
charset = 'utf8'
conn = pymysql.connect(
host=host,
user=user,
passwd=passwd,
charset=charset,
use_unicode=False
)
cur = conn.cursor()
# 如果标题或者内容为空,那么程序将退出,篇文章将会作废并将status设置为1,爬虫继续向下运行获得新的URl
if content.strip() == '' or title.strip() == '':
sql_fail = 'update dg_spider.dg_spider_post set status="%s" where url="%s" ' % ('1', url)
try:
cur.execute(sql_fail)
result = cur.fetchone()
conn.commit()
except Exception as e:
print(e)
conn.rollback()
os._exit(0)
sql = 'update dg_spider.dg_spider_post set title="%s",content="%s",user_id="%s",has_img="%s" where url="%s" ' \
% (title, content, user_id, has_img, url)
try:
cur.execute(sql)
result = cur.fetchone()
conn.commit()
except Exception as e:
print(e)
conn.rollback()
return result
def dbhandle_update_status(url, status):
host = '192.168.1.235'
user = 'root'
passwd = 'douguo2015'
charset = 'utf8'
conn = pymysql.connect(
host=host,
user=user,
passwd=passwd,
charset=charset,
use_unicode=False
)
cur = conn.cursor()
sql = 'update dg_spider.dg_spider_post set status="%s" where url="%s" ' \
% (status, url)
try:
cur.execute(sql)
result = cur.fetchone()
conn.commit()
except Exception as e:
print(e)
conn.rollback()
return result
def dbhandle_get_content(url):
host = '192.168.1.235'
user = 'root'
passwd = 'douguo2015'
charset = 'utf8'
conn = pymysql.connect(
host=host,
user=user,
passwd=passwd,
charset=charset,
use_unicode=False
)
cursor = conn.cursor()
sql = 'select title,content,user_id,gid from dg_spider.dg_spider_post where status=1 and url="%s" limit 1' % url
try:
cursor.execute(sql)
result = cursor.fetchone()
conn.commit()
except Exception as e:
print("***** exception")
print(e)
conn.rollback()
if result is None:
os._exit(1)
title = result[0]
content = result[1]
user_id = result[2]
gid = result[3]
return title.decode(), content.decode(), user_id.decode(), gid.decode()
# 获取爬虫初始化参数
def dbhandle_get_spider_param(url):
host = '192.168.1.235'
user = 'root'
passwd = 'douguo2015'
charset = 'utf8'
conn = pymysql.connect(
host=host,
user=user,
passwd=passwd,
charset=charset,
use_unicode=False
)
cursor = conn.cursor()
sql = 'select title,content,user_id,gid from dg_spider.dg_spider_post where status=0 and url="%s" limit 1' % url
result = ''
try:
cursor.execute(sql)
result = cursor.fetchone()
conn.commit()
except Exception as e:
print("***** exception")
print(e)
conn.rollback()
title = result[0]
content = result[1]
user_id = result[2]
gid = result[3]
return title.decode(), content.decode(), user_id.decode(), gid.decode()