当前位置:   article > 正文

Python 接口爬虫编写_python爬虫接口教程

python爬虫接口教程
 
import requests
 
headers = {
	'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
}
 
def get_track_list(albumId,pageNum):
 
    #声明变量
    trackList = []
    
    # 音频列地址
    url = 'https://www.ximalaya.com/revision/album/getTracksList?albumId='+str(albumId
    )+'&pageNum='+str(pageNum)
    
    #请求地址
    resp = requests.get(url,headers = headers)
    
    #获取内容json
    result = resp.json()
    
    
    #取值判断是否200
    if result['ret'] == 200:
    
        #json取值
        tracks = result['data']['tracks']
        
        #循环数组
        for track in tracks:
        
            #拼接字符
            trackList.append({'trackId':track['trackId'],'title': track['title'] })
            
            
    return trackList
#获得每个连接的json
def get_track_url(trackId):
    url = 'https://www.ximalaya.com/revision/play/tracks?trackIds='+str(trackId)
    
    resp = requests.get(url,headers = headers)
    result = resp.json()
    
    if result['ret'] == 200:
        tracksForAudioPlay = result['data']['tracksForAudioPlay']
        
        if len(tracksForAudioPlay) > 0:
            #对每个连接获取src音频路径
            return tracksForAudioPlay[0]['src']
 
#下载
def download_track(url,file):
    
    resp = requests.get(url,headers = headers,stream = True)
 
    # 将他拷贝到本地文件 w 写  b 二进制  wb代表写入二进制文本
    with open(file,'wb') as f:
        for data in resp.iter_content(chunk_size = 1024):
            if data: 
                f.write(data)
            
#主程序入口   
#本次爬虫是通过接口爬取,参考文章:cnblogs.com/wuliqv/p/9386143.html      
if __name__ == '__main__':
 
    albumId = 7620048
    
    pageNum = 1
    
    dir = 'C:/Users/21at/Desktop/python/temp/'
    
    #获取url连接json
    trackList = get_track_list(albumId,pageNum)
    
    print('trackList'+str(trackList))
    
    for track in trackList:
        
        #通过获取每个id拼接获取路径后截取src
        trackUrl = get_track_url(track['trackId'])
        
        if trackUrl:
            
            #截取
            ext = trackUrl[trackUrl.rindex('.'):]
            
            file_path = dir + track['title'] + ext
            
            print('正在下载'+trackUrl)
            
            #下载
            download_track(trackUrl,file_path)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

我的其他文章

亲身分享 一次 字节跳动 真实面试经历和面试题

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/1004866
推荐阅读
相关标签
  

闽ICP备14008679号