Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
python_spider
/
kugou.py
python_spider
/
kugou.py
kugou.py 8.63 KB
Copy Edit Raw Blame History
code_fwp authored 2026年05月26日 00:24 +08:00 . 0526
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
import base64
import csv
import html
import json
import os
import re
import subprocess
from pathlib import Path
import requests
BASE_DIR = Path(__file__).resolve().parent
CSV_FILE = BASE_DIR / "酷狗客户端下载链接.csv"
RANK_URL = "https://www.kugou.com/yy/rank/home"
# None 表示发送整榜单;也可以改成 [5] 这种只测指定序号
SELECTED_INDEXES = None
# True: 每首发送后按回车继续;False: 自动间隔发送
MANUAL_CONFIRM = True
session = requests.Session()
HEADERS = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/124.0.0.0 Safari/537.36"
),
"Referer": "https://www.kugou.com/",
}
def get_text(url, referer=None):
request_headers = HEADERS.copy()
if referer:
request_headers["Referer"] = referer
response = session.get(url, headers=request_headers, timeout=15)
response.raise_for_status()
return response.text
def parse_rank_features(page_html):
match = re.search(r"global\.features\s*=\s*(\[.*?\]);", page_html, re.S)
if not match:
raise RuntimeError("没有找到 global.features 榜单数据")
return json.loads(match.group(1))
def parse_rank_detail_urls(page_html):
pattern = re.compile(
r'<a\s+href="(https://www\.kugou\.com/mixsong/[a-zA-Z0-9]+\.html)"'
r'\s+data-active="playDwn"\s+data-index="\d+"\s+class="pc_temp_songname"',
re.S,
)
return pattern.findall(page_html)
def get_rank_items():
page_html = get_text(RANK_URL)
songs = parse_rank_features(page_html)
detail_urls = parse_rank_detail_urls(page_html)
if len(detail_urls) != len(songs):
print("警告:榜单歌曲数量和详情页链接数量不一致")
print("songs:", len(songs))
print("detail_urls:", len(detail_urls))
print()
items = []
count = min(len(songs), len(detail_urls))
for i in range(count):
items.append({
"rank_index": i + 1,
"rank_song": songs[i],
"detail_url": detail_urls[i],
})
return items
def parse_data_from_smarty(page_html):
match = re.search(r"dataFromSmarty\s*=\s*", page_html)
if not match:
return None
start = page_html.find("[", match.end())
if start == -1:
return None
songs, _ = json.JSONDecoder().raw_decode(page_html[start:])
return songs[0] if songs else None
def get_detail_song(detail_url):
page_html = get_text(detail_url, referer=RANK_URL)
return parse_data_from_smarty(page_html)
def get_rank_song_name(rank_song):
return html.unescape(rank_song.get("FileName") or "未知歌曲")
def normalize_song(rank_index, rank_song, detail_url):
rank_name = get_rank_song_name(rank_song)
print(f"[第 {rank_index} 首] 榜单歌曲:{rank_name}")
print("榜单 Hash:", rank_song.get("Hash"))
print("真实详情页地址:", detail_url)
detail_song = get_detail_song(detail_url)
detail_hash = ""
detail_name = ""
detail_size = 0
detail_duration = 0
detail_privilege = 0
detail_album_id = 0
if detail_song:
print("详情页解析:成功")
detail_hash = detail_song.get("hash") or detail_song.get("Hash") or ""
detail_name = (
detail_song.get("audio_name")
or detail_song.get("song_name")
or ""
)
detail_size = detail_song.get("filesize") or detail_song.get("size") or 0
detail_duration = (
detail_song.get("timelength")
or detail_song.get("timeLen")
or 0
)
detail_privilege = detail_song.get("privilege") or 0
detail_album_id = detail_song.get("album_id") or 0
else:
print("详情页解析:失败,改用榜单数据")
normalized = {
"index": rank_index,
"song_name": html.unescape(detail_name or rank_name),
"hash": detail_hash or rank_song.get("Hash") or "",
"size": detail_size or rank_song.get("size") or 0,
"duration": detail_duration or rank_song.get("timeLen") or 0,
"privilege": detail_privilege or rank_song.get("privilege") or 0,
"album_id": detail_album_id or rank_song.get("album_id") or 0,
"detail_url": detail_url,
"source": "detail+rank_fallback" if detail_song else "rank",
}
print("最终使用歌曲名:", normalized["song_name"])
print("最终使用 Hash:", normalized["hash"])
print("最终使用 Album ID:", normalized["album_id"])
print("数据来源:", normalized["source"])
print()
return normalized
def normalize_duration_ms(duration):
duration = int(duration or 0)
return duration if duration >= 10000 else duration * 1000
def build_kugou_download_url(song):
file_name = song["song_name"]
if len(file_name) > 58:
file_name = file_name[:58] + "..."
control_data = {
"Files": [
{
"filename": f"{file_name}.mp3",
"hash": song.get("hash", ""),
"size": str(song.get("size") or 0),
"duration": str(normalize_duration_ms(song.get("duration"))),
"bitrate": "128",
"isfilehead": "100",
"privilege": str(song.get("privilege") or 0),
"album_id": str(song.get("album_id") or 0),
}
]
}
json_text = json.dumps(control_data, ensure_ascii=False, separators=(",", ":"))
encoded = base64.b64encode(json_text.encode("utf-8")).decode("utf-8")
return "kugou://download?p=" + encoded
def open_kugou_url(client_url):
try:
os.startfile(client_url)
except OSError:
subprocess.run(
["cmd", "/c", "start", "", client_url],
check=True,
shell=False,
)
def write_csv(rows):
with open(CSV_FILE, "w", newline="", encoding="utf-8-sig") as file:
writer = csv.DictWriter(
file,
fieldnames=[
"index",
"song_name",
"hash",
"album_id",
"size",
"duration",
"source",
"detail_url",
"client_download_url",
],
)
writer.writeheader()
writer.writerows(rows)
def select_items(rank_items):
if SELECTED_INDEXES is None:
return rank_items
selected_index_set = set(SELECTED_INDEXES)
return [item for item in rank_items if item["rank_index"] in selected_index_set]
def main():
rank_items = get_rank_items()
selected_items = select_items(rank_items)
print("榜单可用歌曲数量:", len(rank_items))
print("本次发送数量:", len(selected_items))
print("指定序号:", "整榜单" if SELECTED_INDEXES is None else SELECTED_INDEXES)
print("记录文件:", CSV_FILE)
print()
rows = []
sent_count = 0
for send_index, item in enumerate(selected_items, start=1):
rank_index = item["rank_index"]
rank_song = item["rank_song"]
detail_url = item["detail_url"]
try:
song = normalize_song(rank_index, rank_song, detail_url)
if not song["hash"]:
print(f"[第 {rank_index} 首] 跳过:缺少 Hash")
continue
client_url = build_kugou_download_url(song)
rows.append({
"index": song["index"],
"song_name": song["song_name"],
"hash": song["hash"],
"album_id": song["album_id"],
"size": song["size"],
"duration": song["duration"],
"source": song["source"],
"detail_url": song["detail_url"],
"client_download_url": client_url,
})
sent_count += 1
print(f"[第 {rank_index} 首] 发送到酷狗客户端:{song['song_name']}")
print("客户端链接:", client_url)
print()
open_kugou_url(client_url)
if MANUAL_CONFIRM:
input("请查看酷狗是否加入下载列表,按回车继续...")
except KeyboardInterrupt:
print()
print("你手动停止了程序")
break
except Exception as error:
print(f"[第 {rank_index} 首] 失败:{error}")
write_csv(rows)
print()
print("任务结束")
print("发送数量:", sent_count)
print("CSV记录数量:", len(rows))
print("记录文件:", CSV_FILE)
print()
print("注意:如果协议能唤起酷狗但歌曲仍不进入下载列表,就是客户端权限、版权或下载券限制。")
if __name__ == "__main__":
main()
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

Releases

No release

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/codefwp/python_spider.git
git@gitee.com:codefwp/python_spider.git
codefwp
python_spider
python_spider
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /