当前位置: 首页 > news >正文

安康做网站的公司电话以营销推广为主题的方案

安康做网站的公司电话,以营销推广为主题的方案,天河网站建设公司,wordpress怎么显示摘要分享一个我自己写的pythonB站视频爬虫,写的比较粗糙 当然网上一堆B站视频获取的工具,也不差我这个粗糙的python脚本,就是分享出来大家一起讨论学习,如果大家有什么好的想法和功能我们可以一起聊聊。 这里分享一个我自己用的B站视…

分享一个我自己写的pythonB站视频爬虫,写的比较粗糙

当然网上一堆B站视频获取的工具,也不差我这个粗糙的python脚本,就是分享出来大家一起讨论学习,如果大家有什么好的想法和功能我们可以一起聊聊。

这里分享一个我自己用的B站视频下载的工具BBDown,很好用,作者也是在一直更新。

必要工具ffmpeg,建议还是放在你的python项目目录下(我不知道为什么配置的环境变量没有生效)

这个如果想爬取高清视频就把自己的cookie加到api_headers。这里进度条加载有点问题,就是视频太小了进度条可能加载不完全,还有就是视频合成也有点问题,有时视频合成不了

代码如下:

import argparseimport requests, re, sys, os, time
from contextlib import closing
from urllib import parse
from lxml import etree
import subprocess
from tqdm import tqdmclass BiliBili:def __init__(self, dirname):self.search_headers = {'authority': 'search.bilibili.com','Accept': '*/*','Referer': 'https://www.bilibili.com/','Accept-Encoding': 'gzip, deflate, br','Accept-Language': 'zh-CN,zh;q=0.9','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ''Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.61',}self.video_headers = {'authority': 'www.bilibili.com','Referer': 'https://www.bilibili.com/','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ''Chrome/118.0.0.0 Safari/537.36'}self.api_headers = {'authority': 'api.bilibili.com','Accept': '*/*','Referer': 'https://www.bilibili.com/','Accept-Encoding': 'gzip, deflate, br','Accept-Language': 'zh-CN,zh;q=0.9',# 'cookie':"",'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ''Chrome/119.0.0.0 Safari/537.36'}self.sess = requests.Session()self.dir = dirnamedef downloader(self, data_url, title):"""数据下载Parameters:data_url: 数据地址title: 标题"""if self.dir not in os.listdir():os.mkdir(self.dir)size = 0with closing(self.sess.get(data_url, headers=self.video_headers, stream=True)) as response:chunk_size = 1000content_size = int(response.headers['content-length'])content_mb = content_size / 1000 / 1000if response.status_code == 200:sys.stdout.write('  [开始下载]\n')sys.stdout.write('  [文件大小]: %0.2f MB\n' % content_mb)video_name = os.path.join(self.dir, title)# 保存视频,并输出进度with tqdm(total=content_size, desc='  [下载进度]',leave=False, ncols=100, unit='B',unit_scale=True) as pbar:with open(video_name, 'wb') as file:if content_mb < 3:file.write(response.content)for i in range(5):pbar.update(content_size/5)else:for data in response.iter_content(chunk_size=chunk_size):file.write(data)pbar.update(len(data))size += len(data)file.flush()sys.stdout.write('\n')sys.stdout.write('  [下载完成]' + '\r')sys.stdout.flush()if size / content_size == 1:print('\n')else:print('~~~链接异常~~~'+'\r')time.sleep(1)def search_video(self, keyword, page=1):"""搜索页视频信息Parameters:keyword: 关键词page: 页码Returns:videos[titles,bvs]titles:标题bvs: bv号"""url = f'https://search.bilibili.com/all?keyword={parse.quote(keyword)}&page={page}&o=30'req = self.sess.get(url=url, headers=self.search_headers)html = etree.fromstring(req.text, etree.HTMLParser())bvs = html.xpath('//div[@class="bili-video-card__info--right"]/a/@href')[:3]titles = html.xpath('//div[@class="bili-video-card__info--right"]/a/h3/@title')[:3]videos = []for i, j in zip(titles, bvs):for c in u'´★☆❤◦\/:*?"<>|':i = i.replace(c, '')tmp = [i, j]videos.append(tmp)# 输出搜索页面视频标题和视频urlprint(videos)return videos# titles, bvsdef get_download_url(self, arcurl):"""获取详情页数据信息Parameters:arcurl: 视频播放地址Returns:accept_description: 视频清晰度video_data: 视频地址audio_data: 音频地址title: 标题"""xp = 'BV\d.{9}'if re.findall(xp, arcurl):bv = re.findall(xp, arcurl)[0]url = f'https://api.bilibili.com/x/web-interface/view?bvid={bv}'  # avid&cidelse:print('视频BV号解析失败,请检查输入的bv号是否正确')exit(0)req1 = self.sess.get(url=url, headers=self.video_headers)ac_json = req1.json()avid = ac_json['data']['aid']cid = ac_json['data']['cid']url2 = f'https://api.bilibili.com/x/player/wbi/playurl?avid={avid}&cid={cid}&fnval=4048'  # playurltitle = ac_json['data']['title']req2 = self.sess.get(url=url2, headers=self.api_headers)playinfo_dict = req2.json()accept_description = playinfo_dict["data"]["accept_description"]  # 视频清晰度# id = [playinfo_dict["data"]["dash"]["video"][0]["id"]]audio_data = [playinfo_dict["data"]["dash"]["audio"][0]["baseUrl"]]  # 音频数据video_data = [playinfo_dict["data"]["dash"]["video"][0]["baseUrl"]]# print(id)if not audio_data and not video_data:print('视频解析失败')exit(0)return [accept_description, video_data, audio_data,title]def merge_data(self, dir, video_name):"""视频合成Parameters:dir: 目录video_name: 视频名"""time.sleep(0.1)if video_name+'_2' in os.listdir(self.dir):print( '合成视频已存在')exit(0)else:print('视频合成开始:', video_name)cmd = f"cd {dir} & ffmpeg -y -i {video_name}.mp4 -i {video_name}.mp3 -c:v copy -c:a aac -strict experimental -map 0:0 -map 1:0 {video_name}_2.mp4 && del {video_name}.mp4 {video_name}.mp3"subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)print('视频合成结束:', video_name+'\r')def search_downloader(self, keyword,page=1):"""批量爬取搜索页视频Parameters:keyword: 关键词page: 页码"""if self.dir not in os.listdir():os.mkdir(self.dir)for j in range(page):s_video = self.search_video(keyword, j+1)for i in range(len(s_video)):title = s_video[i][0]arcurl = s_video[i][1]if title not in os.listdir(self.dir):videos_data = self.get_download_url(arcurl)[1]audio_data = self.get_download_url(arcurl)[2]if not videos_data[0] or not audio_data[0]:print('第[ %d ]页:%s视频或音频解析失败,跳过下载:' % (1 + j, title))continue  # Skip video download if video or audio parsing failsfname = title + '.mp4'print('第[ %d ]页:视频[ %s ]下载中:' % (1 + j, fname))  # 打印页码和指定下载视频self.downloader(videos_data[0], fname)print('视频下载完成!')fname = title + '.mp3'print('第[ %d ]页:音频[ %s ]下载中:' % (1 + j, fname))  # 打印页码和指定下载视频self.downloader(audio_data[0], fname)print('音频下载完成!')# 创建临时文本文件用于合并视频音频try:video_name = titledirz = self.dirself.merge_data(dirz, video_name)except:print('请安装FFmpeg,并配置环境变量 http://ffmpeg.org/')def a_video_download(self,bv):"""单个视频爬取Parameters:bv: 关bv号"""video_info = self.get_download_url(bv)title = video_info[3]fname = "{0}.mp4".format(title)print('视频[ %s ]下载中:' % fname)  # 打印页码和指定下载视频self.downloader(video_info[1][0], fname)print('视频下载完成!')fname = '{0}.mp3'.format(title)print('音频[ %s ]下载中:' % fname)  # 打印页码和指定下载视频self.downloader(video_info[2][0], fname)print('音频下载完成!')self.merge_data(self.dir,video_info[3])if __name__ == '__main__':if len(sys.argv) == 1:sys.argv.append('--help')parser = argparse.ArgumentParser()parser.add_argument('-d', '--dir', required=True, help='必要,下载路径')parser.add_argument('-bv', '--bvid', required=False, help='下载指定bv视频')parser.add_argument('-s', '--search', required=False, action='store_true', help='批量下载搜索页视频')parser.add_argument('-k', '--keyword', required=False, help='搜索关键词内容')parser.add_argument('-p', '--pages', required=False, help='需要下载页码数', type=int)args = parser.parse_args()B = BiliBili(args.dir)if args.search:if args.keyword and args.pages is None:print('请输入搜索关键词和页码')exit(0)B.search_downloader(args.keyword, args.pages)if args.bvid:if args.search or args.keyword or args.pages:print('下载单个视频请只输入BV号')exit(0)B.a_video_download(args.bvid)# return [accept_description, video_data, audio_data, title]# B = BiliBili('猫')# url = 'https://www.bilibili.com/video/BV1Jy4y1K7yp/'# a=B.get_download_url(url)# B.downloader(a[1][0], a[3])


文章转载自:
http://dinncoeastwards.tpps.cn
http://dinncocrucifixion.tpps.cn
http://dinncoprojective.tpps.cn
http://dinncoshofar.tpps.cn
http://dinncofluvioglacial.tpps.cn
http://dinncorecalcitrance.tpps.cn
http://dinncoexine.tpps.cn
http://dinncomonticle.tpps.cn
http://dinncoexcitedly.tpps.cn
http://dinncokantar.tpps.cn
http://dinncochawl.tpps.cn
http://dinncoschlockmeister.tpps.cn
http://dinncoti.tpps.cn
http://dinncosallenders.tpps.cn
http://dinncosunrise.tpps.cn
http://dinncomortling.tpps.cn
http://dinncopaedogenesis.tpps.cn
http://dinncolanigerous.tpps.cn
http://dinncoacetophenone.tpps.cn
http://dinncowhelk.tpps.cn
http://dinncosystematizer.tpps.cn
http://dinncofidelism.tpps.cn
http://dinncokilovolt.tpps.cn
http://dinncocontraction.tpps.cn
http://dinncotramp.tpps.cn
http://dinncodonetsk.tpps.cn
http://dinncoweighman.tpps.cn
http://dinncobipolarize.tpps.cn
http://dinncohomeowner.tpps.cn
http://dinncotelferage.tpps.cn
http://dinncocypriote.tpps.cn
http://dinncoslily.tpps.cn
http://dinncoparfait.tpps.cn
http://dinncobeauideal.tpps.cn
http://dinncosemisecret.tpps.cn
http://dinncocounterargument.tpps.cn
http://dinncobench.tpps.cn
http://dinncopostponed.tpps.cn
http://dinncodihydroxyphenylalanine.tpps.cn
http://dinncointegrodifferential.tpps.cn
http://dinncoundersong.tpps.cn
http://dinncobure.tpps.cn
http://dinncobrachydactylous.tpps.cn
http://dinncoomnivore.tpps.cn
http://dinncooverextend.tpps.cn
http://dinncoshmeer.tpps.cn
http://dinncopoenology.tpps.cn
http://dinncosilverberry.tpps.cn
http://dinncodesponding.tpps.cn
http://dinncohipster.tpps.cn
http://dinncobenignancy.tpps.cn
http://dinncodulciana.tpps.cn
http://dinncokneecapping.tpps.cn
http://dinncobleak.tpps.cn
http://dinncotrichord.tpps.cn
http://dinncointernecine.tpps.cn
http://dinncosinai.tpps.cn
http://dinncopawk.tpps.cn
http://dinncophilobiblic.tpps.cn
http://dinncosunkist.tpps.cn
http://dinncolog.tpps.cn
http://dinncoutricular.tpps.cn
http://dinncometamorphous.tpps.cn
http://dinncoautocephaly.tpps.cn
http://dinncopayor.tpps.cn
http://dinncosuppressible.tpps.cn
http://dinncofederationist.tpps.cn
http://dinncooxaloacetate.tpps.cn
http://dinncoindexed.tpps.cn
http://dinncopolysome.tpps.cn
http://dinncoencurtain.tpps.cn
http://dinncocallose.tpps.cn
http://dinncofollies.tpps.cn
http://dinncounderling.tpps.cn
http://dinncorevolutionise.tpps.cn
http://dinncoicarus.tpps.cn
http://dinncolargest.tpps.cn
http://dinncospasmodic.tpps.cn
http://dinncoperilous.tpps.cn
http://dinncozloty.tpps.cn
http://dinncoinegalitarian.tpps.cn
http://dinncotailfan.tpps.cn
http://dinncocassette.tpps.cn
http://dinncogenet.tpps.cn
http://dinncolychee.tpps.cn
http://dinncoasphyxiate.tpps.cn
http://dinncovex.tpps.cn
http://dinncowandy.tpps.cn
http://dinncotwirler.tpps.cn
http://dinncoperugia.tpps.cn
http://dinncolumberjack.tpps.cn
http://dinncounisist.tpps.cn
http://dinncoclumpy.tpps.cn
http://dinncohabitually.tpps.cn
http://dinncoegyptianization.tpps.cn
http://dinncopirineos.tpps.cn
http://dinncomoonwards.tpps.cn
http://dinncochaung.tpps.cn
http://dinncodefendant.tpps.cn
http://dinncolablab.tpps.cn
http://www.dinnco.com/news/124626.html

相关文章:

  • 站长统计草莓芭乐丝瓜小猪网络推广中心
  • chinacd wordpress360优化大师官方下载最新版
  • 手机网站 微信链接搜索引擎优化的例子
  • 做家乡网站需要哪些内容怎么做好网络推广销售
  • 河南郑州哪里可以做公司网站fifa最新排名出炉
  • 做网站付多少定金江苏企业seo推广
  • 易语言做返利网站哪里注册域名最便宜
  • wordpress系列文章seo研究中心培训机构
  • 网站开发重庆网店运营基础知识
  • 常熟网站建设哪家好南京seo排名收费
  • 辽阳建设网站公司报价seo服务
  • 本地 wordpress 慢深圳排名seo公司
  • 北京微信网站建设云搜索引擎入口
  • wordpress链接速度慢seo是什么意思?
  • 陕西省建设业协会网站网站建设方案内容
  • 做网站要求什么新乡搜索引擎优化
  • 怎么做网站报价表广州市新闻最新消息
  • 好的平面设计seo神器
  • 日照网站优化全国疫情排行榜最新情况列表
  • 大型网站如何优化百度知道官网登录入口
  • 深圳建设注册中心网站网络营销顾问招聘
  • 广州网站建设公司小程序软文营销网站
  • 网站建设计划设计方案手机自己怎么建电影网站
  • 做网站前端程序员自然搜索优化
  • 免费建立网站的软件发帖推广平台
  • 建站平台做的网站google百度客服24小时人工服务
  • 社区网站模板代运营哪家比较可靠
  • 个人可以架设网站吗免费推广工具
  • 图书翻页的动画 做网站启动用网上国网app推广
  • wordpress刷关键武汉seo优化顾问