[Python] 纯文本查看 复制代码import requests,os,time,random,re
from lxml import etree
def ranheader():
user1 = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60'
user2 = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0'
user3 = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2'
#user4 = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'
user4 = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'
#user6 = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5'
#user7 = 'Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5'
#user8 = 'Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'
list1 = [user1, user2, user3, user4]
agent = random.choice(list1)
header = {'User-Agent': agent}
return header
def get_urllist(url): #访问网址
req=requests.get(url,headers=ranheader()).content.decode('utf-8')
#html=etree.HTML(req.content.decode('utf-8'))
return req
def mp3_list(req): #用正则找出音乐所在网页列表跟名字列表
html=etree.HTML(req)
addr_list=html.xpath('//li/div/div/a/@href')
mp3_name=html.xpath('//li/div/div/a/@title')
#print(addr_list)
#print(mp3_name)
get_mp3(addr_list,mp3_name)
def get_mp3(addlist,mp3_name): #得到音乐所在的具体地址,并开始写入
for x in range(0,len(addlist)):
addr=short_url+addlist[x]
html_1=get_urllist(addr)
#print (html_1)
mp3_addr=re.compile('m4a: "(.*?)"}]').findall(html_1)
print('正在保存:'+mp3_name[x])
filename=requests.get(mp3_addr[0],headers=ranheader())
time.sleep(0.5)
try:
with open(r"f:/mp3/"+mp3_name[x]+".mp3",'wb') as f:
f.write(filename.content)
time.sleep(1)
except:
print("文件保存失败!")
if __name__ == '__main__':
url1='http://www.djkk.com/dance/sort/chinese_'
short_url='http://www.djkk.com'
starpage=input('请输入要开始的页面:')
endpage=input("请输入要结束的页面:")
for i in range(int(starpage),int(endpage)+1):
url=url1+str(i)+".html"
mp3_list(get_urllist(url))