Python爬虫下载Bilibili番剧弹幕
本文绍如何利用python爬虫下载bilibili番剧弹幕。
准备:
- python3环境
- 需要安装BeautifulSoup,selenium包
- phantomjs
原理:
- 通过aid下载bilibili番剧弹幕
- 通过aid获取cid,如: http://www.bilibili.com/widget/getPageList?aid=9654289
- 下载弹幕地址:http://comment.bilibili.com/cid.xml
代码:
# -*- coding: utf-8 -*-
import requests
import json
import urllib.request
import zlib
import os
import re
from bs4 import BeautifulSoup
from urllib.parse import quote
from selenium import webdriver
headers = { 'Accept':'*/*',
'Accept-Encoding':'gzip, deflate, sdch, br',
'Accept-Language':'zh-CN,zh;q=0.8',
'Access-Control-Request-Headers':'content-encoding, content-type, x-za-batch-size, x-za-log-version, x-za-platform, x-za-product',
'Access-Control-Request-Method':'POST',
'Cache-Control':'no-cache',
'Connection':'keep-alive',
'Host':'bilibili-web-analytics.bilibili.com',
'Origin':'https://www.bilibili.com',
'Pragma':'no-cache',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'
}
for key, value in enumerate(headers):
webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.{}'.format(key)] = value
driver = webdriver.PhantomJS(executable_path='./phantomjs-2.1.1-windows/bin/phantomjs.exe')
def geAidByKeyword(Keyword):
print('正在搜索,请等待......')
search_url="http://search.bilibili.com/bangumi?keyword="+Keyword
search_url=quote(search_url,safe='/:?=')
search_html = urllib.request.urlopen(search_url)
search_bsObj = BeautifulSoup(search_html,'html.parser')
search_linkList = search_bsObj.findAll("a",{"class":"title"})
count=len(search_linkList)
print('已找到%s个' %count)
i=0
for item in search_linkList:
print('%s:%s' % (i,search_linkList[i]['title']))
i=i+1
# 选择
select=input('请选择你要下载的编号:')
select=int(select)
# print(select)
search_link=search_linkList[select]['href']
if search_link=='':
print('输入无效字符')
return False
# 进入
# print(search_link)
search_link="http:"+search_link
select_link_html = urllib.request.urlopen(search_link)
select_link_bsObj = BeautifulSoup(select_link_html,'html.parser')
select_link_list=select_link_bsObj.findAll("li",{"data-newest-ep-id":re.compile("[0-9]+")})
data_season_id=select_link_list[0]['data-season-id']
data_newest_ep_id=select_link_list[0]['data-newest-ep-id']
new_aid_url="http://bangumi.bilibili.com/anime/%s/play#%s" % (data_season_id,data_newest_ep_id)
getNewAid(new_aid_url)
# print(data_season_id)
def getNewAid(url):
driver.get(url)
html = driver.page_source
bsObj = BeautifulSoup(html,'html.parser')
aid_list=bsObj.findAll("a",{"class":"v-av-link"})
aid=aid_list[0].get_text()
aid=aid[2:]
checkAndDown(aid)
def checkAndDown(aid):
title=getAnimeName(aid)
check=input('你要下载的是%s,是否要下载(1:是;0:否):' %title)
print(check)
if int(check)==1:
getPageList(aid)
else:
return False
def getAnimeName(aid):
# print(aid)
html = urllib.request.urlopen("http://www.bilibili.com/video/av%s" %aid)
bsObj = BeautifulSoup(html,'html.parser')
try:
title=bsObj.find("title").get_text()
except Exception as e:
print('获取失败,aid=%s' %aid)
return False
return title
def getPageList(aid):
url="http://www.bilibili.com/widget/getPageList"
params = {
'aid':aid
}
re = requests.get(url,params)
cidDic=json.loads(re.text)
animeName=getAnimeName(aid)
for cidItem in cidDic:
cidItem['animeName']=animeName
downloadDanmu(cidItem)
def downloadDanmu(cidItem):
cid=cidItem['cid']
animeName=cidItem['animeName']
pagename=cidItem['pagename']
comment_url="http://comment.bilibili.com/%s.xml" %cid
comment_page_zip=urllib.request.urlopen(comment_url).read()
comment=zlib.decompressobj(-zlib.MAX_WBITS).decompress(comment_page_zip)
if os.path.exists('%s' % animeName)==False:
os.makedirs('%s' % animeName)
fout=open('%s/%s.xml' % (animeName,pagename),'wb')
fout.write(comment)
fout.close()
print('%s %s下载完成' % (animeName,pagename))
def main():
# aid=input('请输入aid:')
# getPageList(aid)
keyword=input('请输入番剧的关键字:')
geAidByKeyword(keyword)
if __name__ == '__main__':
main()
Python爬虫下载Bilibili番剧弹幕的更多相关文章
- python爬虫下载文件
python爬虫下载文件 下载东西和访问网页差不多,这里以下载我以前做的一个安卓小游戏为例 地址为:http://hjwachhy.site/game/only_v1.1.1.apk 首先下载到内存 ...
- Python 批量下载BiliBili视频 打包成软件
文章目录 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给大家 ...
- Python爬虫爬取美剧网站
一直有爱看美剧的习惯,一方面锻炼一下英语听力,一方面打发一下时间.之前是能在视频网站上面在线看的,可是自从广电总局的限制令之后,进口的美剧英剧等貌似就不在像以前一样同步更新了.但是,作为一个宅diao ...
- bilibili番剧评分爬虫
python选修课学习中练手写的,主要就是查询bilibili提供得api # -*- coding:utf-8 -*- import requests import json import csv ...
- Python爬虫下载美女图片(不同网站不同方法)
声明:以下代码,Python版本3.6完美运行 一.思路介绍 不同的图片网站设有不同的反爬虫机制,根据具体网站采取对应的方法 1. 浏览器浏览分析地址变化规律 2. Python测试类获取网页内容,从 ...
- Python爬虫下载酷狗音乐
目录 1.Python下载酷狗音乐 1.1.前期准备 1.2.分析 1.2.1.第一步 1.2.2.第二步 1.2.3.第三步 1.2.4.第四步 1.3.代码实现 1.4.运行结果 1.Python ...
- python 爬虫 下载图片
import os#导入操作系统模块from urllib.request import urlretrieve#下载url对应的文件from urllib.request import urlope ...
- python 爬虫--下载图片,下载音乐
#下载图片 imgUrl='http://www.pptbz.com/pptpic/UploadFiles_6909/201211/2012111719294197.jpg' r=requests.g ...
- python 爬虫下载英语听力新闻(npr news)为mp3格式
想通过听实时新闻来提高英语听力,学了那么多年的英语,不能落下啊,不然白费背了那么多年的单词. npr news是美国国家公共电台,发音纯正,音频每日更新,以美国为主,世界新闻为辅,比如最近我国武汉发生 ...
随机推荐
- 【SP2713 GSS4 - Can you answer these queries IV】 题解
题目链接:https://www.luogu.org/problemnew/show/SP2713 真暴力啊. 开方你开就是了,开上6次就都没了. #include <cmath> #in ...
- HDU 1213(裸并查集)(无变形)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/ ...
- Reading Fast Packet Processing A Survey
COMST 2018 主要内容 这是一篇有关快速包转发的综述,先介绍了包转发的有关基础知识和背景,具体介绍了包转发的主流方法,对这些方法进行了细致详尽的比较,最后介绍了最新的方法和未来的研究方向. 包 ...
- winrar 弹窗处理
https://www.rarlab.com/ 1.下载英文版 2.把下面这段code文本复制到一个新建的记事本txt文档中,然后另存为rarreg.key文件,注意后缀名.txt改为.key才行. ...
- redis集群环境搭建的错误
安装redis集群需要版本号在3.0以上 redis-cluster安装前需要安装ruby环境 搭建集群需要使用到官方提供的ruby脚本. 需要安装ruby的环境. yum -y install ru ...
- ansible常用配置
1.什么是Ansible 部署参考连接:http://www.ansible.com.cn/ ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfeng ...
- ionic2添加支付宝插件出现问题
安装本地路径插件正常 编译正常 在打开支付页面时候 就报这个错 在手机app点击无效 错误信息: ERROR Error: Uncaught (in promise): Error: No pro ...
- PHP目前比较常见的五大运行模式
做 php 开发的应该都知道 php 运行模式概念吧,本文将要和大家分享的是关于php目前比较常见的五大运行模式:包括cgi .fast-cgi.cli.isapi.apache模块的DLL ,下面作 ...
- Jlink-10 pin 的定义(stm32使用)官方定义
因为在网上找了好久才找到正确的接法,所以专门记载了下来,因为stm32芯片这几个功能引脚会内置上拉电阻,所以不需要再外接电阻了.
- Pyhton-类(2)
·类(2) @ 继承(inheritance) 什么是继承: B继承A:A是父类(超类),B是子类(基类).继承可以实现代码重复利用,实现属性和方法继承. 继承可以使子类拥有父类的属性和方法,也可以重 ...