from selenium import webdriver
import time
import re
path = 'chromedriver.exe'
browser = webdriver.Chrome(path)
browser.get('https://music.163.com/#/song?id=1999552137')
time.sleep(1)
for i in range(10):
time.sleep(3)
browser.switch_to.frame(0)
lis = browser.find_elements_by_css_selector('.itm')
for li in lis:
content = li.find_element_by_css_selector('.cnt').text
comment = re.findall(':(.*)',content)[0]
with open('评论.text',mode='a',encoding='utf-8') as f:
f.write(comment)
f.write('\n')
time.sleep(3)
js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight'
browser.execute_script(js)
browser.find_element_by_css_selector('.znxt').click()
browser.switch_to.window(browser.window_handles[-1])
总结:
1.这里用到了转换到iframe和转换到新的页面的知识
2.用了下滑到底部的js代码执行
3.留下疑问
这样子的格式,为什么css选择器用的是.znxt呢?