forked from Chenimal/crawlerLagou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawlerBase.py
94 lines (84 loc) · 2.9 KB
/
crawlerBase.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
import urllib.request
import urllib.parse
import json
import lib.functions as func
import time
import model
'''
Base Crawler for www.lagou.com
Author: Chen Sun
Since: 10.21.2015
Version: 1.1.2
'''
class CrawlerBase():
# constructor
def __init__(self):
self.total_new = 0
self.table = 'lagou_basic'
self.table2 = 'lagou_company_label'
self.url_base = 'http://www.lagou.com'
self.url_params = '/jobs/positionAjax.json?px=new'
try:
# generate insert query
self.model = model.dbSqlite()
self.iq_1 = self.model.insertQuery(self.table)
self.ip_1 = self.model.insertParam(self.table)
except Exception as e:
msg = time.strftime(
'%Y-%m-%d %H:%M:%S') + '[Error][Init] ' + str(e)
func.logger('crawler', msg)
exit()
# single request(page)
def fetchPageContent(self, post={}):
try:
f = urllib.request.urlopen(
url=self.url_base + self.url_params,
data=urllib.parse.urlencode(post).encode('utf-8'),
timeout=2)
d = f.read().decode('utf-8')
d = json.loads(d)
return d['content']['result']
except Exception as e:
msg = time.strftime(
'%Y-%m-%d %H:%M:%S') + ' [error][network] ' + str(e)
func.logger('crawler', msg)
return []
# save content for single request(page)
def savePageContent(self, data):
c = 0
for i in data:
if not self.hasDuplicate(i):
s = self.addRecord(i)
c = c + 1 if s else c
return c
# checkout duplicate before insert
def hasDuplicate(self, data):
r = self.model.findAll(
"select * from %s where position_id = '%s'" % (self.table, data['positionId']))
return True if r else False
# for single new record
def addRecord(self, data):
try:
p = list(map(lambda x: data.get(x), self.ip_1))
self.model.cursor.execute(self.iq_1, p)
self.model.conn.commit()
# insert into lagou_company_label
p = list(
map(lambda x: (data['positionId'], x), data['companyLabelList']))
q = "insert into " + self.table2 + \
" (position_id, label) values(?,?)"
self.model.cursor.executemany(q, p)
self.model.conn.commit()
return True
except Exception as e:
msg = time.strftime(
'%Y-%m-%d %H:%M:%S') + ' [error][database] ' + str(e)
func.logger('crawler', msg)
return False
# single request
def singleRequest(self, i):
# requests with diffrent page number
d = self.fetchPageContent({'pn': i})
c = self.savePageContent(d)
print('Page %d : %d items were added' % (i, c))
return c