python+Selenium 爬虫爬取慕课网课程评价,并保存为excel

主要功能:

1、爬取慕课网课程评价

2、将课程评价保存到Excel

 

使用:python3.6 selenium Chrome浏览器

python包:selenium、BeautifulSoup、pandas、

部分代码参考:https://blog.csdn.net/weixin_43330908/article/details/82959940

from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd

if __name__ == '__main__':
    writer = pd.ExcelWriter("./mooc课程评论.xls") #设置保存Excel 路径
    driver = webdriver.Chrome(executable_path=".\Chrome\chromedriver.exe") #设置chrome驱动
    url = 'https://www.icourse163.org/course/BIT-268001'  #设置要爬取的课程链接
    #["用户","内容","时间","点赞数","第几次课程"] 待爬取的内容

    driver.get(url)
    cont = driver.page_source
    soup = BeautifulSoup(cont, 'html.parser')
    ele = driver.find_element_by_id("review-tag-button")  # 点击 课程评价
    ele.click()
    xyy = driver.find_element_by_class_name("ux-pager_btn__next")  # 翻页功能
    connt = driver.page_source
    soup = BeautifulSoup(connt, 'html.parser') #得到网页源代码

    all_table = [] #保存所需数据
    all_table.append(["用户","内容","时间","点赞数","第几次课程"])

    for i in range(1374):  # 共1373页
        xyy.click()
        connt = driver.page_source
        soup = BeautifulSoup(connt, 'html.parser')
        content = soup.find_all('div', {
            'class': 'ux-mooc-comment-course-comment_comment-list_item_body'})  # 全部评论

        for ctt in content:
            #获取用户名
            user_name = ctt.find("a",{"class":"primary-link ux-mooc-comment-course-comment_comment-list_item_body_user-info_name"})
            user_name = user_name.text
            print(user_name)

            #发布时间
            publish_time = ctt.find('div', {'class': 'ux-mooc-comment-course-comment_comment-list_item_body_comment-info_time'})
            publish_time = publish_time.text
            publish_time = publish_time[4:]
            print(publish_time)

            #第几次课程
            course_nums = ctt.find('div', {'class': 'ux-mooc-comment-course-comment_comment-list_item_body_comment-info_term-sign'})
            course_nums = course_nums.text
            course_nums = course_nums.replace(" ","")
            course_nums = course_nums.replace("\n", "")
            print(course_nums)

            scontent = []
            aspan = ctt.find_all('span')
            for span in aspan:
                scontent.append(span.string)
            
            #点赞数
            like = scontent[5] 
            
            #课程内容
            scontent = scontent[1]
            print(scontent)
            
            all_table.append([user_name,scontent,publish_time,like,course_nums])
    
    #保存到Excel
    all_table = pd.DataFrame(all_table)
    all_table.to_excel(writer, index=False)
    writer.save()
  • 12
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值