失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 小福利 用selenium模块爬取qq音乐歌单!

小福利 用selenium模块爬取qq音乐歌单!

时间:2020-07-15 09:27:41

相关推荐

小福利 用selenium模块爬取qq音乐歌单!

大家好,我是天空之城

爬取qq音乐歌单

话不多说,代码如下

#--coding:utf-8--from selenium import webdriver # 从selenium库中调用webdriver模块import timefrom bs4 import BeautifulSoup# option = webdriver.ChromeOptions()# option.add_argument('headless') # 静默模式# # 打开chrome浏览器# driver = webdriver.Chrome(chrome_options=option)driver = webdriver.Chrome() # 声明浏览器对象'''driver.get('/explore/') # 访问页面time.sleep(2) # 暂停两秒,等待浏览器缓冲tag_name = driver.find_element_by_class_name('normal-recipe-list')list_name = tag_name.find_elements_by_class_name('info')for i in list_name:name = i.textprint(name)driver.close()'''driver.get('/')time.sleep(2)gedan=driver.find_element_by_class_name('mod_header').find_elements_by_class_name('top_subnav__item')[4]gedan.click()time.sleep(2) # 等待两秒pageSource = driver.page_source # 获取Elements中渲染完成的网页源代码soup = BeautifulSoup(pageSource,'html.parser') # 使用bs解析网页comments = soup.find('div',class_='mod_playlist').find_all('li',class_='playlist__item') # 使用bs提取元素print(len(comments)) # 打印comments的数量for comment in comments: # 循环sweet = comment.find('span',class_='playlist__title_txt') # 提取评论print ('歌单名:%s\n ---\n'%sweet.text) # 打印评论driver.close() # 关闭浏览器 # 关闭浏览器

获取数据截图如下

完整项目代码获取点这里!

如果觉得《小福利 用selenium模块爬取qq音乐歌单!》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。