亲爱的Stackoverflow社区!
我想从CNN RSS提要中抓取新闻文章,并获取每个抓取文章的链接。这在Python NewsPaper库中效果很好,但是很遗憾,我无法以可用格式(即列表或字典)获得输出。
我想将抓取的链接添加到一个SINGLE列表中,而不是许多单独的列表中。
import feedparser as fp
import newspaper
from newspaper import Article
website = {"cnn": {"link": "http://edition.cnn.com/", "rss": "http://rss.cnn.com/rss/cnn_topstories.rss"}}
for source, value in website.items():
if 'rss' in value:
d = fp.parse(value['rss'])
#if there is an RSS value for a company, it will be extracted into d
for entry in d.entries:
if hasattr(entry, 'published'):
article = {}
article['link'] = entry.link
print(article['link'])
输出如下:
http://rss.cnn.com/~r/rss/cnn_topstories/~3/5aHaFHz2VtI/index.html
http://rss.cnn.com/~r/rss/cnn_topstories/~3/_O8rud1qEXA/joe-walsh-trump-gop-voters-sot-crn-vpx.cnn
http://rss.cnn.com/~r/rss/cnn_topstories/~3/xj-0PnZ_LwU/index.html
.......
我想拥有一个包含所有链接的列表,即:
list =[http://rss.cnn.com/~r/rss/cnn_topstories/~3/5aHaFHz2VtI/index.html , http://rss.cnn.com/~r/rss/cnn_topstories/~3/_O8rud1qEXA/joe-walsh-trump-gop-voters-sot-crn-vpx.cnn , http://rss.cnn.com/~r/rss/cnn_topstories/~3/xj-0PnZ_LwU/index.html ,... ]
我尝试通过如下所示的for循环附加内容:
for i in article['link']:
article_list = []
article_list.append(i)
print(article_list)
但是输出是这样的:
['h']
['t']
['t']
['p']
[':']
['/']
['/']
['r']
['s']
...
有人知道另一种方法,如何将内容放入一个列表?或以下字典:
dict = {'links':[link1 , link2 , link 3]}
非常感谢您的帮助!
尝试像这样修改您的代码,看看是否可行:
article_list = []
for entry in d.entries:
if hasattr(entry, 'published'):
article = {}
article['link'] = entry.link
article_list.append(article['link'])