爬取https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.html页面中的新闻数据。
时间: 2023-10-31 17:59:18 浏览: 25
为了爬取 https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.html 页面中的新闻数据,你可以使用 Python 中的 requests 和 BeautifulSoup 库。以下是一个简单的爬虫示例:
```python
import requests
from bs4 import BeautifulSoup
# 发送请求
url = 'https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.html'
response = requests.get(url)
# 解析 HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 查找新闻元素
news_list = soup.find_all('div', class_='zx-list-item-wrap')
# 打印新闻标题和链接
for news in news_list:
title = news.find('a').text.strip()
link = news.find('a')['href']
print(title)
print(link)
```
这个示例中,我们首先使用 requests 库发送 GET 请求并获取响应。然后,我们使用 BeautifulSoup 库将 HTML 解析为可操作的对象。我们可以使用 `find_all` 方法查找所有具有 `zx-list-item-wrap` 类的 div 元素,这些元素包含我们要爬取的新闻。最后,我们遍历新闻元素列表,并从每个元素中提取标题和链接。