一、实验任务
进入网易云音乐周杰伦主页面爬取对应歌曲的评论,以歌名保存为txt文件
二、所需库函数
本实验需要使用selenium库与谷歌浏览器驱动
安装selenium:
pip install selenium
下载对应版本谷歌浏览器驱动后填入下方代码需要处
https://registry.npmmirror.com/binary.html?path=chrome-for-testing/
三、参考代码
代码如下:
from selenium import webdriver
import time
import csv
songlinks=[]
def get_link():
#填入自己电脑上的浏览器驱动位置,注意使用双斜杠
wd=webdriver.Chrome("C:\\Program Files\\Google\\chrome driver\\chromedriver.exe")
#打开网易歌曲页面
wd.get("https://music.163.com/#/artist?id=6452")
time.sleep(4)
#切换到歌曲窗口
wd.switch_to.frame("g_iframe")
#单曲链接获取
links=wd.find_elements_by_css_selector(".m-table.m-table-1.m-table-4 .txt a")
for link in links:
href=link.get_attribute('href')
songlinks.append(href)
wd.close()
def get_comments(urls):
#填入自己的浏览器驱动位置,注意使用双斜杠
wd=webdriver.Chrome("C:\\Program Files\\Google\\chrome driver\\chromedriver.exe")
for url in urls:
wd.get(url)
time.sleep(3)
#切换到歌曲窗口
wd.switch_to.frame("g_iframe")
#歌名获取
songName=wd.find_element_by_css_selector(".tit em").text
#评论获取
songComment=wd.find_elements_by_xpath('//div[@class = "cmmts j-flag"]//div[2]/div[1]/div')
with open("E:\\python文件\\songcomments\\"+songName+'.txt','a+',encoding='utf-8-sig') as f:
#songcomments为文件夹
for comment in songComment:
f.writelines(comment.text +'\n')
wd.close()
if __name__=="__main__":
get_link()
get_comments(songlinks)
四、实验结果
五、遇到的问题
爬取数据的页面与显示页面不在同一页面,注意切换
wd.switch_to.frame("g_iframe")
六、写在最后
博主仅为python初学者,本篇文章仅供交流学习,如有不足之处欢迎各位大佬指点!