亲爱的Stackoverflow社区!
我想从CNN RSS提要中抓取新闻文章,并获得每篇抓取文章的链接。这适用于Python NewsPaper库,但不幸的是,我无法以可用的格式(即列表或字典(获取输出。
我想将抓取的链接添加到一个列表中,而不是许多单独的列表中。
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'])