第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Python 中的請求下載不完整

使用 Python 中的請求下載不完整

慕少森 2023-03-08 10:14:35
我正在關(guān)注一個我們預(yù)測空氣質(zhì)量指數(shù)的在線項目。為此,我們需要先獲取數(shù)據(jù),這些數(shù)據(jù)是從網(wǎng)站下載的。下面是作者提供的源碼:import osimport timeimport requestsimport sysdef 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))這工作得很好?,F(xiàn)在,我嘗試自己編寫相同的代碼。這是我的代碼:import osimport timeimport requestsimport sysdef 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()        但每當(dāng)我運行此腳本時,只會下載第 12 個月的數(shù)據(jù),而不會下載其他月份的其余數(shù)據(jù)。我使用作者提供的代碼進行了檢查,它運行良好,盡管我的代碼與他的代碼完全相同。這真讓我抓狂。誰能指出我哪里出錯了?
查看完整描述

3 回答

?
慕慕森

TA貢獻1856條經(jīng)驗 獲得超17個贊

它不完全一樣,有不同的縮進:

http://img1.sycdn.imooc.com//6407efb3000124ff16640523.jpg


查看完整回答
反對 回復(fù) 2023-03-08
?
肥皂起泡泡

TA貢獻1829條經(jīng)驗 獲得超6個贊

那么,你應(yīng)該縮進這個:


        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)


查看完整回答
反對 回復(fù) 2023-03-08
?
翻過高山走不出你

TA貢獻1875條經(jīng)驗 獲得超3個贊

代碼是正確的,只是存在縮進問題。下面的代碼應(yīng)該在for循環(huán)的內(nèi)部


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)

而下面的代碼應(yīng)該在外層的for循環(huán)中


sys.stdout.flush()


查看完整回答
反對 回復(fù) 2023-03-08
  • 3 回答
  • 0 關(guān)注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號