亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

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

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

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

3 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

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

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


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

TA貢獻1829條經驗 獲得超6個贊

那么,你應該縮進這個:


        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)


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

TA貢獻1875條經驗 獲得超3個贊

代碼是正確的,只是存在縮進問題。下面的代碼應該在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()


查看完整回答
反對 回復 2023-03-08
  • 3 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號