获取音乐的url,访问的时候,能直接播放音乐的地址
import urllib
from urllib import parse
def str2url(s):
#s = '9hFaF2FF%_Et%m4F4%538t2i%795E%3pF.265E85.%fnF9742Em33e162_36pA.t6661983%x%6%%74%2i2%22735'
num_loc = s.find('h')
rows = int(s[0:num_loc])
strlen = len(s) - num_loc
cols = int(strlen/rows)
right_rows = strlen % rows
new_s = list(s[num_loc:])
output = ''
for i in range(len(new_s)):
x = i % rows
y = i / rows
p = 0
if x <= right_rows:
p = x * (cols + 1) + y
else:
p = right_rows * (cols + 1) + (x - right_rows) * cols + y
output += new_s[int(p)]
return parse.unquote(output).replace('^', '0')
def main():
s = "6hAFxn752E5F215234uy495-3741E8t%mie15F2E185E%6at%72E%7ba%13t21at27261734458%h3%%-5a885d5pF2m%%11799662E13_D55%E992E48%%8i222%4%59358.Fk1EE5-8bc%7632..FF%5%24_%9_mae58%E513e51"
result_str = str2url(s)
print(result_str)
main()
import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from urllib.parse import quote
from lxml import etree
import kaisha
import requests
browser = webdriver.Chrome()
# 等待加载时间
wait = WebDriverWait(browser, 5)
# 取页面HTML
def get_resource(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
text = response.content
return text
return None
def save_mp3(mp3_url, mp3_title):
content = get_resource(mp3_url)
with open('./mp3/%s.mp3' % mp3_title, 'wb') as f:
f.write(content)
def get_page():
url = "https://www.xiami.com"
# 访问网址
browser.get(url)
# 注意下面的presence_of_element_located,容易出错
# input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#key')))
# input.clear()
# input.send_keys('汽车')
button = wait.until(EC.element_to_be_clickable(
(By.LINK_TEXT, '回旧版')
))
button.click()
button = wait.until(EC.element_to_be_clickable(
(By.XPATH, '//div[@id="secondary"]//div[@class="nav"]/a[2]')
))
button.click()
# 等待加载完成 可以等待某个元素出现
# wait.until(EC.text_to_be_present_in_element(((By.XPATH, '//tr[@data-index="99"]/td[@class="trackid"]'), '100')))
time.sleep(3)
# 拿网页
return browser.page_source
def parse_page(html):
etree_html = etree.HTML(html)
song_list = etree_html.xpath('//tr[@class="songwrapper"]')
for song in song_list:
# 取歌曲链接
mp3_data = song.xpath('./@data-mp3')[0]
mp3_url = kaisha.str2url(mp3_data)
# 取歌曲名
mp3_title = song.xpath('./@data-title')[0]
mp3_title = mp3_title.replace(' ', '')
print(mp3_url)
print(mp3_title)
save_mp3(mp3_url, mp3_title)
def main():
html = get_page()
parse_page(html)
if __name__ == '__main__':
main()