-
Notifications
You must be signed in to change notification settings - Fork 0
/
bilibili.py
164 lines (138 loc) · 5.65 KB
/
bilibili.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 12 23:13:29 2022
@author: Administrator
"""
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time
import requests
import re
import bs4
import os
import webbrowser
import ffmpeg
# from urllib.request import urlretrieve
web=Chrome()
web.get("https://www.bilibili.com/")
web.find_element_by_xpath('//*[@id="nav-searchform"]/div[1]/input').send_keys("f450无人机",Keys.ENTER)
"""搜索框输入"""
time.sleep(5)
web.switch_to.window(web.window_handles[-1])
# print(web.page_source)
for i in range(3,4):
button=web.find_element_by_xpath(f'//*[@id="i_cecream"]/div[1]/div[1]/div[2]/div/div/div[2]/div/div/button[{i}]')
button.click()
time.sleep(3)
if i==2:
lis=web.find_elements_by_xpath('//*[@id="i_cecream"]/div[1]/div[1]/div[2]/div/div/div[1]/div/div[1]')
else:
lis=web.find_elements_by_xpath('//*[@id="i_cecream"]/div[1]/div[1]/div[2]/div/div/div[1]/div')
for li in lis:
text=li.find_element_by_xpath('./div/div[2]/div/div/a/h3').text
url=li.find_element_by_xpath('./div/div[2]/div/div/a')
urllink=url.get_attribute('href')
print(text)
# print(urllink)
url=urllink
res=requests.get(url)
# print(res.text)
obj=re.compile('"codecid":7},{"id":32,"baseUrl":"(?P<info>.*?)","base_url":".*?',re.S)
page=obj.finditer(res.text)
for li in page:
vudiourl=li.group("info")
# print(li.group("info"))
musobj=re.compile('{"id":30280,"baseUrl":"(?P<mus>.*?)","base_url":.*?')
muspage=musobj.finditer(res.text)
for li in muspage:
audiourl=li.group("mus")
# print(audiourl)
head={
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36",
"Referer":url,
}
vudiores=requests.get(vudiourl,headers=head)
audiores=requests.get(audiourl,headers=head)
# print(vudiores)
# print(audiores)
vudio=vudiores.content
music=audiores.content
try:
os.makedirs(f"./{text}")
except:
pass
with open(f"{text}//video.mp4",'wb') as fie:
fie.write(vudio)
with open(f"{text}//audio.mp3",'wb') as fie:
fie.write(music)
file1=f"{text}//video.mp4"
file2=f"{text}//audio.mp3"
result=f"{text}//output.mp4"
print("下载中...")
os.system(f"E:\\ffmpeg-4.4.1-full_build\\ffmpeg-4.4.1-full_build\\bin\\ffmpeg.exe -i {file1} -i {file2} -c:v copy -c:a copy -bsf:a aac_adtstoasc {result}")
time.sleep(7)
print("下载完成...")
# web=Chrome()
# web.get("https://www.bilibili.com/")
# web.find_element_by_xpath('//*[@id="nav-searchform"]/div[1]/input').send_keys("穿越机",Keys.ENTER)
# """搜索框输入"""
# time.sleep(5)
# web.switch_to.window(web.window_handles[-1])
# # print(web.page_source)
# for i in range(2,3):
# button=web.find_element_by_xpath(f'//*[@id="i_cecream"]/div[1]/div[1]/div[2]/div/div/div[2]/div/div/button[{i}]')
# button.click()
# time.sleep(3)
# if i==2:
# lis=web.find_elements_by_xpath('//*[@id="i_cecream"]/div[1]/div[1]/div[2]/div/div/div[1]/div/div[1]')
# # else:
# # lis=web.find_elements_by_xpath('//*[@id="i_cecream"]/div[1]/div[1]/div[2]/div/div/div[1]/div')
# for li in lis:
# text=li.find_element_by_xpath('./div/div[2]/div/div/a/h3').text
# url=li.find_element_by_xpath('./div/div[2]/div/div/a')
# urllink=url.get_attribute('href')
# print(text)
# # print(urllink)
# url=urllink
# res=requests.get(url)
# # print(res.text)
# playinfos=bs4.BeautifulSoup(res.text,'html.parser')
# playinfo=playinfos.select('script')
# obj=re.compile('<script>window.__playinfo__=(?P<info>.*?)</script>')
# print(playinfo[3])
# # obj=re.compile('"codecid":7},{"id":32,"baseUrl":"(?P<info>.*?)","base_url":".*?',re.S)
# # page=obj.finditer(res.text)
# # for li in page:
# # vudiourl=li.group("info")
# # # print(li.group("info"))
# # musobj=re.compile('{"id":30280,"baseUrl":"(?P<mus>.*?)","base_url":.*?')
# # muspage=musobj.finditer(res.text)
# # for li in muspage:
# # audiourl=li.group("mus")
# # print(audiourl)
# # head={
# # "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36",
# # "Referer":url,
# # }
# # vudiores=requests.get(vudiourl,headers=head)
# # audiores=requests.get(audiourl,headers=head)
# # # print(vudiores)
# # # print(audiores)
# # vudio=vudiores.content
# # music=audiores.content
# # try:
# # os.makedirs(f"./{text}")
# # except:
# # pass
# # with open(f"{text}//video.mp4",'wb') as fie:
# # fie.write(vudio)
# # with open(f"{text}//audio.mp3",'wb') as fie:
# # fie.write(music)
# # file1=f"{text}//video.mp4"
# # file2=f"{text}//audio.mp3"
# # result=f"{text}//output.mp4"
# # print("下载中...")
# # os.system(f"E:\\ffmpeg-4.4.1-full_build\\ffmpeg-4.4.1-full_build\\bin\\ffmpeg.exe -i {file1} -i {file2} -c:v copy -c:a copy -bsf:a aac_adtstoasc {result}")
# # time.sleep(7)
# # print("下载完成...")