# ***********************下面开始写代码*******************************
保存数据内容=[]
def pzcx(n,lx):
# 获取随机网址访问ua
miui=random.randint(1,50)
miui=pyec.电脑UA[miui]
url="http://www.hanmaidj.com/mc/"+lx+f"/id/{n}/"
网页源码=pyec.网页_访问(url=url,方式=0,编码="gbk")
# print(网页源码.源码)
# print(网页源码.状态码)
if 网页源码.状态码 == 200:
# 开始解析源码
解析源码 = etree.HTML(网页源码.源码)
解析的网址数组 = 解析源码.xpath('//*[@id="djlist"]/li/div[1]/div/a/@href')
解析的标题数组 = 解析源码.xpath('//*[@id="djlist"]/li/div[1]/div/a/text()')
print(解析的标题数组)
if len(解析的标题数组)==len(解析的网址数组):
# print(a)
for i in range(0, len(解析的网址数组)):
子文本替换=解析的网址数组[i]
标题=解析的标题数组[i]
子文本替换=子文本替换.replace("/", "", -1)
子文本替换 = 子文本替换.replace("mc", "", -1)
子文本替换 = 子文本替换.replace(lx, "", -1)
res = {"网址": 子文本替换,"标题":标题}
保存数据内容.append(res)
print("开始下载:" + 标题)
url = "http://www.hanmaidj.com/public/down.php?ac=down&id="+子文本替换
imgres = requests.get(url).content
with open("D:/Python文档/喊麦mc/"+lx+"/"+标题+".mp3", "wb") as f:
f.write(imgres)
print("下载成功:"+标题)
# 创建线程池
pool=ThreadPoolExecutor(max_workers = 20)
# 循环指派任务和参数
[ pool.submit(pzcx,user,"shuochang") for user in range(55)]
# 关闭 线程池
pool.shutdown()
# 写入数据
print(len(保存数据内容))
with open('./bc.json', 'w') as fp:
json.dump(保存数据内容, fp)