Search code examples
pythonfiletimepython-requestsstatus

How can I find out the current status of the request?


How can I view the current progress of this request? Nothing is shown until the file completes and I would like to set some sort of indicator if this request is still active.

import requests

with open('file.txt', 'r') as f:
    urls = f.readlines()

datalist=[]
for url in urls:
    data = requests.get(url)
    datalist.append(data.text)

with open('file_complete.txt', 'w') as f:
    for item in datalist:
        f.write("%s\n" % item)

Solution

  • You can add a print() statement before the requests.gets(url) and after datalist.append(data.text). At least you can track the progress by URL.

    for url in urls:
        print("Getting " + url)
        data = requests.get(url)
        datalist.append(data.text)
        print(url + " successfully downloaded")
    

    Your code, however, only writes to the file once all URLs have been downloaded. If the program fails at any point file_complete.txt will not be created. So I suggest writing to the file once any URL download is successful.

    import requests
    
    with open('file.txt', 'r') as f:
        urls = f.readlines()
    
    # datalist=[]  // No longer needed
    for url in urls:
        data = requests.get(url)
    
        with open('file_complete.txt', 'a+') as f:   #change to mode "a+" to append
            f.write(data.text + "\n")
    
    

    Another improvement that can be made -- your code assumes that ALL URLs are valid. We can use a try-except block to catch errors.

    import requests
    
    with open('file.txt', 'r') as f:
        urls = f.readlines()
    
    # datalist=[]  // No longer needed
    for url in urls:
        try:
            data = requests.get(url)
        except:
            printf(url + " failed")
            continue   #moves on to the next url as nothing to write to file
    
        with open('file_complete.txt', 'a+') as f:   #change to mode "a+" to append
            f.write(data.text + "\n")