如何使用Python获取每日新闻

如何使用Python获取每日新闻

如何使用Python获取每日新闻

如何使用Python获取每日新闻

  在本文中,我们将了解如何使用 Python 获取每日新闻。 这里我们将使用 Beautiful Soup 和 request 模块来抓取数据。

需要安装的模块

  • bs4 : Beautiful Soup(bs4) 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据。 这个模块不是 Python 内置的。 要安装此类型,请在终端中输入以下命令。
pip install bs4
  • requests Request 允许您非常轻松地发送 HTTP/1.1 请求。 这个模块也不是 Python 内置的。 要安装此类型,请在终端中输入以下命令。
pip install requests

使用Python获取新闻步骤:

  1、 确保导入这些库。

import requests
from bs4 import BeautifulSoup

  2、 然后要获取https://www.bbc.com/news的HTML内容,添加以下两行代码:

url='https://www.bbc.com/news'
response = requests.get(url)

  3 、 获取特定的 HTML 标签

  为了找到包含新闻标题的 HTML 标签,请访问 https://www.bbc.com/news 并通过右键单击新闻标题并单击“检查”来检查新闻标题:

获取特定的HTML标签

  您将看到所有标题都包含在“<h3>”标签中。 因此,要抓取此网页中的所有“<h3>”标签,请将以下代码行添加到您的脚本中:

  首先,我们将“soup”定义为 BBC 新闻网页的 HTML 内容。 接下来,我们将“headlines”定义为网页中所有“<h3>”标签的数组。 最后,脚本遍历“headlines”数组并逐一显示其所有内容,去除其外部HTML的每个元素并使用“text.strip()”方法仅显示其文本内容。

soup = BeautifulSoup(response.text, 'html.parser')
headlines = soup.find('body').find_all('h3')
for x in headlines:
	print(x.text.strip())

  下面是实现:

import requests
from bs4 import BeautifulSoup

url = 'https://www.bbc.com/news'
response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')
headlines = soup.find('body').find_all('h3')
for x in headlines:
	print(x.text.strip())

  输出:

输出数据

  推荐:Python模块

清理数据

  您可能已经注意到,您的输出包含重复的新闻标题和不是新闻标题的文本内容。

  创建要删除的所有文本元素的列表:

unwanted = [‘BBC World News TV’, ‘BBC World Service Radio’, ‘News daily newsletter’, ‘Mobile app’, ‘Get in touch’]

  然后仅当文本元素不在此列表中时才打印文本元素:

Print(x.text.strip()) 

下面是实现:

import requests
from bs4 import BeautifulSoup

proxy = {
'https': 'socks5://127.0.0.1:10808',
'http': 'socks5://127.0.0.1:10809'
}

url = 'https://www.bbc.com/news/'
response = requests.get(url,proxies=proxy)

soup = BeautifulSoup(response.text, 'html.parser')
print(soup)
headlines = soup.find('body').find_all('h3')
unwanted = ['BBC World News TV', 'BBC World Service Radio',
			'News daily newsletter', 'Mobile app', 'Get in touch']

for x in list(dict.fromkeys(headlines)):
	if x.text.strip() not in unwanted:
		print(x.text.strip())

输出:

输出清理后新闻数据

总结

  以上是晓得博客为你介绍的如何使用Python获取每日新闻的全部内容,希望对你学习Python有所帮助。如有问题,欢迎留言讨论。

  推荐:零基础如何开始学习Python

给文章评分

晓得博客,版权所有丨如未注明,均为原创
晓得博客 » 如何使用Python获取每日新闻

转载请保留链接:https://www.pythonthree.com/how-to-get-the-daily-news-using-python/

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部