多进程练习

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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值