#导入相应的库
# python爬虫之Beautifulsoup模块用法详解 - 知乎 (zhihu.com)
from bs4 import BeautifulSoup
# python中requests库使用方法详解 - 知乎 (zhihu.com)
import requests# python——time库整理(基础知识+实例) - 知乎 (zhihu.com)
import time
# 导入所需的库
from bs4 import BeautifulSoup
import requests
import time
# 定义一个函数以从给定的URL中抓取信息
def get_info(url):
# 发送HTTP GET请求到URL
res = requests.get(url)
res.encoding = 'utf-8'
# 创建一个BeautifulSoup对象以解析HTML内容
soup = BeautifulSoup(res.text, features='lxml')
# 查找具有类名 "list" 的HTML元素
title = soup.find(name='div', attrs={"class": "list"})
# 遍历 "list" 元素中的列表项
for i in title.find_all("li"):
# 从每个列表项中提取时间和标题信息
time = i.find("span").string
title = i.find("a").string
# 打印时间和标题信息
print(time + title)
# 从88递减到1进行迭代
for i in range(88, 0, -1):
if (i == 88):
# 对于第一次迭代,使用基本URL
urls = ['https://www.szpt.edu.cn/old/szxw/szyw.htm']
else:
# 对于后续迭代,使用带有页数的URL
urls = ['https://www.szpt.edu.cn/old/szxw/szyw/{}.htm'.format(i)]
# 遍历URL列表
for url in urls:
# 调用get_info函数以从URL中抓取和打印信息
get_info(url)
# 休眠1秒钟(需要导入time模块才能工作)
time.sleep(1)