报纸图书馆
Newspaper library
作为使用 python 主题的绝对新手,我在使用报纸库扩展时遇到了一些困难。我的目标是定期使用报纸扩展程序下载名为 "tagesschau" 的德国新闻网站的所有新文章和 CNN 的所有文章,以构建我可以在几年内分析的数据堆栈。
如果我做对了,我可以使用以下命令下载所有文章并将其抓取到 python 库中。
import newspaper
from newspaper import news_pool
tagesschau_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 total
news_pool.join()`
如果这是下载所有文章的正确方法,那么我如何提取和保存 python 之外的文章?或者将这些文章保存在 python 中,以便在我再次重新启动 python 时可以重新使用它们?
感谢您的帮助。
您可以使用 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)
以下代码会将下载的文章保存为HTML格式。在文件夹中,您会找到。 tagesschau_paper0.html, tagesschau_paper1.html, tagesschau_paper2.html, .....
import newspaper
from newspaper import news_pool
tagesschau_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)
news_pool.join()
for i in range (tagesschau_paper.size()):
with open("tagesschau_paper{}.html".format(i), "w") as file:
file.write(tagesschau_paper.articles[i].html)
注意:news_pool
没有从 CNN 得到任何东西,所以我跳过了为其编写代码。如果您检查 cnn_paper.size()
,结果为 0
。您必须导入并使用 Source。
上面的代码也可以作为示例保存为其他格式的文章,例如。 txt,也只有文章中需要的部分,例如作者,正文,publish_date.
作为使用 python 主题的绝对新手,我在使用报纸库扩展时遇到了一些困难。我的目标是定期使用报纸扩展程序下载名为 "tagesschau" 的德国新闻网站的所有新文章和 CNN 的所有文章,以构建我可以在几年内分析的数据堆栈。 如果我做对了,我可以使用以下命令下载所有文章并将其抓取到 python 库中。
import newspaper
from newspaper import news_pool
tagesschau_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 total
news_pool.join()`
如果这是下载所有文章的正确方法,那么我如何提取和保存 python 之外的文章?或者将这些文章保存在 python 中,以便在我再次重新启动 python 时可以重新使用它们?
感谢您的帮助。
您可以使用 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)
以下代码会将下载的文章保存为HTML格式。在文件夹中,您会找到。 tagesschau_paper0.html, tagesschau_paper1.html, tagesschau_paper2.html, .....
import newspaper
from newspaper import news_pool
tagesschau_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)
news_pool.join()
for i in range (tagesschau_paper.size()):
with open("tagesschau_paper{}.html".format(i), "w") as file:
file.write(tagesschau_paper.articles[i].html)
注意:news_pool
没有从 CNN 得到任何东西,所以我跳过了为其编写代码。如果您检查 cnn_paper.size()
,结果为 0
。您必须导入并使用 Source。
上面的代码也可以作为示例保存为其他格式的文章,例如。 txt,也只有文章中需要的部分,例如作者,正文,publish_date.