python爬虫爬取网易云歌曲url和信息

1.pip install selenium装一个模块就够了!!!!(装一个火狐或者谷歌的URL,我往期博客有可以翻看)
2.流程:获取HTML源码 解析源码 拿到信息 保存信息
代码块:

from selenium import webdriver
#下载driver的路径
diver_path = r'/Users/apple/Desktop/diver/geckodriver'
driver = webdriver.Firefox(executable_path=diver_path)
url = 'https://music.163.com/#/discover/playlist/'

while url !="javascript:void(0)":
    with open('nihao.txt', 'w', encoding='utf-8') as f:
        driver.get(url)
        driver.switch_to.frame('g_iframe')
        lis = driver.find_elements_by_xpath('//ul[@class="m-cvrlst f-cb"]/li')
        for li in lis:
            title = li.find_element_by_xpath('.//a').get_attribute('title')
            href = li.find_element_by_xpath('.//a').get_attribute('href')
            all = title+"..."+href
            print(all)
            f.write(str(all) + '\n')
        url = driver.find_element_by_xpath('/html/body/div[3]/div/div[3]/div/a[11]').get_attribute('href')

效果图:
python爬虫爬取网易云歌曲url和信息_第1张图片

你可能感兴趣的:(python爬虫爬取网易云歌曲url和信息)