-
Notifications
You must be signed in to change notification settings - Fork 20
/
v1.py
4100 lines (3934 loc) · 207 KB
/
v1.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author : Eeyhan
# @File : v1.py
import gevent
from gevent import monkey
from gevent.pool import Pool
monkey.patch_all()
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import urllib.parse
from lxml import etree
import random
import time
import json
import asyncio
import os
import sys
sys.path.append(os.path.dirname(__file__))
from datetime import datetime
from datetime import timedelta
import re
import redis
from concurrent.futures import ThreadPoolExecutor, wait
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.schedulers.gevent import GeventScheduler
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from proxy.proxy import get_redis as get_proxy_redis
from config import USER_AGENT, REQEUST_URLS, ZHUOPIN_CLIENT_ID, LAGOU_SHOW, POOL, POOL2, POOL3
from config import BOSS_COOKIE, SEARCH_ARGS, INTERVAL, END_PAGE, GEVENT_POOL, THREAD_POOL
from logger import logger
requests.packages.urllib3.disable_warnings()
requests.adapters.DEFAULT_RETRIES = 5
CRAWL_LOG = logger()
class RequestHeader(object):
"""请求类"""
def __init__(self):
self.user_agent = USER_AGENT
def request_headers(self):
"""
请求头
:return:
"""
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,application/json, text/javascript,*/*;q=0.8',
'User-Agent': random.choice(self.user_agent),
'Accept-Language': 'en-US,en;q=0.5',
# 'Connection': 'keep-alive',
'Connection': 'close',
'Accept-Encoding': 'gzip, deflate',
'Upgrade-Insecure-Requests': '1'
}
return headers
class ProxyHandler(object):
"""代理"""
def __init__(self):
pass
def __new__(cls, *args, **kwargs):
if not hasattr(ProxyHandler, "proxy_list"):
ProxyHandler.proxy_list = get_proxy_redis()
return ProxyHandler
@classmethod
def get_proxies(cls):
"""
从redis里获取proxy
:return:
"""
if cls.proxy_list:
return cls.proxy_list
class TargetUrlHandler(object):
"""目标url"""
def __init__(self):
pass
def __new__(cls, *args, **kwargs):
if not hasattr(TargetUrlHandler, "target_urls"):
TargetUrlHandler.target_urls = get_url_redis()
return TargetUrlHandler
@classmethod
def get_target_urls(cls):
"""
从redis里获取proxy
:return:
"""
if cls.target_urls:
return cls.target_urls
class BaseCrawl(RequestHeader):
"""爬虫类"""
DOUMI_COOKIE_URL = 'http://www.doumi.com/%s/'
BAIDU_COOKIE_URL = 'https://zhaopin.baidu.com/quanzhi'
BAIDU_COOKIE_JIANZHI_URL = 'https://zhaopin.baidu.com/jianzhi'
def __init__(self):
super(BaseCrawl, self).__init__()
self.header = self.get_header # 请求头
self.request_urls = self.get_request_urls # 待格式化的所有目标请求站
self.proxy_list = None # 代理列表
self.target_urls = None # 目标url
self.target_urls_dict = dict() # 目标url的字典形式
self.invalid_urls = set() # 无效url
self.jobs = [] # 爬取到的职位信息
self.search_args = SEARCH_ARGS # 搜索关键词列表
self.is_end = False # 完结信号
def get_urls_flag(self, url_name):
"""
获取对应的url的flag值
:param url_name: url别名
:return:
"""
name = 'flag_' + url_name.split('_')[1]
if hasattr(self, name):
attr = getattr(self, name)
return attr
def set_urls_flag(self, url_type):
"""
设置各自url的标志位
:param url_type: url的分类
:return:
"""
name = 'flag_' + url_type
setattr(self, name, False)
def get_random_time(self):
"""
取一个0-0.5之间的随机小数数
:return:
"""
timer = random.uniform(0, 3)
timer = round(timer, 2)
return timer
def get_args(self):
"""
返回一个搜索关键词
:return:
"""
# 乱序
random.shuffle(self.search_args)
count = len(self.search_args)
i = random.randint(0, count)
args = self.search_args.pop(i)
return args
def get_doumi_city_codes(self):
"""
返回斗米网的城市代码
:return:
"""
codes = ['aba', 'akesu', 'alaer', 'alashan', 'aletai', 'ali', 'ankang', 'anqing', 'anshan', 'anshun', 'anyang',
'aomen', 'bj', 'baicheng', 'baise', 'baishan', 'baiyin', 'baoding', 'baoji', 'baoshan', 'baotou',
'bayannaoer', 'bayinguoleng', 'bazhong', 'beihai', 'bengbu', 'benxi', 'bijie', 'binzhou', 'boertala',
'bozhou', 'cc', 'cs', 'cd', 'cq', 'cangzhou', 'changde', 'changdu', 'changji', 'changshu', 'changzhi',
'changzhou', 'chaohu', 'chaoyang', 'chaozhou', 'chengde', 'chenzhou', 'chifeng', 'chizhou', 'chongzuo',
'chuxiong', 'chuzhou', 'cixi', 'dl', 'dg', 'dali', 'dandong', 'danzhou', 'daqing', 'datong',
'daxinganling', 'dazhou', 'dehong', 'deyang', 'dezhou', 'dingxi', 'diqing', 'dongying', 'eerduosi',
'enshi', 'ezhou', 'foshan', 'fz', 'fangchenggang', 'fushun', 'fuxin', 'fuyang', 'jxfuzhou', 'gz', 'gy',
'gannan', 'ganzhou', 'ganzi', 'guangan', 'guangyuan', 'guigang', 'gl', 'guoluo', 'guyuan', 'hrb', 'hn',
'hz', 'hf', 'nmg', 'haibei', 'haidong', 'hainanzhou', 'haixi', 'hami', 'handan', 'hanzhong', 'hebi',
'hechi', 'hegang', 'heihe', 'hengshui', 'hengyang', 'hetian', 'heyuan', 'heze', 'hezhou', 'hljyichun',
'honghe', 'huaian', 'huaibei', 'huaihua', 'huainan', 'huanggang', 'huangnan', 'huangshan', 'huangshi',
'huizhou', 'huludao', 'hulunbeier', 'huzhou', 'jn', 'jiamusi', 'jian', 'jiangmen', 'jiaozhou',
'jiaozuo', 'jiaxing', 'jiayuguan', 'jieyang', 'jilin', 'jimo', 'jinchang', 'jincheng', 'jingdezhen',
'jingmen', 'jingzhou', 'jinhua', 'jining', 'jinzhong', 'jinzhou', 'jiujiang', 'jiuquan', 'jixi',
'jiyuan', 'km', 'kaifeng', 'kashi', 'kelamayi', 'kezilesu', 'kuerle', 'kunshan', 'lz', 'linyi',
'laibin', 'laiwu', 'langfang', 'xz', 'leshan', 'liangshan', 'lianyungang', 'liaocheng', 'liaoyang',
'liaoyuan', 'lijiang', 'lincang', 'linfen', 'linxia', 'linzhi', 'lishui', 'liupanshui', 'liuzhou',
'longnan', 'longyan', 'loudi', 'luan', 'luohe', 'luoyang', 'luzhou', 'lvliang', 'maanshan', 'maoming',
'meishan', 'meizhou', 'mianyang', 'mudanjiang', 'nj', 'nn', 'nb', 'nc', 'nanchong', 'nanping',
'nantong', 'nanyang', 'naqu', 'neijiang', 'ningde', 'nujiang', 'panjin', 'panzhihua', 'pingdingshan',
'pingliang', 'pingxiang', 'pixian', 'puer', 'putian', 'puyang', 'qd', 'qiandongnan', 'qianjiang',
'qiannan', 'qianxinan', 'qingyang', 'qingyuan', 'qinhuangdao', 'qinzhou', 'qh', 'qiqihaer', 'qitaihe',
'quanzhou', 'qujing', 'quzhou', 'rikaze', 'rizhao', 'sh', 'sy', 'sz', 'sjz', 'su', 'sanmenxia',
'sanming', 'sanya', 'shangluo', 'shangqiu', 'shangrao', 'shannan', 'shantou', 'shanwei', 'shaoguan',
'shaoxing', 'shaoyang', 'shennongjia', 'shihezi', 'shiyan', 'shizuishan', 'shuangliu', 'shuangyashan',
'shuozhou', 'siping', 'songyuan', 'suihua', 'suining', 'suizhou', 'suqian', 'ahsuzhou', 'ty', 'tj',
'jstaizhou', 'tacheng', 'taian', 'tangshan', 'tianmen', 'tianshui', 'tieling', 'tongchuan', 'tonghua',
'tongliao', 'tongling', 'tongren', 'tulufan', 'tumushuke', 'zjtaizhou', 'weifang', 'wei', 'wh', 'xj',
'wx', 'weinan', 'wenshan', 'wenzhou', 'wuhai', 'wuhu', 'wujiaqu', 'wulanchabu', 'wuwei', 'wuzhishan',
'wuzhong', 'wuzhou', 'xm', 'xa', 'xianggang', 'xiangtan', 'xiangxi', 'xiangyang', 'xianning',
'xiantao', 'xianyang', 'xiaogan', 'xilinguole', 'xingan', 'xingtai', 'xn', 'xinxiang', 'xinyang',
'xinyu', 'xinzhou', 'xishuangbanna', 'xuancheng', 'xuchang', 'xuzhou', 'yantai', 'jxyichun', 'sxyulin',
'yaan', 'yanan', 'yanbian', 'yancheng', 'yangjiang', 'yangquan', 'yangzhou', 'yibin', 'yichang',
'yili', 'yc', 'yingkou', 'yingtan', 'yiwu', 'yiyang', 'yongzhou', 'yueyang', 'gxyulin', 'yuncheng',
'yunfu', 'yushu', 'yuxi', 'zz', 'zibo', 'zaozhuang', 'zhangjiajie', 'zhangjiakou', 'zhangye',
'zhangzhou', 'zhanjiang', 'zhaoqing', 'zhaotong', 'zhenjiang', 'zhongshan', 'zhongwei', 'zhoukou',
'zhoushan', 'zhuhai', 'zhumadian', 'zhuzhou', 'zigong', 'ziyang', 'zunyi', 'quanguo']
return codes
def ganji_city_codes(self):
"""
赶集网的城市代码
:return:
"""
codes = ['anshan', 'anyang', 'anqing', 'ankang', 'akesu', 'anshun', 'aletai', 'alashan', 'aba', 'ali', 'alaer',
'aomen', 'bj', 'baoding', 'binzhou', 'baotou', 'baoji', 'benxi', 'bengbu', 'beihai', 'bayannaoer',
'baicheng', 'baishan', 'bozhou', 'bazhong', 'baiyin', 'baise', 'bijie', 'bayinguoleng', 'baoshan',
'boertala', 'cd', 'cq', 'cs', 'cc', 'changzhou', 'cangzhou', 'chifeng', 'chengde', 'changde',
'changzhi', 'chenzhou', 'chuzhou', 'chaohu', 'chaozhou', 'changji', 'chizhou', 'chuxiong', 'chongzuo',
'changdu', 'chaoyang', 'changshu', 'cixi', 'dl', 'dg', 'dezhou', 'dongying', 'daqing', 'datong',
'dandong', 'danzhou', 'deyang', 'dazhou', 'dali', 'daxinganling', 'dingxi', 'dehong', 'diqing',
'diaoyudao', 'eerduosi', 'enshi', 'ezhou', 'fz', 'foshan', 'fushun', 'fuyang', 'fuxin', 'jxfuzhou',
'fangchenggang', 'gz', 'gy', 'gl', 'ganzhou', 'guangyuan', 'guangan', 'guigang', 'guyuan', 'gannan',
'ganzi', 'guoluo', 'hz', 'huizhou', 'hrb', 'hf', 'nmg', 'hn', 'handan', 'heze', 'hengshui', 'huaian',
'hengyang', 'huludao', 'huainan', 'hanzhong', 'huaihua', 'huaibei', 'huanggang', 'huzhou', 'huangshi',
'hulunbeier', 'heyuan', 'hebi', 'hegang', 'huangshan', 'honghe', 'hechi', 'hami', 'heihe', 'hezhou',
'haixi', 'hetian', 'haibei', 'haidong', 'huangnan', 'jn', 'jining', 'jilin', 'jinzhou', 'jinhua',
'jiaxing', 'jiangmen', 'jingzhou', 'jiaozuo', 'jinzhong', 'jiamusi', 'jiujiang', 'jincheng', 'jingmen',
'jixi', 'jian', 'jieyang', 'jingdezhen', 'jiyuan', 'jiuquan', 'jinchang', 'jiayuguan', 'jiaozhou',
'jimo', 'km', 'kaifeng', 'kashi', 'kelamayi', 'kuerle', 'kezilesu', 'kunshan', 'lz', 'xz', 'langfang',
'linyi', 'luoyang', 'liaocheng', 'liuzhou', 'lianyungang', 'linfen', 'luohe', 'liaoyang', 'leshan',
'luzhou', 'luan', 'loudi', 'laiwu', 'longyan', 'lvliang', 'lishui', 'liangshan', 'lijiang',
'liupanshui', 'liaoyuan', 'laibin', 'lincang', 'longnan', 'linxia', 'linzhi', 'mianyang', 'mudanjiang',
'maoming', 'meizhou', 'maanshan', 'meishan', 'nj', 'nb', 'nn', 'nc', 'nantong', 'nanyang', 'nanchong',
'neijiang', 'nanping', 'ningde', 'nujiang', 'naqu', 'pingdingshan', 'puyang', 'panjin', 'putian',
'panzhihua', 'pingxiang', 'pingliang', 'puer', 'pixian', 'qd', 'qh', 'qinhuangdao', 'quanzhou',
'qiqihaer', 'qingyuan', 'qujing', 'quzhou', 'qingyang', 'qitaihe', 'qinzhou', 'qianjiang',
'qiandongnan', 'qiannan', 'qianxinan', 'rizhao', 'rikaze', 'sh', 'sz', 'sy', 'sjz', 'su', 'shantou',
'shangqiu', 'sanya', 'suqian', 'shaoxing', 'shiyan', 'siping', 'sanmenxia', 'shaoyang', 'shangrao',
'suining', 'sanming', 'suihua', 'shihezi', 'ahsuzhou', 'shaoguan', 'songyuan', 'suizhou', 'shanwei',
'shuangyashan', 'shuozhou', 'shizuishan', 'shangluo', 'shennongjia', 'shannan', 'shuangliu', 'tj',
'ty', 'tangshan', 'taian', 'zjtaizhou', 'jstaizhou', 'tieling', 'tongliao', 'tonghua', 'tianshui',
'tongling', 'tongchuan', 'tongren', 'tianmen', 'tacheng', 'tulufan', 'tumushuke', 'wh', 'wx', 'xj',
'wei', 'weifang', 'wenzhou', 'wuhu', 'weinan', 'wuhai', 'wuzhou', 'wulanchabu', 'wuwei', 'wenshan',
'wuzhong', 'wujiaqu', 'wuzhishan', 'xa', 'xm', 'xn', 'xuzhou', 'xianyang', 'xingtai', 'xiangyang',
'xinxiang', 'xiangtan', 'xuchang', 'xinyang', 'xiaogan', 'xinzhou', 'xianning', 'xinyu', 'xuancheng',
'xiantao', 'xilinguole', 'xiangxi', 'xingan', 'xishuangbanna', 'xianggang', 'yc', 'yichang', 'yantai',
'yangzhou', 'yancheng', 'yingkou', 'yueyang', 'yuncheng', 'sxyulin', 'yibin', 'yangquan', 'yanan',
'yiyang', 'yongzhou', 'gxyulin', 'jxyichun', 'yangjiang', 'yanbian', 'yuxi', 'yili', 'yunfu',
'hljyichun', 'yaan', 'yingtan', 'yushu', 'yiwu', 'zz', 'zhuhai', 'zibo', 'zhongshan', 'zaozhuang',
'zhangjiakou', 'zhuzhou', 'zhenjiang', 'zhoukou', 'zhanjiang', 'zhumadian', 'zhaoqing', 'zigong',
'zunyi', 'zhangzhou', 'zhoushan', 'zhangye', 'ziyang', 'zhangjiajie', 'zhaotong', 'zhongwei']
return codes
def get_ganji_search_args(self):
"""
返回赶集网普通类的搜索关键词
:return:
"""
args_dict = {'销售': 'shichangyingxiao', '技工工人': 'jigongyibangongren', '行政后勤': 'xingzhenghouqin',
'营业员': 'yingyeyuan', '财务': 'caiwushenji', '教师': 'jiaoshi', '保安': 'baoan', '淘宝职位': 'taobao',
'销售代表': 'xiaoshoudaibiao', '普工': 'xuetugong', '助理秘书': 'wenmiwenyuan', '收银员': 'shouyinyuan',
'会计': 'caikuai', '司机': 'siji', '保洁/清洁工': 'baojie', '快递员': 'kuaidi', '电话销售': 'dianhuaxiaoshou',
'操作工': 'caozuogong', '行政专员': 'xingzhengzhuli', '服务员': 'judianfuwuyuan', '美术创意': 'meishusheji',
'汽车制造服务': 'qiche', '保姆护工': 'baomu', '仓库管理员': 'cangkuguanli', '销售助理': 'xiaoshouzhuli',
'焊工': 'hangong', '人力资源': 'renliziyuan', '客服': 'kefu', '平面设计': 'pingmiansheji',
'房产经纪人': 'fdcjingjiren', '小时工': 'xiaoshigong', '美容/美发': 'meirongmeifa', '跟单员': 'gendanyuan',
'餐饮/酒店': 'jiudiancanyin', '人事专员': 'renshizhuli', '导购': 'daogou', '市场公关媒介': 'shichanggongguan',
'保险': 'baoxianjingjiren', '物业管理': 'wuyeguanlizhiwei'}
return args_dict
def get_58_city_code(self):
"""
获取58网的城市代码
:return:
"""
codes = ['hf', 'wuhu', 'bengbu', 'fy', 'hn', 'anqing', 'suzhou', 'la', 'huaibei', 'chuzhou', 'mas', 'tongling',
'xuancheng', 'bozhou', 'huangshan', 'chizhou', 'ch', 'hexian', 'hq', 'tongcheng', 'ningguo',
'tianchang', 'dongzhi', 'wuweixian', 'fz', 'xm', 'qz', 'pt', 'zhangzhou', 'nd', 'sm', 'np', 'ly',
'wuyishan', 'shishi', 'jinjiangshi', 'nananshi', 'longhai', 'shanghangxian', 'fuanshi', 'fudingshi',
'anxixian', 'yongchunxian', 'yongan', 'zhangpu', 'sz', 'gz', 'dg', 'fs', 'zs', 'zh', 'huizhou', 'jm',
'st', 'zhanjiang', 'zq', 'mm', 'jy', 'mz', 'qingyuan', 'yj', 'sg', 'heyuan', 'yf', 'sw', 'chaozhou',
'taishan', 'yangchun', 'sd', 'huidong', 'boluo', 'haifengxian', 'kaipingshi', 'lufengshi', 'nn',
'liuzhou', 'gl', 'yulin', 'wuzhou', 'bh', 'gg', 'qinzhou', 'baise', 'hc', 'lb', 'hezhou', 'fcg',
'chongzuo', 'guipingqu', 'beiliushi', 'bobaixian', 'cenxi', 'gy', 'zunyi', 'qdn', 'qn', 'lps', 'bijie',
'tr', 'anshun', 'qxn', 'renhuaishi', 'qingzhen', 'lz', 'tianshui', 'by', 'qingyang', 'pl', 'jq',
'zhangye', 'wuwei', 'dx', 'jinchang', 'ln', 'linxia', 'jyg', 'gn', 'dunhuang', 'haikou', 'sanya',
'wzs', 'sansha', 'qh', 'wenchang', 'wanning', 'tunchang', 'qiongzhong', 'lingshui', 'df', 'da', 'cm',
'baoting', 'baish', 'danzhou', 'zz', 'luoyang', 'xx', 'ny', 'xc', 'pds', 'ay', 'jiaozuo', 'sq',
'kaifeng', 'puyang', 'zk', 'xy', 'zmd', 'luohe', 'smx', 'hb', 'jiyuan', 'mg', 'yanling', 'yuzhou',
'changge', 'lingbaoshi', 'qixianqu', 'ruzhou', 'xiangchengshi', 'yanshiqu', 'changyuan', 'huaxian',
'linzhou', 'qinyang', 'mengzhou', 'wenxian', 'weishixian', 'lankaoxian', 'tongxuxian', 'lyxinan',
'yichuan', 'mengjinqu', 'lyyiyang', 'wugang', 'yongcheng', 'suixian', 'luyi', 'yingchixian', 'shenqiu',
'taikang', 'shangshui', 'qixianq', 'junxian', 'fanxian', 'gushixian', 'huaibinxian', 'dengzhou',
'xinye', 'hrb', 'dq', 'qqhr', 'mdj', 'suihua', 'jms', 'jixi', 'sys', 'hegang', 'heihe', 'yich', 'qth',
'dxal', 'shanda', 'shzhaodong', 'zhaozhou', 'wh', 'yc', 'xf', 'jingzhou', 'shiyan', 'hshi', 'xiaogan',
'hg', 'es', 'jingmen', 'xianning', 'ez', 'suizhou', 'qianjiang', 'tm', 'xiantao', 'snj', 'yidou',
'hanchuan', 'zaoyang', 'wuxueshi', 'zhongxiangshi', 'jingshanxian', 'shayangxian', 'songzi',
'guangshuishi', 'chibishi', 'laohekou', 'gucheng', 'yichengshi', 'nanzhang', 'yunmeng', 'anlu', 'dawu',
'xiaochang', 'dangyang', 'zhijiang', 'jiayuxian', 'suixia', 'cs', 'zhuzhou', 'yiyang', 'changde', 'hy',
'xiangtan', 'yy', 'chenzhou', 'shaoyang', 'hh', 'yongzhou', 'ld', 'xiangxi', 'zjj', 'liling', 'lixian',
'czguiyang', 'zixing', 'yongxing', 'changningshi', 'qidongxian', 'hengdong', 'lengshuijiangshi',
'lianyuanshi', 'shuangfengxian', 'shaoyangxian', 'shaodongxian', 'yuanjiangs', 'nanxian', 'qiyang',
'xiangyin', 'huarong', 'cilixian', 'zzyouxian', 'sjz', 'bd', 'ts', 'lf', 'hd', 'qhd', 'cangzhou', 'xt',
'hs', 'zjk', 'chengde', 'dingzhou', 'gt', 'zhangbei', 'zx', 'zd', 'qianan', 'renqiu', 'sanhe', 'wuan',
'xionganxinqu', 'lfyanjiao', 'zhuozhou', 'hejian', 'huanghua', 'cangxian', 'cixian', 'shexian',
'bazhou', 'xianghe', 'lfguan', 'zunhua', 'qianxixian', 'yutianxian', 'luannanxian', 'shaheshi', 'su',
'nj', 'wx', 'cz', 'xz', 'nt', 'yz', 'yancheng', 'ha', 'lyg', 'taizhou', 'suqian', 'zj', 'shuyang',
'dafeng', 'rugao', 'qidong', 'liyang', 'haimen', 'donghai', 'yangzhong', 'xinghuashi', 'xinyishi',
'taixing', 'rudong', 'pizhou', 'xzpeixian', 'jingjiang', 'jianhu', 'haian', 'dongtai', 'danyang',
'baoyingx', 'guannan', 'guanyun', 'jiangyan', 'jintan', 'szkunshan', 'sihong', 'siyang', 'jurong',
'sheyang', 'funingxian', 'xiangshui', 'xuyi', 'jinhu', 'nc', 'ganzhou', 'jj', 'yichun', 'ja', 'sr',
'px', 'fuzhou', 'jdz', 'xinyu', 'yingtan', 'yxx', 'lepingshi', 'jinxian', 'fenyi', 'fengchengshi',
'zhangshu', 'gaoan', 'yujiang', 'nanchengx', 'fuliangxian', 'cc', 'jl', 'sp', 'yanbian', 'songyuan',
'bc', 'th', 'baishan', 'liaoyuan', 'gongzhuling', 'meihekou', 'fuyuxian', 'changlingxian', 'huadian',
'panshi', 'lishu', 'sy', 'dl', 'as', 'jinzhou', 'fushun', 'yk', 'pj', 'cy', 'dandong', 'liaoyang',
'benxi', 'hld', 'tl', 'fx', 'pld', 'wfd', 'dengta', 'fengcheng', 'beipiao', 'kaiyuan', 'yinchuan',
'wuzhong', 'szs', 'zw', 'guyuan', 'hu', 'bt', 'chifeng', 'erds', 'tongliao', 'hlbe', 'bycem', 'wlcb',
'xl', 'xam', 'wuhai', 'alsm', 'hlr', 'xn', 'hx', 'haibei', 'guoluo', 'haidong', 'huangnan', 'ys',
'hainan', 'geermushi', 'qd', 'jn', 'yt', 'wf', 'linyi', 'zb', 'jining', 'ta', 'lc', 'weihai',
'zaozhuang', 'dz', 'rizhao', 'dy', 'heze', 'bz', 'lw', 'zhangqiu', 'kl', 'zc', 'shouguang', 'longkou',
'caoxian', 'shanxian', 'feicheng', 'gaomi', 'guangrao', 'huantaixian', 'juxian', 'laizhou', 'penglai',
'qingzhou', 'rongcheng', 'rushan', 'tengzhou', 'xintai', 'zhaoyuan', 'zoucheng', 'zouping', 'linqing',
'chiping', 'hzyc', 'boxing', 'dongming', 'juye', 'wudi', 'qihe', 'weishan', 'yuchengshi', 'linyixianq',
'leling', 'laiyang', 'ningjin', 'gaotang', 'shenxian', 'yanggu', 'guanxian', 'pingyi', 'tancheng',
'yiyuanxian', 'wenshang', 'liangshanx', 'lijin', 'yinanxian', 'qixia', 'ningyang', 'dongping',
'changyishi', 'anqiu', 'changle', 'linqu', 'juancheng', 'ty', 'linfen', 'dt', 'yuncheng', 'jz',
'changzhi', 'jincheng', 'yq', 'lvliang', 'xinzhou', 'shuozhou', 'linyixian', 'qingxu', 'liulin',
'gaoping', 'zezhou', 'xiangyuanxian', 'xiaoyi', 'xa', 'xianyang', 'baoji', 'wn', 'hanzhong', 'yl',
'yanan', 'ankang', 'sl', 'tc', 'shenmu', 'hancheng', 'fugu', 'jingbian', 'dingbian', 'cd', 'mianyang',
'deyang', 'nanchong', 'yb', 'zg', 'ls', 'luzhou', 'dazhou', 'scnj', 'suining', 'panzhihua', 'ms', 'ga',
'zy', 'liangshan', 'guangyuan', 'ya', 'bazhong', 'ab', 'ganzi', 'anyuexian', 'guanghanshi',
'jianyangshi', 'renshouxian', 'shehongxian', 'dazu', 'xuanhan', 'qux', 'changningx', 'xj', 'changji',
'bygl', 'yili', 'aks', 'ks', 'hami', 'klmy', 'betl', 'tlf', 'ht', 'shz', 'kzls', 'ale', 'wjq', 'tmsk',
'kel', 'alt', 'tac', 'lasa', 'rkz', 'sn', 'linzhi', 'changdu', 'nq', 'al', 'rituxian', 'gaizexian',
'km', 'qj', 'dali', 'honghe', 'yx', 'lj', 'ws', 'cx', 'bn', 'zt', 'dh', 'pe', 'bs', 'lincang',
'diqing', 'nujiang', 'milexian', 'anningshi', 'xuanwushi', 'hz', 'nb', 'wz', 'jh', 'jx', 'tz', 'sx',
'huzhou', 'lishui', 'quzhou', 'zhoushan', 'yueqingcity', 'ruiancity', 'yiwu', 'yuyao', 'zhuji',
'xiangshanxian', 'wenling', 'tongxiang', 'cixi', 'changxing', 'jiashanx', 'haining', 'deqing',
'dongyang', 'anji', 'cangnanxian', 'linhai', 'yongkang', 'yuhuan', 'pinghushi', 'haiyan', 'wuyix',
'shengzhou', 'xinchang', 'jiangshanshi', 'pingyangxian', 'hk', 'am', 'tw', 'quanguo', 'cn',
'gllosangeles', 'glsanfrancisco', 'glnewyork', 'gltoronto', 'glvancouver', 'glgreaterlondon',
'glmoscow', 'glseoul', 'gltokyo', 'glsingapore', 'glbangkok', 'glchiangmai', 'gldubai', 'glauckland',
'glsydney', 'glmelbourne']
return codes
def get_chinahr_city_code(self):
"""
获取中华英才网的城市代码
:return:
"""
codes = ['bj', 'sh', 'tj', 'cq', 'hf', 'wuhu', 'bengbu', 'fy', 'hn', 'anqing', 'suzhou', 'la', 'huaibei',
'chuzhou', 'mas', 'tongling', 'xuancheng', 'bozhou', 'huangshan', 'chizhou', 'ch', 'hexian', 'hq',
'tongcheng', 'ningguo', 'tianchang', 'dongzhi', 'wuweixian', 'fz', 'xm', 'qz', 'pt', 'zhangzhou', 'nd',
'sm', 'np', 'ly', 'wuyishan', 'shishi', 'jinjiangshi', 'nananshi', 'longhai', 'shanghangxian',
'fuanshi', 'fudingshi', 'anxixian', 'yongchunxian', 'yongan', 'zhangpu', 'sz', 'gz', 'dg', 'fs', 'zs',
'zh', 'huizhou', 'jm', 'st', 'zhanjiang', 'zq', 'mm', 'jy', 'mz', 'qingyuan', 'yj', 'sg', 'heyuan',
'yf', 'sw', 'chaozhou', 'taishan', 'yangchun', 'sd', 'huidong', 'boluo', 'haifengxian', 'kaipingshi',
'lufengshi', 'nn', 'liuzhou', 'gl', 'yulin', 'wuzhou', 'bh', 'gg', 'qinzhou', 'baise', 'hc', 'lb',
'hezhou', 'fcg', 'chongzuo', 'guipingqu', 'beiliushi', 'bobaixian', 'cenxi', 'gy', 'zunyi', 'qdn',
'qn', 'lps', 'bijie', 'tr', 'anshun', 'qxn', 'renhuaishi', 'qingzhen', 'lz', 'tianshui', 'by',
'qingyang', 'pl', 'jq', 'zhangye', 'wuwei', 'dx', 'jinchang', 'ln', 'linxia', 'jyg', 'gn', 'dunhuang',
'haikou', 'sanya', 'wzs', 'sansha', 'qh', 'wenchang', 'wanning', 'tunchang', 'qiongzhong', 'lingshui',
'df', 'da', 'cm', 'baoting', 'baish', 'danzhou', 'zz', 'luoyang', 'xx', 'ny', 'xc', 'pds', 'ay',
'jiaozuo', 'sq', 'kaifeng', 'puyang', 'zk', 'xy', 'zmd', 'luohe', 'smx', 'hb', 'jiyuan', 'mg',
'yanling', 'yuzhou', 'changge', 'lingbaoshi', 'qixianqu', 'ruzhou', 'xiangchengshi', 'yanshiqu',
'changyuan', 'huaxian', 'linzhou', 'qinyang', 'mengzhou', 'wenxian', 'weishixian', 'lankaoxian',
'tongxuxian', 'lyxinan', 'yichuan', 'mengjinqu', 'lyyiyang', 'wugang', 'yongcheng', 'suixian', 'luyi',
'yingchixian', 'shenqiu', 'taikang', 'shangshui', 'qixianq', 'junxian', 'fanxian', 'gushixian',
'huaibinxian', 'dengzhou', 'xinye', 'hrb', 'dq', 'qqhr', 'mdj', 'suihua', 'jms', 'jixi', 'sys',
'hegang', 'heihe', 'yich', 'qth', 'dxal', 'shanda', 'shzhaodong', 'zhaozhou', 'wh', 'yc', 'xf',
'jingzhou', 'shiyan', 'hshi', 'xiaogan', 'hg', 'es', 'jingmen', 'xianning', 'ez', 'suizhou',
'qianjiang', 'tm', 'xiantao', 'snj', 'yidou', 'hanchuan', 'zaoyang', 'wuxueshi', 'zhongxiangshi',
'jingshanxian', 'shayangxian', 'songzi', 'guangshuishi', 'chibishi', 'laohekou', 'gucheng',
'yichengshi', 'nanzhang', 'yunmeng', 'anlu', 'dawu', 'xiaochang', 'dangyang', 'zhijiang', 'jiayuxian',
'suixia', 'cs', 'zhuzhou', 'yiyang', 'changde', 'hy', 'xiangtan', 'yy', 'chenzhou', 'shaoyang', 'hh',
'yongzhou', 'ld', 'xiangxi', 'zjj', 'liling', 'lixian', 'czguiyang', 'zixing', 'yongxing',
'changningshi', 'qidongxian', 'hengdong', 'lengshuijiangshi', 'lianyuanshi', 'shuangfengxian',
'shaoyangxian', 'shaodongxian', 'yuanjiangs', 'nanxian', 'qiyang', 'xiangyin', 'huarong', 'cilixian',
'zzyouxian', 'sjz', 'bd', 'ts', 'lf', 'hd', 'qhd', 'cangzhou', 'xt', 'hs', 'zjk', 'chengde',
'dingzhou', 'gt', 'zhangbei', 'zx', 'zd', 'qianan', 'renqiu', 'sanhe', 'wuan', 'xionganxinqu',
'lfyanjiao', 'zhuozhou', 'hejian', 'huanghua', 'cangxian', 'cixian', 'shexian', 'bazhou', 'xianghe',
'lfguan', 'zunhua', 'qianxixian', 'yutianxian', 'luannanxian', 'shaheshi', 'su', 'nj', 'wx', 'cz',
'xz', 'nt', 'yz', 'yancheng', 'ha', 'lyg', 'taizhou', 'suqian', 'zj', 'shuyang', 'dafeng', 'rugao',
'qidong', 'liyang', 'haimen', 'donghai', 'yangzhong', 'xinghuashi', 'xinyishi', 'taixing', 'rudong',
'pizhou', 'xzpeixian', 'jingjiang', 'jianhu', 'haian', 'dongtai', 'danyang', 'baoyingx', 'guannan',
'guanyun', 'jiangyan', 'jintan', 'szkunshan', 'sihong', 'siyang', 'jurong', 'sheyang', 'funingxian',
'xiangshui', 'xuyi', 'jinhu', 'nc', 'ganzhou', 'jj', 'yichun', 'ja', 'sr', 'px', 'fuzhou', 'jdz',
'xinyu', 'yingtan', 'yxx', 'lepingshi', 'jinxian', 'fenyi', 'fengchengshi', 'zhangshu', 'gaoan',
'yujiang', 'nanchengx', 'fuliangxian', 'cc', 'jl', 'sp', 'yanbian', 'songyuan', 'bc', 'th', 'baishan',
'liaoyuan', 'gongzhuling', 'meihekou', 'fuyuxian', 'changlingxian', 'huadian', 'panshi', 'lishu', 'sy',
'dl', 'as', 'jinzhou', 'fushun', 'yk', 'pj', 'cy', 'dandong', 'liaoyang', 'benxi', 'hld', 'tl', 'fx',
'pld', 'wfd', 'dengta', 'fengcheng', 'beipiao', 'kaiyuan', 'yinchuan', 'wuzhong', 'szs', 'zw',
'guyuan', 'hu', 'bt', 'chifeng', 'erds', 'tongliao', 'hlbe', 'bycem', 'wlcb', 'xl', 'xam', 'wuhai',
'alsm', 'hlr', 'xn', 'hx', 'haibei', 'guoluo', 'haidong', 'huangnan', 'ys', 'hainan', 'geermushi',
'qd', 'jn', 'yt', 'wf', 'linyi', 'zb', 'jining', 'ta', 'lc', 'weihai', 'zaozhuang', 'dz', 'rizhao',
'dy', 'heze', 'bz', 'lw', 'zhangqiu', 'kl', 'zc', 'shouguang', 'longkou', 'caoxian', 'shanxian',
'feicheng', 'gaomi', 'guangrao', 'huantaixian', 'juxian', 'laizhou', 'penglai', 'qingzhou',
'rongcheng', 'rushan', 'tengzhou', 'xintai', 'zhaoyuan', 'zoucheng', 'zouping', 'linqing', 'chiping',
'hzyc', 'boxing', 'dongming', 'juye', 'wudi', 'qihe', 'weishan', 'yuchengshi', 'linyixianq', 'leling',
'laiyang', 'ningjin', 'gaotang', 'shenxian', 'yanggu', 'guanxian', 'pingyi', 'tancheng', 'yiyuanxian',
'wenshang', 'liangshanx', 'lijin', 'yinanxian', 'qixia', 'ningyang', 'dongping', 'changyishi', 'anqiu',
'changle', 'linqu', 'juancheng', 'ty', 'linfen', 'dt', 'yuncheng', 'jz', 'changzhi', 'jincheng', 'yq',
'lvliang', 'xinzhou', 'shuozhou', 'linyixian', 'qingxu', 'liulin', 'gaoping', 'zezhou',
'xiangyuanxian', 'xiaoyi', 'xa', 'xianyang', 'baoji', 'wn', 'hanzhong', 'yl', 'yanan', 'ankang', 'sl',
'tc', 'shenmu', 'hancheng', 'fugu', 'jingbian', 'dingbian', 'cd', 'mianyang', 'deyang', 'nanchong',
'yb', 'zg', 'ls', 'luzhou', 'dazhou', 'scnj', 'suining', 'panzhihua', 'ms', 'ga', 'zy', 'liangshan',
'guangyuan', 'ya', 'bazhong', 'ab', 'ganzi', 'anyuexian', 'guanghanshi', 'jianyangshi', 'renshouxian',
'shehongxian', 'dazu', 'xuanhan', 'qux', 'changningx', 'xj', 'changji', 'bygl', 'yili', 'aks', 'ks',
'hami', 'klmy', 'betl', 'tlf', 'ht', 'shz', 'kzls', 'ale', 'wjq', 'tmsk', 'kel', 'alt', 'tac', 'lasa',
'rkz', 'sn', 'linzhi', 'changdu', 'nq', 'al', 'rituxian', 'gaizexian', 'km', 'qj', 'dali', 'honghe',
'yx', 'lj', 'ws', 'cx', 'bn', 'zt', 'dh', 'pe', 'bs', 'lincang', 'diqing', 'nujiang', 'milexian',
'anningshi', 'xuanwushi', 'hz', 'nb', 'wz', 'jh', 'jx', 'tz', 'sx', 'huzhou', 'lishui', 'quzhou',
'zhoushan', 'yueqingcity', 'ruiancity', 'yiwu', 'yuyao', 'zhuji', 'xiangshanxian', 'wenling',
'tongxiang', 'cixi', 'changxing', 'jiashanx', 'haining', 'deqing', 'dongyang', 'anji', 'cangnanxian',
'linhai', 'yongkang', 'yuhuan', 'pinghushi', 'haiyan', 'wuyix', 'shengzhou', 'xinchang',
'jiangshanshi', 'pingyangxian', 'hk', 'am', 'tw']
return codes
def get_gzc_city_code(self):
"""
获取工作虫网站的城市代码
:return:
"""
codes = ['bj', 'sh', 'gz', 'sz', 'tj', 'cq', 'wh', 'cd', 'hz', 'zz', 'jn', 'xa', 'dl', 'fz', 'nn', 'dg', 'xm',
'wx', 'qd', 'ty', 'sjz', 'nj', 'hf', 'su', 'cc', 'cs', 'hrb', 'sy', 'jincheng', 'hu', 'tangshan',
'qinhuangdao', 'handan', 'xingtai', 'zjk', 'chengde', 'cangzhou', 'langfang', 'hengshui', 'datong',
'changzhi', 'jingzhong', 'yuncheng', 'linfen', 'bd', 'cz', 'xz', 'nt', 'nb', 'wz', 'yz', 'jh', 'fs',
'zs', 'zh', 'huizhou', 'qz', 'nc', 'gy', 'mianyang', 'km', 'luoyang', 'xx', 'ny', 'linyi', 'dz', 'wf',
'lz', 'sx', 'tz', 'jx', 'jm', 'st', 'zhanjiang', 'haikou', 'sanya', 'liuzhou', 'gl', 'deyang', 'lasa',
'xc', 'ay', 'jiaozuo', 'sq', 'kaifeng', 'zk', 'yt', 'zb', 'jining', 'dy', 'wuhu', 'bengbu', 'fy', 'yc',
'xy', 'yy', 'changde', 'hy', 'xj', 'xn', 'yinchuan', 'bt', 'chifeng', 'pingxiang']
return codes
def get_baidu_city_code(self):
"""
获取百度百聘的城市代码
:return:
"""
codes = ['北京', '天津', '太原', '大同', '呼和浩特', '包头', '石家庄', '廊坊', '邯郸',
'上海', '杭州', '宁波', '温州', '南京', '济南', '青岛', '台州', '嘉兴', '金华', '绍兴', '苏州', '无锡', '常州', '南通', '扬州', '徐州',
'连云港', '福州', '厦门', '泉州', '烟台', '潍坊', '临沂', '淄博', '菏泽', '威海', '合肥', '马鞍山', '芜湖',
'广州', '深圳', '海口', '三亚', '南宁', '桂林', '玉林', '百色',
'武汉', '南昌', '郑州', '长沙', '九江', '赣州', '株洲', '常德', '宜昌', '十堰', '荆州', '洛阳', '南阳', '新乡', '安阳',
'重庆', '成都', '绵阳', '贵阳', '遵义', '昆明', '大理', '拉萨',
'西安', '宝鸡', '西宁', '银川', '兰州', '咸阳', '天水', '乌鲁木齐', '昌吉', '固原',
'沈阳', '大连', '哈尔滨', '长春', '吉林', '朝阳', '锦州', '四平', '大庆', '牡丹江', ]
return codes
def get_zhilian_city_code(self):
"""
获取智联招聘的城市代码
:return:
"""
return ['530', '538', '763', '531', '801', '653', '736', '600', '613', '635', '702', '703', '639',
'599', '854', '719', '749', '551', '622', '636', '654', '681', '682', '565', '664', '773']
def handle_time(self, timestamp):
"""
处理时间格式,返回一个日期格式
:param timestamp:
:return:
"""
date = str(timestamp)
while date.endswith('0'):
date = date[:-1]
new_date = int(date)
temp_time = time.localtime(new_date)
# format_date = time.strftime("%Y-%m-%d %H:%M:%S", temp_time)
format_date = time.strftime("%Y-%m-%d", temp_time)
return format_date
def get_proxy(self):
"""
获取代理
:return:
"""
if self.proxy_list:
proxy = random.choice(self.proxy_list)
return proxy
def get_session(self, url_name, url=None):
"""
获取session对象
:param url_name: url别名
:param url: 请求的url
:return:
"""
session = requests.session()
retry = Retry(connect=5, backoff_factor=1)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
session.keep_alive = False
if not url:
if 'parser_baidu' == url_name:
session.get(self.BAIDU_COOKIE_URL, headers=self.header, timeout=(3, 7))
elif 'parser_baidu_jianzhi' == url_name:
session.get(self.BAIDU_COOKIE_JIANZHI_URL, headers=self.header, timeout=(3, 7))
else:
session.get(url, headers=self.header, timeout=(3, 7))
return session
def get_baidu_token(self, token_url, url_name):
"""
获取百度百聘的api所需的token数据
:param token_url: 获取token的url
:param url_name: url别名
:return:
"""
if token_url:
session = self.get_session(url_name)
token = ''
res = ''
try:
proxy = self.get_proxy()
req = session.get(token_url, headers=self.header, proxies=proxy, verify=False, timeout=(3, 7))
res = req.content.decode('utf-8')
session.keep_alive = False
session.close()
except BaseException as e:
time.sleep(3)
gevent.sleep(3)
try:
proxy = self.get_proxy()
req = session.get(token_url, headers=self.header, proxies=proxy, verify=False, timeout=(3, 7))
res = req.content.decode('utf-8')
session.keep_alive = False
session.close()
except BaseException as e:
# print('触发百度反爬侦测')
CRAWL_LOG.error('Hit by Baidu anti-crawl policy')
if res:
if 'parser_baidu' == url_name:
token = re.findall(r'"nekot"] = "(.*)";', res)
token = token[0][::-1] if token else ''
elif 'parser_baidu_jianzhi' == url_name:
token = re.findall(r"zp_pc_nekot = '(.*)';", res)
token = token[0][::-1] if token else ''
if token:
new_token = urllib.parse.quote(token)
return new_token
@property
def get_header(self):
"""
获取请求头
:return:
"""
return self.request_headers()
@property
def get_request_urls(self):
"""
获取所有的待爬取的网址,格式化url的参数
:return:
"""
urls = []
for item in REQEUST_URLS:
self.set_urls_flag(item['type'])
item['type'] = 'parser_' + item['type']
urls.append(item)
# 乱序
random.shuffle(urls)
return urls
def get_already_crawl_site(self):
"""
已有的招聘网站域名
:return:
"""
sites = []
for item in REQEUST_URLS:
temp_url = item.get('url').split('/')
url = temp_url[2].split('.', 1)[1]
sites.append(url)
return sites
def request_site(self):
"""
遍历请求所有的url
:return:
"""
for args in self.search_args:
args_urlencode = urllib.parse.quote(args)
for urls in self.request_urls:
if self.is_end:
CRAWL_LOG.info('all target urls request finished')
return self.jobs
else:
url = urls.get('url')
url_name = urls.get('type')
domain = url_name.split('_', 1)[1]
print('正在爬取 %s 网站的 %s 相关岗位的数据' % (domain, args))
self.request_url(url, url_name, args_urlencode, args)
def get_cookie(self, url, url_name=None, args=None):
"""
访问对应网页获取cookie
:param url: 指定网页url
:param url_name: 目标网址别名
:param args: 搜索关键词
:return:
"""
req = requests.session()
headers = self.header
if url_name and 'dajie' in url_name:
headers.update({
'referer': 'https://so.dajie.com/job/search?keyword={q}'.format(q=args),
'dnt': '1',
'accept-language': 'zh-CN,zh;q=0.9',
})
dajie_url = 'https://so.dajie.com/job/search?keyword={q}&from=job&clicktype=blank'.format(q=args)
req.get(dajie_url, headers=headers, timeout=(3, 7))
elif url_name and 'chinahr_old' in url_name:
headers.update({
'Host': 'www.chinahr.com',
'Referer': 'http://www.chinahr.com/jobs/',
})
req.get(url, headers=headers, timeout=(3, 7))
else:
headers.update({
'Referer': url,
})
req.get(url, headers=headers, timeout=(3, 7))
cookie = req.cookies
req.keep_alive = False
req.close()
return cookie
def get_city_codes(self, url_name):
"""
获取城市代码
:param url_name: url别名
:return:
"""
city_dict = {
'parser_zhilian': self.get_zhilian_city_code,
'parser_ganji': self.ganji_city_codes,
'parser_ganji_it': self.ganji_city_codes,
'parser_58': self.get_58_city_code,
'parser_chinahr': self.get_chinahr_city_code,
'parser_gongzuochong': self.get_gzc_city_code,
'parser_baidu': self.get_baidu_city_code,
'parser_baidu_jianzhi': self.get_baidu_city_code,
'parser_doumi': self.get_doumi_city_codes,
}
func = city_dict.get(url_name)
if not func:
city_codes = []
else:
city_codes = func()
return city_codes
def get_scheduler(self):
cls = BlockingScheduler
return cls()
def request_url(self, url, url_name, args_urlencode=None, args=None):
"""
请求对应的网址
:param url:url
:param url_name:url别名
:param args_urlencode: 已编码的搜索职位名参数
:param args: 未编码的搜索职位名参数
:return:
"""
if not self.is_end:
# 索引字段
index = args
if not args_urlencode:
args_urlencode = urllib.parse.quote('python')
# 获取城市代码
city_codes = self.get_city_codes(url_name)
# 如果有城市代码
if city_codes:
for city_code in city_codes:
self.enumerate_request_urls(url, url_name, args, args_urlencode, index, city_code)
else:
self.enumerate_request_urls(url, url_name, args, args_urlencode, index)
def enumerate_request_urls(self, url, url_name, args, args_urlencode, index, city_code=None):
"""
枚举所有的url
:param url: 待请求为格式化的url
:param url_name: url的别名
:param args: 搜索关键词
:param args_urlencode: 已编码的搜索关键词
:param index: 索引
:param city_code: 城市代码,如果为空则表示为全国地区
:return:
"""
# 如果数据库内无值,生成值并存入数据库(第一次运行程序)
if not self.target_urls:
# 生成所有的目标url
target_urls, target_urls_dict = self.generate_reqeust_target_urls(url, url_name, city_code, args,
args_urlencode, index, is_generate=True)
if not target_urls:
self.is_end = True
print('请配置需要爬取的网站,再运行程序')
return
# 保存所有的目标url,这里如果是协程或者多线程,可能会导致页码和真实情况有少量偏移,正常现象
else:
self.target_urls = target_urls
self.target_urls_dict = target_urls_dict
save_url_redis(target_urls)
# 开始遍历请求
self.generate_reqeust_target_urls(url, url_name, city_code, args, args_urlencode, index)
# 如果数据库有值(第二次及之后的运行程序)
else:
# 生成所有的目标url
target_urls, self.target_urls_dict = self.generate_reqeust_target_urls(url, url_name, city_code, args,
args_urlencode, index,
is_generate=True)
target_urls = target_urls if target_urls else set()
market_urls = get_url_redis('market_urls')
market_urls = market_urls if market_urls else set()
invalid_urls = get_url_redis('invalid_urls')
invalid_urls = invalid_urls if invalid_urls else set()
# 比对数据,得到最新的待爬取的目标url
if target_urls: # 如果有最新的目标url
temp_compare_url = self.target_urls
temp_compare_url.update(market_urls, invalid_urls)
if target_urls != temp_compare_url: # 数据库内的目标url不完整(上次启动意外终止;有新增的网站)
self.target_urls = target_urls - invalid_urls
# 如果没有最新的目标url
self.target_urls -= invalid_urls
market_urls -= invalid_urls
self.target_urls -= market_urls
if not self.target_urls: # 已爬取完毕
# 立即保存已爬到的数据
save_url_redis(self.jobs)
self.is_end = True
CRAWL_LOG.info('all target urls request finished')
return
elif not self.is_end:
self.flush_target_urls_dict()
save_url_redis(self.target_urls)
# 开始遍历请求
self.generate_reqeust_target_urls(url, url_name, city_code, args, args_urlencode, index)
def generate_reqeust_invalid_urls(self, url, city_code, url_name, args, args_urlencode, page=None):
"""
生成无效的url
:param url: url
:param city_code: 城市代码
:param url_name: url别名
:param args: 搜索关键词
:param args_urlencode: 已url编码的搜索关键词
:param page: 页码
:return:
"""
invalid_target_urls = set()
if not page:
page = 0
for i in range(page, END_PAGE):
end_url, end_url_dict = self.distribute_urls(url, url_name, city_code, args, args_urlencode, i, set(),
dict())
if end_url:
invalid_target_urls.update(end_url)
return invalid_target_urls
def generate_reqeust_target_urls(self, url, url_name, city_code, args, args_urlencode, index, is_generate=False):
"""
生成所有的url和遍历请求
:param url: 待爬取的url
:param url_name: url的别名
:param city_code: 城市代码
:param args: 关键词
:param args_urlencode: 已url编码好的关键词
:param index: 索引
:param is_generate: 是否是生成所有的目标url
:return: 返回所有的待爬取的目标url
"""
db_page = get_market_page_redis()
page = db_page if db_page else 0
if page >= END_PAGE:
print('设置有误!数据库内的页码不能大于等于配置文件的页码,请重新设置再运行程序')
# 生成目标url
if is_generate:
target_urls = set()
target_urls_dict = dict()
for i in range(page, END_PAGE): # 页码总数随意,但一般情况下每个网站搜出来的职位最多就100页左右
# 生成
temp_urls, temp_urls_dict = self.distribute_urls(url, url_name, city_code, args, args_urlencode, i,
set(), dict())
target_urls.update(temp_urls)
target_urls_dict.update(temp_urls_dict)
return target_urls, target_urls_dict
# 请求目标url
# target_urls_dict生成的是不同网站的url,因为url不固定,所以是{url:url_name}的格式
elif self.target_urls_dict:
self.request_format_site(url_name, args, args_urlencode, index, page=page)
def request_format_site(self, url_name, args, args_urlencode, index, page=None):
"""
分发请求url
:param url_name: url别名
:param page: 页码
:param args: 搜索关键词
:param args_urlencode: 已作url编码的搜索关键词
:param index: 索引
:param city_code: 城市代码
:return:
"""
for current_url in list(self.target_urls_dict):
# 获取最新待爬取的url
if self.invalid_urls:
self.target_urls -= self.invalid_urls
self.flush_target_urls_dict()
url_name = self.target_urls_dict.get(current_url)
if url_name:
city_codes = self.get_city_codes(url_name) # url_name 改变,city_code也要跟着改变
if city_codes:
for city_code in city_codes:
self.distribute_request_url(current_url, url_name, args, args_urlencode, index, page, city_code)
else:
self.distribute_request_url(current_url, url_name, args, args_urlencode, index, page)
def flush_target_urls_dict(self):
"""
刷新目标url字典
:return:
"""
key = list(self.target_urls_dict.keys())
for k in key:
if k not in self.target_urls:
self.target_urls_dict.pop(k)
def distribute_urls(self, url, url_name, city_code, args, args_urlencode, i, target_urls, target_urls_dict):
"""
分发生成url
:param url: url
:param url_name: url别名
:param city_code: 城市代码
:param args: 搜索关键词
:param args_urlencode: 已url编码的搜索关键词
:param i: 页码
:param target_urls: 目标url集合
:param target_urls_dict: 目标url字典
:return:
"""
temp_url = ''
if 'zhilian' in url_name:
i *= 90
temp_url = url.format(c=city_code, p=i, q=args_urlencode)
elif 'parser_ganji' == url_name:
ganji_args = self.get_ganji_search_args()
if args in ganji_args:
args_value = ganji_args.get(args)
temp_url = url.format(c=city_code, p=i, q=args_value)
elif 'parser_ganji_it' == url_name:
i = (i - 1) * 32
temp_url = url.format(c=city_code, p=i, q=args_urlencode)
elif url_name in {'parser_58', 'parser_gongzuochong', 'parser_chinahr'}:
temp_url = url.format(c=city_code, p=i, q=args_urlencode)
elif 'baidu' in url_name:
city_code1 = urllib.parse.quote(city_code)
city_code2 = urllib.parse.quote(city_code1)
if 'parser_baidu' == url_name:
# 百度百聘的api把城市参数做了两层url编码
token_url = 'https://zhaopin.baidu.com/quanzhi?city={c}&query={q}'.format(c=city_code1,
q=args_urlencode)
elif 'parser_baidu_jianzhi' == url_name:
# 百度百聘的api把城市参数做了两层url编码
token_url = 'https://zhaopin.baidu.com/jianzhi?city={c}&query={q}'.format(c=city_code1,
q=args_urlencode)
else:
token_url = ''
token = self.get_baidu_token(token_url, url_name)
if token:
if i == 1:
i -= 1
i *= 20
temp_url = url.format(c=city_code2, p=i, q=args_urlencode, token=token)
elif 'yjs' in url_name:
i -= 1
if i < 0:
i = 0
i *= 10
temp_url = url.format(p=i, q=args_urlencode)
elif 'jobcn' in url_name:
temp_url = url
elif url_name in {'parser_jiaoshizhaopin', 'parser_telecomhr'}:
args = urllib.parse.quote(args, encoding='gb2312')
temp_url = url.format(p=i, q=args)
elif 'shuobo' in url_name and i == 1:
'其他条件已经由else接收'
temp_url = 'http://www.51shuobo.com/s/result/kt1_kw-{q}/'.format(q=args_urlencode)
elif url_name in {'parser_liepin', 'parser_job1001'}:
i -= 1
temp_url = url.format(p=i, q=args_urlencode)
elif 'linkin' in url_name:
city = '中国'
c = urllib.parse.quote(city)
if i == 1:
i = 0
i *= 25
temp_url = url.format(c=c, p=i, q=args_urlencode)
else:
temp_url = url.format(p=i, q=args_urlencode)
if temp_url:
target_urls.add(temp_url) # 不能用update
target_urls_dict.update({temp_url: url_name})
return target_urls, target_urls_dict
else:
return set(), set()
def distribute_request_url(self, target_url, url_name, args, args_urlencode, index, page, city_code=None):
"""
请求格式化好的所有url
:param target_url: 目标url
:param url_name: url别名
:param proxy: 代理
:param args: 搜索关键词
:param args_urlencode: url编码后后的搜索关键词
:param index: 搜索关键词
:param page: 已爬取的页码
:param city_code: 城市代码
:return:
"""
proxy = None
domain = url_name.split('_')[1]
for i in range(page, END_PAGE): # 页码总数随意,但一般情况下每个网站搜出来的职位最多就100页左右
# 当标志位为真,即标志该站某一页已经没有数据,该网站停止爬取,终止循环
print('正在爬取网站 %s 的 %s 岗位的 第 %s 页数据' % (domain, args, i))
flag = self.get_urls_flag(url_name)
if flag:
# print('该链接下已无 %s 相关数据,已切换到其他url继续爬取.....' % index)
# 删除对应的无效url
invalid_url = self.generate_reqeust_invalid_urls(target_url, city_code, url_name, args,
args_urlencode, page)
self.invalid_urls.update(invalid_url)
if self.invalid_urls:
self.target_urls -= self.invalid_urls
save_url_redis(self.invalid_urls, 'invalid_urls')
target_urls = self.target_urls
cover_url_redis(target_urls) # 覆盖存储
break
try:
if self.proxy_list:
proxy = self.get_proxy()
timer = self.get_random_time()
time.sleep(timer)
gevent.sleep(timer)
self.request_format_url(target_url, url_name, proxy, args_urlencode, i, index, city_code)
except BaseException as e:
time.sleep(2)
gevent.sleep(2)
if self.proxy_list:
if proxy in self.proxy_list:
self.proxy_list.remove(proxy)
proxy = self.get_proxy()
try:
self.request_format_url(target_url, url_name, proxy, args_urlencode, i, index, city_code)
except BaseException as e:
save_redis(self.jobs)
CRAWL_LOG.error('request exception occurred:%s' % e)
save_market_page_redis(i) # 保存已爬取的页码
def request_format_url(self, url, url_name, proxy, args_urlencode, i, index, city_code=None):
"""
请求已经格式化好的url
:param url: 格式化好的url
:param url_name: url别名
:param proxy: 代理
:param args_urlencode: 已编码的搜索职位名参数
:param i: 页码
:param index: 搜索的关键词
:param city_code: 城市代码
:return:
"""
# 中华英才网相关
chinahr_url = 'http://www.chinahr.com/jobs/'
# 卓聘相关
zhuopin_data = {
'CID': '',
'Q': args_urlencode,
'pageIndex': i,
'pageSize': '20',
'ReferrerType': '',
'qTitle': '1',
'JobLocation': '',
'CompanyIndustry': '',
'JobType': '',
'AnnualSalaryMin': '-1',
'AnnualSalaryMax': '-1',
'CompanyType': '',
'ReleaseDate': '',
'GID': '1e597c4a-7bae-483e-9807-41a583778abc'
}
# 拉钩相关
lagou_data = {