(Sep-23-2018, 05:44 PM)snippsat Wrote:Thank you for your answer. In this example, parse_func is called inside the render_template, and it sends request every time when the user goes to '/' route. I want to send a request every 12 hours, independent of the user choice of route. How can I avoid to send request every time?(Sep-22-2018, 12:31 PM)jomonetta Wrote: does the server able to execute the other independent script which uses cron?Can get some solution with cron to work,but as server is running there are several solution made for this APScheduler,Celery,schedule, ect... Here example with APScheduler,it's nice as can add job for several functions. It run withBackgroundScheduler
so it will not interfef with main loop that server run.from flask import Flask, render_template, jsonify from apscheduler.schedulers.background import BackgroundScheduler import random import requests app = Flask(__name__) def parse_func(): response = requests.get('https://nghttp2.org/httpbin/get') r = response.json() lst = [r['url'], r['origin']] rand_value = random.choice(lst) return(rand_value) @app.route("/") def template_test(): return render_template('sh2.html', location=parse_func()) if __name__ == '__main__': scheduler = BackgroundScheduler() scheduler.add_job(parse_func, 'interval', seconds=15) scheduler.start() app.run(debug=True)So now callingparse_func()
every 15-sec,longer eghour=12
. Just to see values on client side use jinja,with a reload script. sh2.html:<!doctype html> <html> <head> <title>Some title</title> <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" /> </head> <body> <p> {{ location }}</p> </body> <script> function timedRefresh(timeoutPeriod) { setTimeout("location.reload(true);",timeoutPeriod); } window.onload = timedRefresh(15000); </script> </html>Can also look at this post.
Web App That Request Data from Another Web Site every 12-hours
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
How to scraping data from dinamic site | sergio21124444 | 2 | 780 |
Nov-08-2023, 12:43 PM Last Post: sergio21124444 |
|
POST request with form data issue web scraping | hoff1022 | 1 | 2,741 |
Aug-14-2020, 10:25 AM Last Post: kashcode |
|
Scraping a dynamic data-table in python through AJAX request | filozofo | 1 | 3,935 |
Aug-14-2020, 10:13 AM Last Post: kashcode |
|
How to retrieve data from site | ROHK | 2 | 2,490 |
Mar-01-2019, 12:26 PM Last Post: ROHK |
|
Mechanize and BeautifulSoup read not correct hours | vaeVictis | 5 | 4,518 |
Jan-15-2019, 01:27 PM Last Post: metulburr |
Users browsing this thread: 2 Guest(s)