慕桂英3389331
2021-08-17 10:55:47
作為使用 python 主題的絕對新手,我在使用報紙庫擴(kuò)展時遇到了一些困難。我的目標(biāo)是定期使用報紙擴(kuò)展程序下載名為“tagesschau”的德國新聞網(wǎng)站的所有新文章和 CNN 的所有文章,以構(gòu)建我可以在幾年內(nèi)進(jìn)行分析的數(shù)據(jù)堆棧。如果我做對了,我可以使用以下命令下載所有文章并將其抓取到 python 庫中。import newspaperfrom newspaper import news_pooltagesschau_paper = newspaper.build('http://tagesschau.de')cnn_paper = newspaper.build('http://cnn.com')papers = [tagesschau_paper, cnn_paper]news_pool.set(papers, threads_per_source=2) # (3*2) = 6 threads totalnews_pool.join()`如果這是下載所有文章的正確方法,那么我如何在 python 之外提取和保存這些文章?或者將這些文章保存在 python 中,以便我再次重新啟動 python 時可以重用它們?
2 回答

素胚勾勒不出你
TA貢獻(xiàn)1827條經(jīng)驗 獲得超9個贊
您可以使用 pickle 在 python 之外保存對象并稍后重新打開它們:
file_Name = "testfile"
# open the file for writing
fileObject = open(file_Name,'wb')
# this writes the object news_pool to the
# file named 'testfile'
pickle.dump(news_pool,fileObject)
# here we close the fileObject
fileObject.close()
# we open the file for reading
fileObject = open(file_Name,'r')
# load the object from the file into var news_pool_reopen
news_pool_reopen = pickle.load(fileObject)
添加回答
舉報
0/150
提交
取消