亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python手機號前7位歸屬地爬蟲代碼實例

瀏覽:116日期:2022-07-31 15:59:45

需求分析

項目上需要用到手機號前7位,判斷號碼是否合法,還有歸屬地查詢。舊的數據是幾年前了太久了,打算用python爬蟲重新爬一份

單線程版本

# coding:utf-8import requestsfrom datetime import datetimeclass PhoneInfoSpider: def __init__(self, phoneSections): self.phoneSections = phoneSections def phoneInfoHandler(self, textData): text = textData.splitlines(True) # print('text length:' + str(len(text))) if len(text) >= 9: number = text[1].split(’’’)[1] province = text[2].split(’’’)[1] mobile_area = text[3].split(’’’)[1] postcode = text[5].split(’’’)[1] line = 'number:' + number + ',province:' + province + ',mobile_area:' + mobile_area + ',postcode:' + postcode line_text = number + ',' + province + ',' + mobile_area + ',' + postcode print(line_text) # print('province:' + province) try:f = open(’./result.txt’, ’a’)f.write(str(line_text) + ’n’) except Exception as e:print(Exception, ':', e) def requestPhoneInfo(self, phoneNum): try: url = ’https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=’ + phoneNum response = requests.get(url) self.phoneInfoHandler(response.text) except Exception as e: print(Exception, ':', e) def requestAllSections(self): # last用于接上次異常退出前的號碼 last = 0 # last = 4 # 自動生成手機號碼,后四位補0 for head in self.phoneSections: head_begin = datetime.now() print(head + ' begin time:' + str(head_begin)) # for i in range(last, 10000): for i in range(last, 10):middle = str(i).zfill(4)phoneNum = head + middle + '0000'self.requestPhoneInfo(phoneNum) last = 0 head_end = datetime.now() print(head + ' end time:' + str(head_end))if __name__ == ’__main__’: task_begin = datetime.now() print('phone check begin time:' + str(task_begin)) # 電信,聯通,移動,虛擬運營商 dx = [’133’, ’149’, ’153’, ’173’, ’177’, ’180’, ’181’, ’189’, ’199’] lt = [’130’, ’131’, ’132’, ’145’, ’146’, ’155’, ’156’, ’166’, ’171’, ’175’, ’176’, ’185’, ’186’, ’166’] yd = [’134’, ’135’, ’136’, ’137’, ’138’, ’139’, ’147’, ’148’, ’150’, ’151’, ’152’, ’157’, ’158’, ’159’, ’172’, ’178’, ’182’, ’183’, ’184’, ’187’, ’188’, ’198’] add = [’170’] all_num = dx + lt + yd + add # print(all_num) print(len(all_num)) # 要爬的號碼段 spider = PhoneInfoSpider(all_num) spider.requestAllSections() task_end = datetime.now() print('phone check end time:' + str(task_end))

發現爬取一個號段,共10000次查詢,單線程版大概要多1個半小時,太慢了。

多線程版本

# coding:utf-8import requestsfrom datetime import datetimeimport queueimport threadingthreadNum = 32class MyThread(threading.Thread): def __init__(self, func): threading.Thread.__init__(self) self.func = func def run(self): self.func()def requestPhoneInfo(): global lock while True: lock.acquire() if q.qsize() != 0: print('queue size:' + str(q.qsize())) p = q.get() # 獲得任務 lock.release() middle = str(9999 - q.qsize()).zfill(4) phoneNum = phone_head + middle + '0000' print('phoneNum:' + phoneNum) try:url = ’https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=’ + phoneNum# print(url)response = requests.get(url)# print(response.text)phoneInfoHandler(response.text) except Exception as e:print(Exception, ':', e) else: lock.release() breakdef phoneInfoHandler(textData): text = textData.splitlines(True) if len(text) >= 9: number = text[1].split(’’’)[1] province = text[2].split(’’’)[1] mobile_area = text[3].split(’’’)[1] postcode = text[5].split(’’’)[1] line = 'number:' + number + ',province:' + province + ',mobile_area:' + mobile_area + ',postcode:' + postcode line_text = number + ',' + province + ',' + mobile_area + ',' + postcode print(line_text) # print('province:' + province) try: f = open(’./result.txt’, ’a’) f.write(str(line_text) + ’n’) except Exception as e: print(Exception, ':', e)if __name__ == ’__main__’: task_begin = datetime.now() print('phone check begin time:' + str(task_begin)) dx = [’133’, ’149’, ’153’, ’173’, ’177’, ’180’, ’181’, ’189’, ’199’] lt = [’130’, ’131’, ’132’, ’145’, ’155’, ’156’, ’166’, ’171’, ’175’, ’176’, ’185’, ’186’, ’166’] yd = [’134’, ’135’, ’136’, ’137’, ’138’, ’139’, ’147’, ’150’, ’151’, ’152’, ’157’, ’158’, ’159’, ’172’, ’178’, ’182’, ’183’, ’184’, ’187’, ’188’, ’198’] all_num = dx + lt + yd print(len(all_num)) for head in all_num: head_begin = datetime.now() print(head + ' begin time:' + str(head_begin)) q = queue.Queue() threads = [] lock = threading.Lock() for p in range(10000): q.put(p + 1) print(q.qsize()) for i in range(threadNum): middle = str(i).zfill(4) global phone_head phone_head = head thread = MyThread(requestPhoneInfo) thread.start() threads.append(thread) for thread in threads: thread.join() head_end = datetime.now() print(head + ' end time:' + str(head_end)) task_end = datetime.now() print('phone check end time:' + str(task_end))

多線程版的1個號碼段1000條數據,大概2,3min就好,cpu使用飆升,大概維持在70%左右。

總共40多個號段,爬完大概1,2個小時,總數據41w左右

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产免费网站看v片元遮挡 国产免费自拍 | 乱色视频中文字幕 | 欧美三级在线 | 国产一区在线播放 | 在线播放一区 | 天天爱天天做色综合 | 久青草国产在线 | 99综合网 | 国产免费资源高清小视频在线观看 | 日本香蕉视频 | 伊人色综合久久天天网蜜月 | 手机在线观看黄色 | 久久最新精品 | 免费区欧美一级毛片 | 国产综合日韩伦理 | 一区二区在线视频 | 欧美在线一区二区 | 国产秦先生大战白丝97在线 | 九九热国产视频 | 色老成人精品视频在线观看 | 久久综合五月婷婷 | 亚洲国产精品一区二区久久hs | 中文字幕一区在线播放 | 2022麻豆剧果冻传媒 | 亚洲精品亚洲人成人网 | 亚洲在线小视频 | 亚洲欧美日韩三级 | 欧美成人观看 | 黄频在线免费观看 | va在线视频| 欧美成人免费videos | 国产福利午夜自产拍视频在线 | 成人影院久久久久久影院 | 爱爱小说视频永久免费网站 | 亚洲精品一区二区三区婷婷月 | 韩国日本一级毛片免费视频 | 久久国产精品免费一区二区三区 | 2022日本卡一卡二新区 | 国产精品亚洲精品爽爽 | 日本高清免费zzzzzzzz | 亚洲国产精品久久卡一 |