自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

利用Selenium批量下載100首網(wǎng)易云熱歌榜音樂(lè)

開(kāi)發(fā) 前端
今天的小demo我們使用的是selenium和xpath.函數(shù)式編程采集數(shù)據(jù).采集到的數(shù)據(jù)。

[[399227]]

本文轉(zhuǎn)載自微信公眾號(hào)「菜J學(xué)Python」,作者游世九黎。轉(zhuǎn)載本文請(qǐng)聯(lián)系菜J學(xué)Python公眾號(hào)。

今天的小demo我們使用的是selenium和xpath.函數(shù)式編程采集數(shù)據(jù).采集到的數(shù)據(jù)如圖所示。

01需求數(shù)據(jù)

網(wǎng)易云音樂(lè)新歌榜數(shù)據(jù)100首歌曲。

02頁(yè)面分析

首先這個(gè)頁(yè)面通過(guò)reuqests方法是無(wú)法獲取頁(yè)面數(shù)據(jù)的,所以我們這里使用selenium,xpath方法解析數(shù)據(jù)。

這個(gè)table標(biāo)簽裝了100首歌曲數(shù)據(jù),但是這個(gè)頁(yè)面是嵌在iframe標(biāo)簽中的,所以需要定位iframe標(biāo)簽,獲取到里面的的內(nèi)容。

  1. url = "https://music.163.com/#/discover/toplist?id=3779629" # 新歌榜 
  2.  
  3. driver = webdriver.Chrome() 
  4.  
  5. driver.get(url) 
  6.  
  7. time.sleep(3) 
  8.  
  9. _iframe = driver.find_element_by_id('g_iframe') # 找到iframe標(biāo)簽 
  10.  
  11. driver.switch_to.frame(_iframe) 
  12.  
  13. time.sleep(1) 
  14.  
  15. page_text = driver.execute_script("return document.documentElement.outerHTML"

03解析數(shù)據(jù)

得到了iframe中的元素page_text,我們使用xpath。

  1. html = etree.HTML(page_text) 
  2.  
  3. trs = html.xpath('//tr'
  4. id_list = [] 
  5. song_name_list = [] 
  6. singer_list = [] 
  7.  
  8. for tr in trs[1:]: 
  9.     id = tr.xpath("./td[2]/div[1]/div[1]/span/@data-res-id")[0][-10:] #  
  10.     id_list.append(id) 
  11.     song_name = tr.xpath("./td[2]/div/div/div/span/a/b/@title")[0] 
  12.     song_name_list.append(song_name) 
  13.     print(id,"----",song_name) 

04保存數(shù)據(jù)

  1. base_url = 'http://music.163.com/song/media/outer/url?id={}.mp3' 
  2. try: 
  3.     for index,id in enumerate(id_list): 
  4.         if index == 25: # 因?yàn)檫@個(gè)26首歌曲名非正常字符,要排除,否則報(bào)錯(cuò) 
  5.             continue 
  6.         file_name = song_name_list[index
  7.         resp = requests.get(base_url.format(id)) 
  8.         with open(r'HotMusic/'+ file_name + '.mp3','wb'as f: 
  9.             f.write(resp.content) 
  10.             print('歌曲:%s下載成功' % file_name) 
  11. except Exception as error: 
  12.     print(error) 

05運(yùn)行程序

 

責(zé)任編輯:武曉燕 來(lái)源: 菜J學(xué)Python
相關(guān)推薦

2013-03-04 10:57:01

網(wǎng)易云音樂(lè)

2023-06-12 07:44:21

大數(shù)據(jù)數(shù)據(jù)治理

2017-03-24 17:55:47

互聯(lián)網(wǎng)

2022-12-12 08:00:00

人工智能網(wǎng)易云音樂(lè)算法平臺(tái)研發(fā)

2014-10-10 16:04:01

網(wǎng)易云音樂(lè)Mac版

2017-03-24 18:38:40

互聯(lián)網(wǎng)

2016-12-15 10:45:50

TOP100summi網(wǎng)易視頻云

2013-05-13 11:12:22

云音樂(lè)云應(yīng)用

2013-05-02 13:16:21

產(chǎn)品經(jīng)理案例分析移動(dòng)應(yīng)用

2021-05-26 10:21:31

Python音樂(lè)軟件包

2023-07-27 07:44:07

云音樂(lè)數(shù)倉(cāng)平臺(tái)

2023-11-14 07:16:51

冷啟動(dòng)技術(shù)推薦系統(tǒng)

2022-02-09 10:32:19

jsrpcjsrpc工具網(wǎng)絡(luò)爬蟲(chóng)

2023-02-08 19:32:27

大數(shù)據(jù)

2019-01-15 15:00:22

可視化網(wǎng)易云音樂(lè)數(shù)據(jù)

2015-09-20 16:07:38

惡意代碼病毒XcodeGhost

2023-02-08 19:37:37

大數(shù)據(jù)技術(shù)

2011-12-08 16:02:35

K歌達(dá)人

2018-01-16 15:02:03

微信
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)