You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python爬取GoogleNews RSS源如何仅输出第一条新闻

你的news_list是findAll()方法返回的有序序列,元素顺序和Google News RSS接口返回的新闻顺序完全一致,直接取索引为0的第一个元素即可实现仅打印第一条新闻的需求,建议加一层非空判断避免无新闻返回时触发索引异常。

修改后的代码如下:

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen

news_url = "https://news.google.com/rss?hl=de&gl=DE&ceid=DE:de"
Client = urlopen(news_url)
xml_page = Client.read()
Client.close()

soup_page = soup(xml_page, "xml")
news_list = soup_page.findAll("item")

# 仅打印第一条新闻
if news_list:
    first_news = news_list[0]
    print(first_news.title.text)
    print(first_news.link.text)
    print(first_news.pubDate.text)

内容的提问来源于stack exchange,提问作者user17115310

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 02:48:02