【问题标题】:i want to get CNN news article all link我想获得 CNN 新闻文章的所有链接
【发布时间】:2019-01-31 16:51:24
【问题描述】:

我想获取 CNN 新闻文章的全部链接

例如

在这个链接里

  https://edition.cnn.com/search/?q=%20news&size=10

我可以显示最新的 10 条新闻

获取新闻链接 我尝试了两种方法。

   html_page = urlopen(url)
   soup = BeautifulSoup(html_page, "lxml")

   cnn_paper = newspaper.build(url, memoize_articles=False)  # ~15 seconds
   n_list = []
   for article in cnn_paper.articles:
      n_list.append(article.url)

req = Request(url)
html_page = urlopen(req)

soup = BeautifulSoup(html_page, "lxml")
links = []
for link in soup.findAll('a'):
    links.append(link.get('href'))

但我无法获取新闻链接

如果你转到下一页,我只能得到相同的链接

【问题讨论】:

    标签: python beautifulsoup python-newspaper


    【解决方案1】:

    试试这个:

    for link in soup.find_all('a'):
        links.append(link.get('href'))
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    猜你喜欢
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 2011-01-22
    • 2021-11-26
    • 2014-10-03
    • 2020-06-11
    相关资源
    最近更新 更多