爬虫爬取某个网站的新闻包括时间地名人名代码示例
以下是一个简单的Python爬虫示例,它可以从一个新闻网站上抓取新闻标题、发布时间、地名和人名。
import requests
from bs4 import BeautifulSoup
url = "https://news.example.com/"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
articles = soup.find_all("article")
for article in articles:
title = article.find("h2").text.strip()
date = article.find("time")["datetime"]
location = article.find("span", class_="location").text.strip()
person = article.find("span", class_="person").text.strip()
print("Title:", title)
print("Date:", date)
print("Location:", location)
print("Person:", person)
print()
在这个示例中,我们首先使用requests库发送了一个GET请求,然后使用BeautifulSoup库解析HTML响应。我们使用find_all()方法查找所有的文章,并使用find()方法查找每篇文章中的标题、日期、地点和人物。最后,我们打印这些信息到控制台上。
当然,具体的网站结构可能会有所不同,需要根据实际情况进行调整。同时,也需要注意网站的爬取规则和法律法规
原文地址: https://www.cveoy.top/t/topic/dHzX 著作权归作者所有。请勿转载和采集!