抓取黑马程序员论坛数据python代码
时间: 2024-06-15 12:06:50
浏览: 137
抓取黑马程序员论坛数据的Python代码可以使用第三方库requests和BeautifulSoup来实现。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 发送HTTP请求获取页面内容
url = "https://bbs.itheima.com/"
response = requests.get(url)
html = response.text
# 使用BeautifulSoup解析页面内容
soup = BeautifulSoup(html, "html.parser")
# 获取帖子列表
post_list = soup.find_all("div", class_="post-item")
# 遍历帖子列表并提取信息
for post in post_list:
title = post.find("a", class_="post-title").text.strip()
author = post.find("a", class_="post-author").text.strip()
date = post.find("span", class_="post-date").text.strip()
print("标题:", title)
print("作者:", author)
print("日期:", date)
print("--")
这段代码使用requests库发送HTTP请求获取