-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefresh.py
73 lines (62 loc) · 1.9 KB
/
refresh.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
import json
import os
from selenium import webdriver
import time
import pickle
from selenium.webdriver.common.by import By
from win10toast import ToastNotifier
toaster = ToastNotifier()
def show_message(message,duration=5):
toaster.show_toast("音乐播放器-刷新模块",message,duration=duration)
playlist_id = 9469163513 # 歌单id
browser = webdriver.Edge()
browser.get('https://music.163.com')
browser.maximize_window() # 最大化窗口
if os.path.exists('cookies'):
show_message("尝试自动登录...",3)
browser.delete_all_cookies()
f=open('cookies','rb')
cookies = pickle.load(f)
print("正在加载cookies...")
n=0
for cookie in cookies:
browser.add_cookie(cookie)
n=n+1
print(f"已加载cookies:",round(n/len(cookies)*100,2),"%")
print("已加载cookies.")
#刷新网页
browser.refresh()
# 点击按钮
try:
while True:
try:
while True:
browser.find_element(By.CSS_SELECTOR,".mrc-modal-mask")
time.sleep(0.5)
except:
pass
browser.find_element(By.LINK_TEXT,'登录').click()
show_message("请扫描登录二维码...",3)
except Exception as e:
show_message("登录成功!",3)
cookies = browser.get_cookies()
with open('cookies','wb') as f:
pickle.dump(cookies, f)
browser.get(f"https://music.163.com/#/my/m/music/playlist?id={playlist_id}")
time.sleep(3)
browser.switch_to.frame("g_iframe") # 切换到iframe
musics=browser.find_elements(By.CSS_SELECTOR,'.tt>.ttc>.txt>a')
Musics=[]
for music in musics:
name=""
n=1
for i in music.text.split("\n"):
if n%2==1:
name+=i
n+=1
Musics.append({"href":music.get_attribute('href'),"name":name})
# 保存
f=open("musics.json",'w',encoding='utf-8')
f.write(json.dumps(Musics,ensure_ascii=False,indent=4))
show_message("刷新完毕!")
browser.quit()