微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python:报纸模块 – 任何直接从URL获取文章的方法?

我正在使用报纸模块为python找到here.

在教程中,它描述了如何汇集不同报纸的建设.它会同时生成它们. (请参阅上面链接中的“多线程文章下载”)

有没有办法直接从网址列表中提取文章?也就是说,有什么方法可以将多个URL添加到以下设置中并让它同时下载和解析它们?

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])

解决方法:

我能够通过为每篇文章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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐