简体   繁体   English

如何从 Python 中的 URL 中提取每小时数据?

[英]How to pull hourly data from a URL in Python?

I'm querying data in JSON format, but I want it to continue pulling every time updated data is added.我正在查询 JSON 格式的数据,但我希望它在每次添加更新数据时继续拉取。 I also insert this data to the Postgresql database.我还将这些数据插入到 Postgresql 数据库中。 But since data is added to the url hourly, instead of waiting for the database to fill up, I want it to quickly pull the last 2-3 months' data at the first time with a loop and then continue to shoot every hour.但是由于url是按小时添加数据的,与其等数据库填满,不如让它在第一时间循环快速拉取最近2-3个月的数据,然后每小时继续拍摄。 How can I do that?我怎样才能做到这一点?

parameters = {
  "StationId": "377e1216-bcc7-42c0-aad8-4d5b3d602b78",
  "StartDate": "12.01.2022%2000:00:00",
  "EndDate": "12.01.2022%2001:00:00 }
payload = {}
headers= {}
url="https://api.ibb.gov.tr/havakalitesi/OpenDataPortalHandler/GetAQIByStationId"

req = requests.get(url, params = parameters)
if req.status_code == 200:
    decodeUrl = unquote(istek.url)
    response = requests.get(decodeUrl,headers=headers, data = payload)
    result = json.loads(response.text)
    print("Success")
else:
    print("Wrong.")

You could start by making the parameters variable, so you can use them in every call.您可以从使参数变量开始,这样您就可以在每次调用中使用它们。 Something like this:像这样的东西:

parameters = {
  "StationId": "377e1216-bcc7-42c0-aad8-4d5b3d602b78",
  "StartDate": start_date,
  "EndDate": end_date }

Then split the functionality to trigger the calls into 2. 1 "script" (for lack of better terminology) is triggered once to populate the database.然后将触发调用的功能拆分为 2 个。1 个“脚本”(由于缺乏更好的术语)被触发一次以填充数据库。 Get the current date and subtract 3 months, then put that into the parameters.获取当前日期并减去 3 个月,然后将其放入参数中。

Your second script would do the same but instead take the previous hour and input that into the parameters.您的第二个脚本将执行相同的操作,但将前一个小时输入到参数中。 Then (depending on your OS) run a cron-job or scheduled task where you fire off the second script.然后(取决于您的操作系统)运行一个 cron 作业或计划任务,您可以在其中触发第二个脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM