由于黑马程序员论坛需要登录账号才能访问,因此需要先登录账号才能进行抓取。以下是使用Python requests和BeautifulSoup库进行抓取的代码:

import requests
from bs4 import BeautifulSoup

# 登录账号
login_url = 'http://bbs.itheima.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1'
username = 'your_username'
password = 'your_password'
data = {'fastloginfield': 'username', 'username': username, 'password': password, 'quickforward': 'yes', 'handlekey': 'ls'}

session = requests.session()
session.post(login_url, data=data)

# 抓取人工智能板块帖子
url = 'http://bbs.itheima.com/forum.php?mod=forumdisplay&fid=185'
response = session.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

# 获取帖子的标题、作者、发布时间以及链接
posts = soup.find_all('tbody', id=lambda x: x and x.startswith('normalthread_'))
for post in posts:
    title = post.find('a', class_='s xst').text
    author = post.find('a', class_='xw1').text
    time = post.find('td', class_='by').em.text
    link = post.find('a', class_='s xst')['href']
    print(title, author, time, link)

需要将your_usernameyour_password替换为自己的账号信息。运行后会输出所有帖子的标题、作者、发布时间以及链接。

访问黑马程序员论坛抓取人工智能板块帖子获取帖子的标题、作者、发布时间以及链接

原文地址: https://www.cveoy.top/t/topic/bqt5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录