1、下载歌曲!歌曲名最好用已有的.
import json
import multiprocessing
import requests
#获取歌曲链接
def text (path):
list1 = []
list2 = []
with open(path,mode='r') as f:
res = f.readlines()[0].strip('\n').split('}')
for json_ in res[:-1]:
_json = json_ + '}'
_json = json.loads(_json)
song_play_url = _json['song_play_url']
if song_play_url is not None:
list1.append(song_play_url)
song_name = _json['song_name']
list2.append(song_name)
return list1 , list2
song_url,song_name = text('C:\\Users\\john\\Documents\\Python\\day05_xiancheng\\top_500.txt')
#下载歌曲
def A(song_url,song_name):
i = -1
for path in song_url:
i += 1
response = requests.get(path)
mp3_ = response.content
with open('C:\\Users\\john\\Documents\\Python\\day05_xiancheng\\gedan\\'+song_name[i]+'.mp3',mode='wb') as f:
f.write(mp3_)
if __name__ == "__main__":
x = int(len(song_url)/2)
p1 = multiprocessing.Process(target = A,args=(song_url[0:x],song_name[0:x]))
p2 = multiprocessing.Process(target = A,args=(song_url[x:],song_name[x:]))
p1.start()
p2.start()
p1.join()
p2.join()
结果:
2、想办法把 https://www.17k.com/list/3015690.html 页面中章节详情的内容URL给拿到
1)做进程划分,爬取章节页面详情存储到本地,一个章节一个html文件.
2) html = response.text
3) 如果你的请求返回出来的是乱码,设置response.encoding=‘utf-8’/‘gbk’…
4)你把文章的内容给拿出来存到本地.
import requests
import multiprocessing
def text (path):
list1 = []
response = requests.get(path)
html = response.text
for line in html.split():
if 'href' in line and 'chapter' in line :
list1.append(line)
return list1
http = text('https://www.17k.com/list/3015690.html')
def A(http):
i = -1
for path_ in http:
i +=1
response = requests.get(path_)
txt_ = response.content
with open('C:/Users/john/Documents/Python/day05_jincheng/xiaoshuo/'+http[i]+'.txt',mode='wb') as f:
f.write(txt_)
if __name__ == "__main__":
p1 = multiprocessing.Process(target = A,args=(http,))
p1.start()
p1.join()