Python如何實(shí)現(xiàn)大數(shù)據(jù)收集至excel-創(chuàng)新互聯(lián)

小編給大家分享一下Python如何實(shí)現(xiàn)大數(shù)據(jù)收集至excel,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對(duì)外擴(kuò)展宣傳的重要窗口,一個(gè)合格的網(wǎng)站不僅僅能為公司帶來(lái)巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺(tái),創(chuàng)新互聯(lián)面向各種領(lǐng)域:成都除甲醛成都網(wǎng)站設(shè)計(jì)公司、網(wǎng)絡(luò)營(yíng)銷推廣解決方案、網(wǎng)站設(shè)計(jì)等建站排名服務(wù)。

一、在工程目錄中新建一個(gè)excel文件

二、使用python腳本程序?qū)⒛繕?biāo)excel文件中的列頭寫入,本文省略該部分的code展示,可自行網(wǎng)上查詢

三、以下code內(nèi)容為:實(shí)現(xiàn)從接口獲取到的數(shù)據(jù)值寫入excel的整體步驟

       1、整體思路:

             (1)、根據(jù)每日調(diào)取接口的日期來(lái)作為excel文件中:列名為“收集日期”的值

             (2)、程序默認(rèn)是每天會(huì)定時(shí)調(diào)取接口并獲取接口的返回值并寫入excel中(我使用的定時(shí)任務(wù)是:linux下的contab)

             (3)、針對(duì)接口異常未正確返回?cái)?shù)據(jù)時(shí),使用特殊符號(hào)如:NA代替并寫入excel文件中(后期使用excel數(shù)據(jù)做分析時(shí)有用)

        2、完整代碼如下:

import requests, xlrd, os, sys, urllib3
from datetime import date, timedelta
from xlutils.copy import copy
basedir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(basedir)
from lib.mysqldb import mysqldb
from lib.public_methods import test_login
def collect_data():
  """test_rooms.test_kpi卡片下:adr指標(biāo)值收集"""
  get_all_code_sql = 'select DISTINCT test_code from test_info WHERE open_flag = 1'
  test_code_all = mysqldb("test_data").selectsql(get_all_code_sql)
  test_code_list = []
  adr_insert_data_list = []
  yesterday = (date.today() + timedelta(days=-1)).strftime("%Y-%m-%d")
  adr_insert_data_list.append(yesterday)
  for j in range(len(test_code_all)):
    test_code_list.append(test_code_all[j]["test_code"])
  for m in range(len(test_code_list)):
    url = "https://www.baidu.com/test/api/data/query.json"
    header = {
      "Content-Type": "application/json;charset=UTF-8",
      "Cookie": str(test_login())
    }
    param = {
      "code": "test_rooms.test_kpi",
      "page": 1,
      "pageSize": 1000,
      "params": {
        "start_date_year": "2019",
        "start_date_month": "9",
        "start_date_day": "16",
        "end_date_year": "2019",
        "currency_type": "usd",
        "end_date_day": "16",
        "end_date_month": "9",
        "tests": "test_001"
      }
    }
    """替換請(qǐng)求參數(shù)中的開始日期"""
    param["params"]["start_date_year"] = str(yesterday).split("-")[0]
    param["params"]["start_date_month"] = str(yesterday).split("-")[1]
    param["params"]["start_date_day"] = str(yesterday).split("-")[2]
    """替換請(qǐng)求參數(shù)中的結(jié)束日期"""
    param["params"]["end_date_year"] = param["params"]["start_date_year"]
    param["params"]["end_date_month"] = param["params"]["start_date_month"]
    param["params"]["end_date_day"] = param["params"]["start_date_day"]
    param["params"]["tests"] = test_code_list[m]
    urllib3.disable_warnings()
    result = requests.post(url=url, headers=header, json=param, verify=False).json()
    if str(result["data"]["data"]) != "None":
      """adr指標(biāo)值收集"""
      indicatorList = result["data"]["data"]["test_indicator_list"]
      test_actualorLast_Forecast = result["data"]["data"]["test_actual"]
      new_indicator_actualvalue = {}
      i = 0
      while i < len(indicatorList):
        dit = {indicatorList[i]: test_actualorLast_Forecast[i]}
        new_indicator_actualvalue.update(dit)
        i += 1
      if str(new_indicator_actualvalue["adr"]) == "--":
        adr_value_result = "NA"
        adr_insert_data_list.append(adr_value_result)
      else:
        adr_value_result = new_indicator_actualvalue["adr"]
        adr_insert_data_list.append(adr_value_result)
    else:
      adr_value_result = "NA"
      adr_insert_data_list.append(adr_value_result)
  """adr指標(biāo)值數(shù)據(jù)收集入excel路徑"""
  workbook = xlrd.open_workbook(basedir + "/data/collect_data_center.xls") # 打開工作簿
  sheets = workbook.sheet_names() # 獲取工作簿中的所有表格
  worksheet = workbook.sheet_by_name(sheets[0]) # 獲取工作簿中所有表格中的的第一個(gè)表格
  rows_old = worksheet.nrows # 獲取表格中已存在的數(shù)據(jù)的行數(shù)
  new_workbook = copy(workbook) # 將xlrd對(duì)象拷貝轉(zhuǎn)化為xlwt對(duì)象
  new_worksheet = new_workbook.get_sheet(0) # 獲取轉(zhuǎn)化后工作簿中的第一個(gè)表格
  for i in range(0, 1):
    for j in range(0, len([adr_insert_data_list][i])):
      new_worksheet.write(i + rows_old, j, [adr_insert_data_list][i][j]) # 追加寫入數(shù)據(jù),注意是從i+rows_old行開始寫入
  new_workbook.save(basedir + "/data/collect_data_center.xls") # 保存工作簿
  print("adr指標(biāo)值---xls格式表格【追加】寫入數(shù)據(jù)成功!")

              3、從步驟2中的代碼可看出代碼整體分為3個(gè)部分:

                    (1)、組裝接口參數(shù);

                    (2)、調(diào)用接口將接口返回的結(jié)果集收集在list中;

                    (3)、將收集的結(jié)果寫入excel中并保存;

tips:windows與linux下excel的路徑格式需要區(qū)分下,以上代碼中的"/data/collect_data_center.xls"為linux環(huán)境下路徑

看完了這篇文章,相信你對(duì)“Python如何實(shí)現(xiàn)大數(shù)據(jù)收集至excel”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,感謝各位的閱讀!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)站名稱:Python如何實(shí)現(xiàn)大數(shù)據(jù)收集至excel-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://muchs.cn/article24/eidce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站收錄、品牌網(wǎng)站設(shè)計(jì)做網(wǎng)站、建站公司、域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司