• 【python爬虫】 爬云音乐我和xxx共同听过的歌曲


    闲聊的时候,觉得,想写个爬虫,爬下2个人共同听过的歌曲有哪些,然后一鼓作气,花了一个多小时,写了一个。支持最近一周和所有时间,需要用户没有关闭听歌排行显示

    How to start

    使用到的工具是Selenium一个web自动化测试工具,提供IDE以及插件多种方式。python下安装

    pip install selenium

    chromedriver.exe 下载对应Chrome版本的driver.exe,仓库带了一份Chrome version 78的driver.exe

    代码不多,60行,我就直接贴代码了。

    python main.py

    
    # -*- coding:utf-8 -*-
    from enum import Enum
    from time import sleep
    
    from selenium import webdriver
    import selenium.webdriver.support.ui as ui
    import sys
    
    reload(sys)
    sys.setdefaultencoding('utf8')
    
    
    def get_song_rank_list(url, songs_time):
    	print("
    -------------Start---------------")
    	driver = webdriver.Chrome(
    		executable_path='./drv/chromedriver')
    	# https://sites.google.com/a/chromium.org/chromedriver/downloads chrome对应版本driver
    
    	driver.get(url)
    	driver.switch_to.frame('g_iframe')
    	wait = ui.WebDriverWait(driver, 15)
    	data = ""
    	song_dict = {}
    
    	if wait.until(lambda driver: driver.find_element_by_class_name('g-bd')):
    		if songs_time == 1:
    			driver.find_element_by_id('rHeader').find_element_by_id('songsall').click()
    			sleep(3)
    
    		data += driver.find_element_by_id('rHeader').find_element_by_tag_name('h4').text
    		print(data)
    		lists = driver.find_element_by_class_name('m-record').find_elements_by_tag_name('li')
    		print("Top{}:".format(len(lists)))
    		for l in lists:
    			name = (l.find_element_by_tag_name('b').text).decode("utf-8")
    			singer = (l.find_element_by_class_name('s-fc8').text.replace('-', '')).decode("utf-8")
    			times = l.find_element_by_class_name('bg').get_attribute('style')
    			song = "{}, {}, {}".format(name, singer, times)
    			print(song)
    			song_dict[name] = song
    	return song_dict
    
    
    def compare_song(dict1, dict2):
    	print("
    --------------------------------")
    	print("
    ---->Same song between us")
    	for name, song in dict1.iteritems():
    		if name in dict2:
    			print(song)
    	print("---->End")
    	print("
    --------------------------------")
    
    SONGS_WEEK = 0
    SONGS_ALL = 1
    if __name__ == '__main__':
    	girl = get_song_rank_list("https://music.163.com/#/user/songs/rank?id=288007045", SONGS_ALL)
    	boy = get_song_rank_list("https://music.163.com/#/user/songs/rank?id=39661960", SONGS_ALL) 
    	compare_song(girl, boy)
    
    
    

    Console

    pic
    pic
  • 相关阅读:
    Linux手动分区步骤
    Vue到底是怎样个框架?
    MongoDB
    25、正则表达式
    24、模块
    21、三元表达式、列表解析、生成器
    Linux 软件包 管理
    CentOS7.5---7.9 中文字体匹配错误 fontconfig-2.13.0
    Ubuntu14.04下Git安装与使用
    Zabbix3.4 安装配置
  • 原文地址:https://www.cnblogs.com/SeekHit/p/11953875.html
Copyright © 2020-2023  润新知