最近一段时间,许多老师利用腾讯课堂教学,可通过如下方法获取课程名称。
导入库
import requests
from bs4 import BeautifulSoup
import bs4
获取网页信息
def getHTMLText(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""
提取课程名称
def fillCourseList(clist, html):
soup = BeautifulSoup(html, "html.parser")
clist.append(soup.title.string[0:-12])
cclist = []
for li in soup.find('ol').contents[3]('ol')[0].children:
if isinstance(li, bs4.element.Tag):
nt = li('div')[1]
cclist.append([nt('h5')[0].string, nt('span')[0].string])
clist.append(cclist)
输出为markdown文件
'''
start 起始网址代码(包括start)
end 结束网址代码(不包括end)
fname 数据保存到文件的名称(不包括扩展名)
mode 是否重新生成文件
True 是
False 否
'''
def printUnivList(start, end, fname='CourseList', mode=False):
if mode == True:
with open('{}.md'.format(fname), 'w', encoding = 'UTF-8') as f:
f.write('## 腾讯课堂课程汇总\n\n|课程名称|特征数字|详细信息|课程网址|\n|:----:|:----:|:----:|:----:|\n')
for n in range(start,end):
try:
cinfo = []
url = 'https://ke.qq.com/course/{}'.format(n)
html = getHTMLText(url)
fillCourseList(cinfo, html)
cinfo.append(str(n))
except:
continue
CourseListString = '|' + cinfo[0] + '|' + cinfo[2] + '|' + '[点击进入](./CourseDetail/{}.md)'.format(n) + \
'|[点击进入](https://ke.qq.com/course/' + cinfo[2] + ')|\n'
with open('{}.md'.format(fname), 'a', encoding = 'UTF-8') as f:
f.write(CourseListString)
if mode == True:
with open('./CourseDetail/{}.md'.format(n), 'w', encoding = 'UTF-8') as f:
f.write('## {}  课程详情\n\n|课程名称|课程时间|\n|:----:|:----:|\n'.format(cinfo[0]))
for c in cinfo[1]:
with open('./CourseDetail/{}.md'.format(n), 'a', encoding = 'UTF-8') as f:
f.write('|'+c[0]+'|'+c[1]+'|\n')
print("\r {}已完成".format(n))
执行文件
print('开始爬取。。。')
printUnivList(1000000, 1001000, fname='CourseList(1000000-1001000)', mode=True)
input('爬取结束,按任意键退出。。。')
附录:源文件
附录:部分结果演示
注:此处详细信息无作用
腾讯课堂课程汇总
课程名称 | 特征数字 | 详细信息 | 课程网址 |
---|---|---|---|
C语言程序设计 | 1000076 | 点击进入 | 点击进入 |
基础化学 | 1000078 | 点击进入 | 点击进入 |
大学物理 III (计科/网络) | 1000080 | 点击进入 | 点击进入 |
针织时装工艺CAD | 1000082 | 点击进入 | 点击进入 |
详细信息展示
大学物理 III (计科/网络) 课程详情
课程名称 | 课程时间 |
---|---|
第37讲 总复习 | 6月11日 10:01-11:42 |
第36讲 电磁感应习题课 | 6月9日 10:02-11:42 |
第35讲 互感 自感 | 6月8日 07:55-09:36 |
第34讲 动生电动势 感生电动势 | 6月4日 10:02-11:41 |
第33讲 法拉第电磁感应定律 动生电动 | 6月1日 07:50-09:35 |
第32讲 磁学习题课 | 5月28日 10:00-11:40 |
第31讲 磁场对载流线圈的作用力 | 5月26日 09:59-11:41 |
第30讲 磁场对载流导线的作用力 | 5月25日 07:56-09:36 |
第29讲 磁场对运动电荷的作用 洛伦 | 5月21日 09:58-11:40 |
第28讲 磁场习题课 | 5月18日 07:56-09:37 |
第27讲 安培环路定理的应用 | 5月14日 10:00-11:42 |
第26讲 磁场的高斯定律 安培环路定理 | 5月12日 09:58-11:40 |