0

I want Python to print out strings while a process is happening, such as the download in the script below. However, I'm finding it difficult. For example, maybe I want Python to say after 10 seconds of download, "Hey, it's been 10 secs." I can put the start time only before the process, and the end time only after the process, which means I can't track while the download is happening.

import requests, re, time

link = "https://web.archive.org/cdx/search/cdx?url=twitter.com/realdonaldtrump/status&matchType=prefix&filter=statuscode:200"

response = requests.head(link).text
urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', response)
for url in urls:
    start = time.time()
    data2.append(f"{url}")
    end = time.time()
    time_elapsed = end - start
    if time_elapsed >= 10:
        print("It's been 10 seconds.")

1 Answers1

1

you can use progress bar:

  1. use progressbar module
import progressbar
from time import sleep
bar = progressbar.ProgressBar(maxval=20, \
    widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
bar.start()
for i in xrange(20):
    bar.update(i+1)
    sleep(0.1)
bar.finish()
def print_progress_bar(total, current, barsize=60):
    progress = int(current*barsize/total)
    completed = str(int(current*100/total)) + '%'
    print('[', chr(9608)*progress, ' ', completed, '.'*(barsize-progress), '] ', str(i)+'/'+str(total), sep='', end='\r', flush=True)

example:

total = 6000
barsize = 60
print_frequency = max(min(total//barsize, 100), 1)
print("Start Task..", flush=True)
for i in range(1, total+1):
  if i%print_frequency == 0 or i == 1:
    print_progress_bar(total, i, barsize)
print("\nFinished", flush=True)
Tal Folkman
  • 2,368
  • 1
  • 7
  • 21