1

该脚本使用 time.sleep (3600) 每隔 1 小时执行一次 google 请求,并生成一个 txt 文件,其中包含他翻阅了一天半的所有短语。我想使用 TIMESTAMP 正确执行此操作。有人可以帮助我吗?

import urllib
import urllib2
import json
import time

while 1:
    url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=AIzaSyA3758yM14aTX7aI9_v5AvKI2X1m56HszI" 

    code = {

    "request": {
    "passengers": {
      "adultCount": 1,
      "childCount": 1
     },
    "slice": [
      {
        "origin": "SSA",
        "destination": "GRU",
        "date": "2015-06-19",
        "permittedDepartureTime":
        {
          "kind": "qpxexpress#timeOfDayRange",
          "earliestTime": "22:00",
          "latestTime": "23:00"
        }
      },
      {
        "origin": "GRU",
        "destination": "SSA",
        "date": "2015-06-30",
        "permittedDepartureTime":
        {
          "kind": "qpxexpress#timeOfDayRange",
          "earliestTime": "05:00",
          "latestTime": "12:00"
        }
      }
    ],
    "solutions": 3
      }
    }

    #hoje = "%s" % (time.strftime("%Y_%m_%d")) 

    jsonreq = json.dumps(code, encoding = 'utf-8')
    req = urllib2.Request(url, jsonreq, {'Content-Type': 'application/json'})
    flight = urllib2.urlopen(req)
    response = flight.read()
    flight.close()
    #print(response)
    print("----------------")


    texto=(response)
    v_file= open("ssaGRU.json" ,"a")

    #hora = time.strftime("%H:%M:%S %Z")
    v_file.write(texto)
    #v_file.write("[%s] Hora do json.\r\n" % (hora)) 
    v_file.close()

    time.sleep(15)
4

1 回答 1

0
current_time = time.strftime("%H:%M", time.localtime())
v_file = open("ssaGRU.json", "a")
v_file.write(str(current_time) + ': ')
v_file.write(texto + '\n')
v_file.close()

这将在输入每一行之前打印您的当前时间,并在最后添加一个空行,这样您来自不同时间的数据就不会停留在一行上。

如果需要%m.%d.%y,您也可以添加。current_time如果texto不是字符串,请确保添加str(texto).

于 2015-06-16T02:48:49.647 回答