【python代码:模拟浏览器访问B站,并下载java相关的视频教程】

帮我写段代码:模拟浏览器访问B站,并下载java相关的视频教程。使用python和go两种方式

Python实现:

import requests
from bs4 import BeautifulSoup
import re
import subprocess

# 获取B站Java教程页面的视频链接
url = "https://search.bilibili.com/all?keyword=java%E6%95%99%E7%A8%8B&order=click&duration=0&tids_1=0&page=1"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
elems = soup.find_all('a', {'class': 'title'})
urls = []
for elem in elems:
    urls.append("https:"+elem['href'])

# 下载视频
for url in urls:
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
    match = re.search(r'"baseUrl":"(.*?)"', str(soup))
    if match:
        video_url = match.group(1).replace("\\", "")
        subprocess.call(["wget", "-O", "video.mp4", video_url])

Go实现:

package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"regexp"
)

func main() {
	// 获取B站Java教程页面的视频链接
	resp, _ := http.Get("https://search.bilibili.com/all?keyword=java%E6%95%99%E7%A8%8B&order=click&duration=0&tids_1=0&page=1")
	defer resp.Body.Close()

	bodyBytes, _ := io.ReadAll(resp.Body)
	body := string(bodyBytes)

	re := regexp.MustCompile(`class='title' href='(.*?)' target`)
	matches := re.FindAllStringSubmatch(body, -1)

	urls := []string{}
	for _, match := range matches {
		urls = append(urls, "https:"+match[1])
	}

	// 下载视频
	for _, url := range urls {
		resp, _ := http.Get(url)
		defer resp.Body.Close()
		bodyBytes, _ := io.ReadAll(resp.Body)
		body := string(bodyBytes)

		re := regexp.MustCompile(`"baseUrl":"(.*?)"`)
		match := re.FindStringSubmatch(body)

		if len(match) > 0 {
			video_url := match[1]
			video_url = regexp.MustCompile(`\\`).ReplaceAllString(video_url, "")
			f, _ := os.Create("video.mp4")
			defer f.Close()
			resp, _ := http.Get(video_url)
			defer resp.Body.Close()
			io.Copy(f, resp.Body)
			fmt.Println("Downloaded video from", url)
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值