我怎样才能知道请求的当前状态?

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

如何查看此请求的当前进度?文件完成之前不会显示任何内容,如果此请求仍处于活动状态,我想设置某种指示器。

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)

requests.get() 是一个阻塞调用。如果你想有更多的控制权,你可以在单独的线程中发送你的请求。如果有顾虑,您还可以添加 timeouts。但是不,没有办法检查 in-progress get 请求的进度。

您可以在 requests.gets(url) 之前和 datalist.append(data.text) 之后添加 print() 语句。至少你可以通过 URL.

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

但是,您的代码只会在 所有 URL 已下载后才写入文件。如果程序在任何时候失败,file_complete.txt 将不会被创建。所以我建议一旦 URL 下载成功就写入文件。

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")

可以进行的另一项改进 -- 您的代码假定所有 URL 都是有效的。我们可以使用 try-except 块来捕获错误。

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")