import json import time from multiprocessing.dummy import Pool import pymongo import requests import pandas as pd myclient = pymongo.MongoClient('localhost', 27017) # 连接MongoDB db = myclient['temp_user_info'] DataYuan = db['qqandtel'] DataResult = db["result"] def getUserTelAddress(tel): api_url = r'https://cx.shouji.360.cn/phonearea.php?number=' print("开始查询",tel) ret = requests.request(method="POST",url=api_url+str(tel)) result = ret.content.decode("utf-8").encode("utf-8").decode('unicode_escape') print(result) return json.loads(result) def loadData(): # 将这一千条数据添加到Mongodb url = r"綦江校区2K.xlsx" name = pd.read_excel(url,sheet_name="Sheet1",header=0)["姓名"] tel= pd.read_excel(url, sheet_name="Sheet1", header=0,dtype={'电话':str})["电话"] # 从MongoDB读取数据 jsq = 0 for i in range(0,len(name)): DataYuan.insert_one({"姓名":name[i],"电话":str(tel[i])}) # 2020条数据 def getinfo(tel): result = getUserTelAddress(tel) code = result["code"] if code !=0: # DataYuan.insert({"QQ号":qq}) # 没有删除,因此也无需添加 pass else: data = result["data"] old = DataYuan.find_one({"电话": tel}) DataResult.insert_one( {"姓名": old["姓名"],"电话":old["电话"] ,"地区":data["province"],"地区2": data['city'], '手机归属': data['sp']}) DataYuan.delete_one({"电话": tel}) time.sleep(0.2) def run(data): pool = Pool(10) # 多线程 pool.map(getinfo, data) def start(): while DataYuan.find().count() > 0: data = [] for i in DataYuan.find(): data.append(i["电话"]) # print(data) run(data) if __name__ == '__main__': # loadData() start()