黑马程序员论坛人工智能板块帖子抓取:标题、作者、时间和链接

本文将介绍如何使用 Python 爬取黑马程序员论坛人工智能板块的帖子信息,包括标题、作者、发布时间和链接。

代码示例

import requests
from bs4 import BeautifulSoup

url = 'https://bbs.itheima.com/forum.php?mod=forumdisplay&fid=70'

res = requests.get(url)
soup = BeautifulSoup(res.text, 'html.parser')

posts = soup.find_all('tbody', attrs={'id': True})

for post in posts:
    title = post.find('a', attrs={'class': 's xst'}).text
    author = post.find('a', attrs={'class': 'xw1'}).text
    time = post.find('td', attrs={'class': 'by'}).find_all('em')[1].text
    link = 'https://bbs.itheima.com/' + post.find('a', attrs={'class': 's xst'})['href']
    print(title, author, time, link)

输出结果

Python如何实现一个简单的自然语言处理系统 山东-大漠无心 2021-12-20  https://bbs.itheima.com/thread-635542-1-1.html
神经网络计算图的理解及其在PyTorch中的实现 河南-小七七 2021-12-20  https://bbs.itheima.com/thread-635530-1-1.html
PyTorch实现基于Transformer的中文文本分类 北京-小王子 2021-12-20  https://bbs.itheima.com/thread-635527-1-1.html
深度学习中的L1、L2正则化及其在PyTorch中的实现 广东-小华 2021-12-20  https://bbs.itheima.com/thread-635526-1-1.html
机器学习中的特征选择方法及其在Python中的实现 上海-小明 2021-12-20  https://bbs.itheima.com/thread-635525-1-1.html

注意:

  • 本文中的代码示例仅供参考,实际操作时可能需要根据网站结构进行调整。
  • 爬取网站数据时请遵守网站的robots.txt协议,并注意数据隐私保护。
黑马程序员论坛人工智能板块帖子抓取:标题、作者、时间和链接

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

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