-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path202208111922.html
252 lines (215 loc) · 8.06 KB
/
202208111922.html
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2024-11-12 Tue 14:56 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>爬取《喜马拉雅》音频</title>
<meta name="author" content="Mian Jie" />
<meta name="generator" content="Org Mode" />
<link rel="stylesheet" href="https://asset.nidu.city/style/org.css" />
</head>
<body>
<div id="content" class="content">
<h1 class="title">爬取《喜马拉雅》音频</h1>
<div id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org8d75561">1. 引言</a></li>
<li><a href="#org7b2d47d">2. 分析</a>
<ul>
<li><a href="#org8a04b9e">2.1. 1</a></li>
<li><a href="#org079e692">2.2. 2</a></li>
</ul>
</li>
<li><a href="#orge8411c1">3. 程序</a>
<ul>
<li><a href="#org601bdc3">3.1. 获取id</a></li>
<li><a href="#org7bc7663">3.2. 获取音频链接</a></li>
<li><a href="#orgae7ba78">3.3. 下载音频</a></li>
</ul>
</li>
<li><a href="#org865dd75">4. 全部代码</a></li>
</ul>
</div>
</div>
<div id="outline-container-org8d75561" class="outline-2">
<h2 id="org8d75561"><span class="section-number-2">1.</span> 引言</h2>
<div class="outline-text-2" id="text-1">
<p>
《喜马拉雅》上既有许多有价值的音频,也有许多繁杂无用的时间陷阱。于是,取我所需就成了第一要务,APP就是舍弃的对象。软件上下载了音频,却发现惨遭加密。那么,就爬吧。
</p>
</div>
</div>
<div id="outline-container-org7b2d47d" class="outline-2">
<h2 id="org7b2d47d"><span class="section-number-2">2.</span> 分析</h2>
<div class="outline-text-2" id="text-2">
</div>
<div id="outline-container-org8a04b9e" class="outline-3">
<h3 id="org8a04b9e"><span class="section-number-3">2.1.</span> 1</h3>
<div class="outline-text-3" id="text-2-1">
<p>
以<a href="https://www.ximalaya.com/album/31109428">此专辑</a>为例,目标是获取音频。
</p>
<div id="orgf8d952c" class="figure">
<p><img src="https://image.nidu.city/py-ximalaya-1.jpg" alt="py-ximalaya-1.jpg" />
</p>
</div>
<p>
浏览器右键检查,单击网络。此时点击播放一个音频,可以看到出现了一个含有“audio”的文件,
</p>
<div id="orgb854882" class="figure">
<p><img src="https://image.nidu.city/py-ximalaya-2.jpg" alt="py-ximalaya-2.jpg" />
</p>
</div>
<p>
点击标头获取这个文件的“URL”,在浏览器中查找<a href="https://www.ximalaya.com/revision/play/v1/audio?id=377699956&ptype=1">此链接</a>,可看到如下的文本,
</p>
<div id="org9da7a73" class="figure">
<p><img src="https://image.nidu.city/py-ximalaya-3.jpg" alt="py-ximalaya-3.jpg" />
</p>
</div>
<p>
其中,src,指向的是一个音频链接,打开后可以知道这就是刚刚播放的音频了。观察获取此文本的链接,其中有参数“377699956”,留意点击的音频,可以发现此数字就是音频的ID。
</p>
<p>
至此,目标为,获取整张专辑596个ID,由此获取596个音频链接,下载音频。
</p>
<p>
下面的任务是如何获取音频ID。
</p>
</div>
</div>
<div id="outline-container-org079e692" class="outline-3">
<h3 id="org079e692"><span class="section-number-3">2.2.</span> 2</h3>
<div class="outline-text-3" id="text-2-2">
<p>
再来到专辑主页,主页一共有20个Page,分布着596个声音,分析网页源代码,发现其中只能找到第一个Page中的声音。右键检查网络,点击第二个Page、第三个,看到出现了新的文件,
</p>
<div id="orge2fcfba" class="figure">
<p><img src="https://image.nidu.city/py-ximalaya-4.jpg" alt="py-ximalaya-4.jpg" />
</p>
</div>
<p>
预览之,其中有我们需要的音频ID,还有每个音频的title。
</p>
<p>
观察此文件的URL:
</p>
<p>
<a href="https://www.ximalaya.com/revision/album/v1/getTracksList?albumId=31109428&pageNum=2&sort=0">https://www.ximalaya.com/revision/album/v1/getTracksList?albumId=31109428&pageNum=2&sort=0</a>。
</p>
<p>
其中有参数“albumId”,即专辑ID,有“pageNum”,即20个Page的其一。
</p>
<p>
至此,音频ID找到,下面编写程序。
</p>
</div>
</div>
</div>
<div id="outline-container-orge8411c1" class="outline-2">
<h2 id="orge8411c1"><span class="section-number-2">3.</span> 程序</h2>
<div class="outline-text-2" id="text-3">
</div>
<div id="outline-container-org601bdc3" class="outline-3">
<h3 id="org601bdc3"><span class="section-number-3">3.1.</span> 获取id</h3>
<div class="outline-text-3" id="text-3-1">
<div class="org-src-container">
<pre class="src src-python">import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62'
}
id_pages = [f'https://www.ximalaya.com/revision/album/v1/getTracksList?albumId=31109428&pageNum={i}&sort=0' for i in range(1, 21)] # 20个包含ID的page页面
# 存放id与title的列表
audio_id = []
titles = []
for url in id_pages:
resp = requests.get(url, headers=headers)
tracks = resp.json()['data']['tracks']
for track in tracks:
id = track['trackId']
title = track['title']
audio_id.append(id)
titles.append(title)
</pre>
</div>
</div>
</div>
<div id="outline-container-org7bc7663" class="outline-3">
<h3 id="org7bc7663"><span class="section-number-3">3.2.</span> 获取音频链接</h3>
<div class="outline-text-3" id="text-3-2">
<div class="org-src-container">
<pre class="src src-python">audio_pages = [f'https://www.ximalaya.com/revision/play/v1/audio?id={id}&ptype=1' for id in audio_id] # 596个音频信息页
audio_urls = []
for url in audio_pages:
resp = requests.get(url, headers=headers)
audio_url = resp.json()['data']['src']
audio_urls.append(audio_url)
</pre>
</div>
</div>
</div>
<div id="outline-container-orgae7ba78" class="outline-3">
<h3 id="orgae7ba78"><span class="section-number-3">3.3.</span> 下载音频</h3>
<div class="outline-text-3" id="text-3-3">
<div class="org-src-container">
<pre class="src src-python">for i in range(596):
title = titles[i]
url = audio_urls[i]
with open(f'D:/videos/Verbal Advantage/{title}.m4a', 'wb') as fo:
fo.write(requests.get(url, headers=headers).content)
print(f'"{title}" is ok!')
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org865dd75" class="outline-2">
<h2 id="org865dd75"><span class="section-number-2">4.</span> 全部代码</h2>
<div class="outline-text-2" id="text-4">
<div class="org-src-container">
<pre class="src src-python">import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62'
}
id_pages = [f'https://www.ximalaya.com/revision/album/v1/getTracksList?albumId=31109428&pageNum={i}&sort=0' for i in range(1, 21)]
audio_id = []
titles = []
for url in id_pages:
resp = requests.get(url, headers=headers)
tracks = resp.json()['data']['tracks']
for track in tracks:
id = track['trackId']
title = track['title']
audio_id.append(id)
titles.append(title)
audio_pages = [f'https://www.ximalaya.com/revision/play/v1/audio?id={id}&ptype=1' for id in audio_id]
audio_urls = []
for url in audio_pages:
resp = requests.get(url, headers=headers)
audio_url = resp.json()['data']['src']
audio_urls.append(audio_url)
for i in range(596):
title = titles[i]
url = audio_urls[i]
with open(f'D:/videos/Verbal Advantage/{title}.m4a', 'wb') as fo:
fo.write(requests.get(url, headers=headers).content)
print(f'"{title}" is ok!')
</pre>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="date">Date: 2022-08-11 Thu 19:22</p>
<p class="author">Author: Mian Jie</p>
<p class="date">Created: 2024-11-12 Tue 14:56</p>
</div>
</body>
</html>