我正在为python 这里使用报纸模块。
在教程中,它描述了如何汇集不同报纸的建设。它同时产生它们。(请参阅上面链接中的“多线程文章下载”)
有什么方法可以直接从urls列表中提取文章吗?也就是说,我是否可以将多个urls插入到下面的设置中,并让它同时下载和解析它们?
from newspaper import Article
url = 'http://www.bbc.co.uk/zhongwen/simp/chinese_news/2012/12/121210_hongkong_politics.shtml'
a = Article(url, language='zh') # Chinese
a.download()
a.parse()
print(a.text[:150])发布于 2016-07-06 19:13:38
我能够通过为每个文章URL创建一个Source来做到这一点。(免责声明:不是python开发人员)
import newspaper
urls = [
'http://www.baltimorenews.net/index.php/sid/234363921',
'http://www.baltimorenews.net/index.php/sid/234323971',
'http://www.atlantanews.net/index.php/sid/234323891',
'http://www.wpbf.com/news/funeral-held-for-gabby-desouza/33874572',
]
class SingleSource(newspaper.Source):
def __init__(self, articleURL):
super(StubSource, self).__init__("http://localhost")
self.articles = [newspaper.Article(url=url)]
sources = [SingleSource(articleURL=u) for u in urls]
newspaper.news_pool.set(sources)
newspaper.news_pool.join()
for s in sources:
print s.articles[0].html发布于 2018-08-07 16:10:30
我知道这个问题确实很古老,但这是我在谷歌上搜索如何获取多线程报纸时最先出现的链接之一。虽然Kyles的答案很有帮助,但它并不完整,我认为它有一些排印.
import newspaper
urls = [
'http://www.baltimorenews.net/index.php/sid/234363921',
'http://www.baltimorenews.net/index.php/sid/234323971',
'http://www.atlantanews.net/index.php/sid/234323891',
'http://www.wpbf.com/news/funeral-held-for-gabby-desouza/33874572',
]
class SingleSource(newspaper.Source):
def __init__(self, articleURL):
super(SingleSource, self).__init__("http://localhost")
self.articles = [newspaper.Article(url=articleURL)]
sources = [SingleSource(articleURL=u) for u in urls]
newspaper.news_pool.set(sources)
newspaper.news_pool.join()我将Stubsource更改为Singlesource,将其中一个urls更改为articleURL。当然,这只是下载网页,你仍然需要解析它们才能得到文本。
multi=[]
i=0
for s in sources:
i+=1
try:
(s.articles[0]).parse()
txt = (s.articles[0]).text
multi.append(txt)
except:
pass在我的100个url示例中,与仅按顺序处理每个url相比,这花费了一半的时间。(编辑:在将样本数量增加到2000年之后,大约减少了四分之一。)
(编辑:让整个工作与多线程!)我对我的实现使用了非常好的这解释。当样本大小为100个urls时,使用4个线程所需的时间与上面的代码相当,但是将线程数量增加到10会进一步减少大约一半。更大的样本大小需要更多的线程来提供一个可比较的差异。
import newspaper
from multiprocessing.dummy import Pool as ThreadPool
def getTxt(url):
article = Article(url)
article.download()
try:
article.parse()
txt=article.text
return txt
except:
return ""
pool = ThreadPool(10)
# open the urls in their own threads
# and return the results
results = pool.map(getTxt, urls)
# close the pool and wait for the work to finish
pool.close()
pool.join()发布于 2020-04-09 00:54:40
以约瑟夫的回答为基础。我假设最初的海报想要使用多线程来提取一堆数据,并将其正确地存储在某个地方。经过多次尝试,我认为我已经找到了一个解决方案,它可能不是最有效的,但它是有效的,我试着使它更好,然而,我认为newspaper3k插件可能有点错误。但是,这在将所需的元素提取到DataFrame中时是有效的。
import newspaper
from newspaper import Article
from newspaper import Source
import pandas as pd
gamespot_paper = newspaper.build('https://www.gamespot.com/news/', memoize_articles=False)
bbc_paper = newspaper.build("https://www.bbc.com/news", memoize_articles=False)
papers = [gamespot_paper, bbc_paper]
news_pool.set(papers, threads_per_source=4)
news_pool.join()
#Create our final dataframe
df_articles = pd.DataFrame()
#Create a download limit per sources
limit = 100
for source in papers:
#tempoary lists to store each element we want to extract
list_title = []
list_text = []
list_source =[]
count = 0
for article_extract in source.articles:
article_extract.parse()
if count > limit:
break
#appending the elements we want to extract
list_title.append(article_extract.title)
list_text.append(article_extract.text)
list_source.append(article_extract.source_url)
#Update count
count +=1
df_temp = pd.DataFrame({'Title': list_title, 'Text': list_text, 'Source': list_source})
#Append to the final DataFrame
df_articles = df_articles.append(df_temp, ignore_index = True)
print('source extracted')请建议任何改进!
https://stackoverflow.com/questions/37403563
复制相似问题