使用 Python 中的请求下载不完整

Incomplete download using requests in Python

我正在关注一个预测空气质量指数的在线项目。为此,我们需要先获取数据,这些数据是从网站下载的。这里是作者提供的源码:

import os
import time
import requests
import sys

def retrieve_html():
    for year in range(2013,2019):
        for month in range(1,13):
            if(month<10):
                url='http://en.tutiempo.net/climate/0{}-{}/ws-421820.html'.format(month
                                                                          ,year)
            else:
                url='http://en.tutiempo.net/climate/{}-{}/ws-421820.html'.format(month
                                                                          ,year)
            texts=requests.get(url)
            text_utf=texts.text.encode('utf=8')
            
            if not os.path.exists("Data/Html_Data/{}".format(year)):
                os.makedirs("Data/Html_Data/{}".format(year))
            with open("Data/Html_Data/{}/{}.html".format(year,month),"wb") as output:
                output.write(text_utf)
            
        sys.stdout.flush()
        
if __name__=="__main__":
    start_time=time.time()
    retrieve_html()
    stop_time=time.time()
    print("Time taken {}".format(stop_time-start_time))

这很好用。现在,我尝试自己编写相同的代码。这是我的代码:

import os
import time
import requests
import sys


def retrieve_html():
    for year in range(2013,2019):
        for month in range(1,13):
            if(month<10):
                url='http://en.tutiempo.net/climate/0{}-{}/ws-421820.html'.format(month, year)
            else:
                url='http://en.tutiempo.net/climate/{}-{}/ws-421820.html'.format(month, year)
        
        texts=requests.get(url)
        text_utf=texts.text.encode("utf=8")
        
        if not os.path.exists("Data/Html_Data/{}".format(year)):
            os.makedirs("Data/Html_Data/{}".format(year))
        
        with open("Data/Html_Data/{}/{}.html".format(year,month),"wb") as output:
            output.write(text_utf)
            
    sys.stdout.flush()
        
if __name__=="__main__":
    start_time=time.time()
    retrieve_html()
    stop_time=time.time()
    print("Time taken: {}".format(stop_time-start_time))

但是每当我 运行 这个脚本时,只有第 12 个月的数据会被下载,其他月份的数据不会被下载。我使用作者提供的代码进行了检查,它运行良好,尽管我的代码与他的代码完全相同。这真让我抓狂。谁能指出我哪里出错了?

不完全一样,有不同的缩进:

嗯,你应该缩进:

        texts=requests.get(url)
        text_utf=texts.text.encode("utf=8")
        
        if not os.path.exists("Data/Html_Data/{}".format(year)):
            os.makedirs("Data/Html_Data/{}".format(year))
        
        with open("Data/Html_Data/{}/{}.html".format(year,month),"wb") as output:
            output.write(text_utf)

代码是正确的,只是有缩进问题。下面的代码应该在内部for循环

texts=requests.get(url)
text_utf=texts.text.encode("utf=8")
        
if not os.path.exists("Data/Html_Data/{}".format(year)):
   os.makedirs("Data/Html_Data/{}".format(year))
        
   with open("Data/Html_Data/{}/{}.html".format(year,month),"wb") as output:
        output.write(text_utf)

而下面的代码应该在外层的for循环中

sys.stdout.flush()