鍍金池/ 問答/Python/ IDLE獲取網(wǎng)頁(yè)源碼后print()后就卡死了

IDLE獲取網(wǎng)頁(yè)源碼后print()后就卡死了

初學(xué) python,所用的版本為 python3,參照網(wǎng)上在 IDLE 中寫出了如下代碼:

from urllib.request import urlopen

html = urlopen("http://tieba.baidu.com/")  
print(html.read().decode('utf-8'))  

然后 print() 之后 IDLE 雖然輸出了,但是也就一直卡在這兒,如果不使用 print 而是將這個(gè)內(nèi)容保存到文件是沒問題的。請(qǐng)問是 IDLE 的問題,還是代碼的問題,如何解決呢?

回答
編輯回答
囍槑

可以試試先把內(nèi)容存為變量。

lines = html.read().decode('utf-8').split()
for line in lines:
    print(line)
2018年8月29日 08:36