爬取jove网站视频字幕

 以章节为爬取单元,直接运行即可

from urllib import request
from bs4 import BeautifulSoup
import os
import shutil
import requests

host = "https://www.jove.com"

##添加章节链接
chapters = ["https://www.jove.com/science-education-library/47/cell-structure-and-function",
           "https://www.jove.com/science-education-library/48/membranes-and-cellular-transport"]

##扫描章节中的lessons
lessons = {}
for i in range(len(chapters)):
    print(i)
    s = request.urlopen(chapters[i])
    soup = BeautifulSoup(s,"html.parser")
    lesson = soup.find_all('div', class_='lesson-title')
    for les in lesson:
        if ":" in les.find("a").getText().strip() and les.find("a").getText().strip()[0].isdigit():
            lessons[les.find("a").getText().strip()] = host+les.find("a").get("href")
        else:
            print("不存在"+les.find("a").getText().strip())

##以txt文件格式保存字幕
for key in lessons:
    print("title:"+key)
    soup_lesson = BeautifulSoup(request.urlopen(lessons[key]),"html.parser")
    transcript = soup_lesson.find(id="transcript")
    tran_p = transcript.find_all("p")
    content = ""
    for i in tran_p:
        content += i.getText()+"\n\n"
        file = open(key+".txt","w")
        file.write(content)
        file.close()

print("爬取完成")

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值