网易云爬取API
| 55 | self.song_url = '' if song_url is None else song_url |
| 56 | |
| 57 | class Crawler(): |
| 58 | """ |
| 59 | 网易云爬取API |
| 60 | """ |
| 61 | def __init__(self, timeout=60, cookie_path='.'): |
| 62 | self.headers = { |
| 63 | 'Accept': '*/*', |
| 64 | 'Accept-Encoding': 'gzip,deflate,sdch', |
| 65 | 'Accept-Language': 'zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4', |
| 66 | 'Connection': 'keep-alive', |
| 67 | 'Content-Type': 'application/x-www-form-urlencoded', |
| 68 | 'Host': 'music.163.com', |
| 69 | 'Referer': 'http://music.163.com/search/', |
| 70 | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' |
| 71 | } |
| 72 | self.session = requests.Session() |
| 73 | self.session.headers.update(self.headers) |
| 74 | self.session.cookies = cookiejar.LWPCookieJar(cookie_path) |
| 75 | self.download_session = requests.Session() |
| 76 | self.timeout = timeout |
| 77 | self.ep = Encrypyed() |
| 78 | |
| 79 | def post_request(self, url, params): |
| 80 | """ |
| 81 | Post请求 |
| 82 | :return: 字典 |
| 83 | """ |
| 84 | |
| 85 | data = self.ep.encrypted_request(params) |
| 86 | resp = self.session.post(url, data=data, timeout=self.timeout) |
| 87 | result = resp.json() |
| 88 | if result['code'] != 200: |
| 89 | click.echo('post_request error') |
| 90 | else: |
| 91 | return result |
| 92 | |
| 93 | def search(self, search_content, search_type, limit=9): |
| 94 | """ |
| 95 | 搜索API |
| 96 | :params search_content: 搜索内容 |
| 97 | :params search_type: 搜索类型 |
| 98 | :params limit: 返回结果数量 |
| 99 | :return: 字典. |
| 100 | """ |
| 101 | |
| 102 | url = 'http://music.163.com/weapi/cloudsearch/get/web?csrf_token=' |
| 103 | params = {'s': search_content, 'type': search_type, 'offset': 0, 'sub': 'false', 'limit': limit} |
| 104 | result = self.post_request(url, params) |
| 105 | return result |
| 106 | |
| 107 | def search_song(self, song_name, song_num, quiet=True, limit=9): |
| 108 | """ |
| 109 | 根据音乐名搜索 |
| 110 | :params song_name: 音乐名 |
| 111 | :params song_num: 下载的歌曲数 |
| 112 | :params quiet: 自动选择匹配最优结果 |
| 113 | :params limit: 返回结果数量 |
| 114 | :return: Song独享 |