抓取黑马程序员论坛人工智能板块的帖子
抓取的方式有很多,以下是一种基于Python的示例代码:
import requests
from bs4 import BeautifulSoup
url = 'https://bbs.itheima.com/forum-ai-1.html'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
posts = soup.select('.xst') # 获取帖子标题
for post in posts:
print(post.get_text()) # 输出帖子标题
这段代码使用了requests库发送HTTP请求,以及BeautifulSoup库对HTML进行解析。其中,url指定了要抓取的页面地址,headers则是模拟浏览器发送请求的头信息。接着,使用requests.get()方法获取页面内容,再用BeautifulSoup库解析出标题,最后循环输出标题内容。
原文地址: https://www.cveoy.top/t/topic/bqEu 著作权归作者所有。请勿转载和采集!