爬取腾讯课堂IT-互联网分类的的课程信息存入csv文件
标签(空格分隔): python
from urllib.request import urlopen
from bs4 import BeautifulSoup
#获取IT-互联网分类每页的课程的链接
for i in range(1,35):
url = 'https://ke.qq.com/course/list?mt=1001&page='+str(i)
html = urlopen(url)
bs_obj = BeautifulSoup(html,'html.parser')
bs_obj = bs_obj.find('ul',{'class':'course-card-list'})
links = bs_obj.find_all('a',{'class':'item-tt-link'})
#准备一个文件,先将标题写入的文件中
with open('./tengxu.csv','w',encoding='utf8') as f:
f.write('课程名称,链接地址,购买人数,好评度,价格\n')
#获取每个课程的名称、链接、报名人数、好评度、价格
for link in links:
course_name = link.get_text()
course_link = 'https:' + link.attrs.get('href')
course_html = urlopen(course_link)
bs_objec = BeautifulSoup(course_html,'html.parser')
number_buyer_bs = bs_objec.find('span',{'class':'apply-num js-apply-num'})
number_buyer = number_buyer_bs.get_text()
good_evaluate_bs = bs_objec.find('span',{'class','rate-num'})
good_evaluate = good_evaluate_bs.get_text()
price_bs = bs_objec.find('span',{'class':'price'})
price = price_bs.get_text()
#将信息写入到文件中
with open('./tengxu.csv','a',encoding='utf8') as f:
f.write(course_name)
f.write(',')
f.write(course_link)
f.write(',')
f.write(number_buyer)
f.write(',')
f.write(good_evaluate)
f.write(',')
f.write(price)
f.write('\n')