首页 > 解决方案 > 报纸(python)获取所有cnn新闻网址

问题描述

例如在这个 url ( https://edition.cnn.com/search/?q=%20news&size=10&from=5540&page=555 )

在 html 文件中我可以找到这个链接(html 标签)

<div class="cnn-search__result-thumbnail">         
     <a href="https://www.cnn.com/2018/03/27/asia/north-korea-kim-jong-un-china-visit/index.html">
  <img src="./Search CNN - Videos, Pictures, and News - 
    CNN.com_files/180328104116china-xi-kim-story-body.jpg">
 </a>

但在这段代码中

    cnn_paper = newspaper.build(url, memoize_articles=False)
     for article in cnn_paper.articles:
          print(article.url) 

我找不到新闻链接

https://edition.cnn.com/search/?q=%20news&size=10&from=5540&page=555 https://edition.cnn.com/search/?q=%20news&size=10&from=5550&page=556

获取相同的链接

标签: pythonhtmlpython-newspaper

解决方案


搜索结果从来自不同请求的 JSON 文件动态显示: https ://search.api.cnn.io/content?q=news&size=50&from=0

尺寸最大可以为 50。

res = requests.get("https://search.api.cnn.io/content?q=news&size=50&from=0")
links = [x['url'] for x in res.json()['result']]

推荐阅读