2014-07-14 38 views
0

從rss提要中,我能夠從每個提要中抓取內容,但現在試圖將每個輸出保存爲具有其標題的單個txt文件,例如newstitle1.txt,newstitle2.txt ...但到目前爲止,使用下面的代碼,它只保存列表中的最後一個輸出。有想法該怎麼解決這個嗎?保存爲抓取的輸出

with open('textfile.txt', 'w') as handle: 
     handle.write(text) 
    counter+=1 
+0

使用'open('textfile.txt','a')' – Ben

+0

附加輸出。我需要每個輸出的txt文件 – hepzibah

+0

我沒有看到文件名「textfile.txt」的任何更改,它每次都會被覆蓋。 – Ben

回答

1
# somewhere in code 

counter = 0 

# in place where you save it 

with open('textfile' + str(counter) + '.txt', 'w') as handle: 
    handle.write(text) 
counter+=1 

,或者如果你有可變newstitle

with open(newstitle + '.txt', 'w') as handle: 
    handle.write(text) 

稱號,並期待在本土字母,空格和文件名等怪字符。

+0

工作..謝謝 – hepzibah