weitiandiExport.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import os, sys,time,datetime
  4. import urllib, urllib2, sys
  5. import requests
  6. from mongoengine import register_connection, PointField, DynamicDocument, StringField
  7. import simplejson as json
  8. import ssl
  9. import xlrd
  10. from xlrd import xldate_as_tuple
  11. from collections import OrderedDict
  12. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  13. sys.path.insert(0, PROJECT_ROOT)
  14. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "configs.testing")
  15. from script.base import init_env
  16. init_env(interactive = False)
  17. from apps.web.core.db import Searchable
  18. from apps.web.core.utils import generate_excel_report
  19. register_connection(alias = 'spider',
  20. name = 'spider',
  21. host = '211.159.224.10',
  22. port = 27119,
  23. username = 'service',
  24. password = 'oOzjoQcO5DyyiN97AY0NpzJ6vztjNpx5',
  25. authentication_source = 'admin')
  26. class weitiandiDevice(Searchable):
  27. meta = {
  28. 'collection': 'weitiandi_device',
  29. 'db_alias': 'spider',
  30. }
  31. # 先把所有的设备二维码编号爬下来
  32. # page=0
  33. # while True:
  34. # url = 'https://trade.api3.sdaascloud.com/trade/nearbyChargers?key=&page=%s&size=10&latitude=30.48029&longitude=114.42073&raidus=3000000000' % page
  35. # strhtml = requests.get(url,timeout = 15).text
  36. # result = json.loads(strhtml)
  37. # if result.has_key('data') and result['data'].has_key('content') and len(result['data']['content']) == 0:
  38. # break
  39. # for dev in result['data']['content']:
  40. # xzzDevice.get_collection().update({'qrId':dev['qrId']},{'$set':dev},upsert = True)
  41. # page += 1
  42. # 根据二维码编号,获取设备更详细的信息
  43. def get_tel_zone(tel):
  44. if not tel or len(tel) <= 7:
  45. return '',''
  46. tel = tel.replace('-','')
  47. tel = tel.replace(' ','')
  48. print tel
  49. host = 'https://api04.aliyun.venuscn.com'
  50. path = '/mobile'
  51. method = 'GET'
  52. appcode = '8296fcd952e34713ba91fbdceb13e915'
  53. querys = 'mobile=%s' % tel
  54. bodys = {}
  55. url = host + path + '?' + querys
  56. try:
  57. request = urllib2.Request(url)
  58. request.add_header('Authorization', 'APPCODE ' + appcode)
  59. ctx = ssl.create_default_context()
  60. ctx.check_hostname = False
  61. ctx.verify_mode = ssl.CERT_NONE
  62. response = urllib2.urlopen(request, timeout = 15, context=ctx)
  63. content = response.read()
  64. if (content):
  65. result = json.loads(content)
  66. if result['msg'] != 'success':
  67. return '', ''
  68. return result['data']['prov'],result['data']['city']
  69. except Exception,e:
  70. return '',''
  71. # ownerDict = {}
  72. # for shopInfo in pianyichongDevice.get_collection().find():
  73. # if not shopInfo.has_key('manager'):
  74. # continue
  75. # if ownerDict.has_key(shopInfo['manager']):
  76. # ownerDict[shopInfo['manager']] += 1
  77. # else:
  78. # ownerDict[shopInfo['manager']] = 1
  79. # records = []
  80. # ii = 0
  81. # for rcd in shankailaidianDevice.get_collection().find({}):
  82. # ii += 1
  83. # if ii % 100 == 0:
  84. # print ii
  85. # dataList = [
  86. # (u'编号', str(rcd['vStationNo'])),
  87. # (u'总收入', float(rcd['dTotalIncome'])),
  88. # (u'地域ID', str(rcd['iAreaId'])),
  89. # (u'businessType', str(rcd['iBusinessType'])),
  90. # (u'iManagerId', str(rcd['iManagerId'])),
  91. # (u'添加时间', str(rcd['tAddTime'])),
  92. # (u'打开时间', str(rcd['tOpenTime'])),
  93. # (u'地址', str(rcd['vAddress'])),
  94. # (u'地域', str(rcd['vAreaName'])),
  95. # (u'电话', rcd['vHelpMobile'] if rcd['vHelpMobile'] != '' else rcd['vTelephone']),
  96. # # (u'充电站名称', str(rcd['vStationName'])),
  97. # ]
  98. #
  99. # records.append(OrderedDict(dataList))
  100. #
  101. # generate_excel_report('F:/shankailaidian1.xlsx', records,True)
  102. records = []
  103. owerDict = {}
  104. ii = 0
  105. # for rcd in weitiandiDevice.get_collection().find({'sn':{'$gte':'GD1B90000'}}):
  106. # ii += 1
  107. # if ii % 100 == 0:
  108. # print ii
  109. # dataList = [
  110. # (u'编号', rcd['sn']),
  111. # (u'联系方式', rcd['mobile']),
  112. # ]
  113. #
  114. # records.append(OrderedDict(dataList))
  115. # generate_excel_report('F:/weitiandi_4G.xlsx', records,True)
  116. for rcd in weitiandiDevice.get_collection().find({'spiderData':'0421'}):
  117. if rcd['mobile'] not in owerDict:
  118. owerDict[rcd['mobile']] = 1
  119. else:
  120. owerDict[rcd['mobile']] += 1
  121. for mobile,count in owerDict.items():
  122. ii += 1
  123. if ii % 100 == 0:
  124. print ii
  125. dataList = [
  126. (u'联系方式', mobile),
  127. (u'个数', count),
  128. ]
  129. records.append(OrderedDict(dataList))
  130. generate_excel_report('F:/weitiandi_4G_count2.xlsx', records,True)
  131. print('OK')