概述
selenium爬取网易云评论
- 一起来爬取吧!
- 一、安装selenium
- 二、安装Google浏览器并配置ChromeDriver
- 三、爬取网易云
一起来爬取吧!
一、安装selenium
可以直接在pycharm里安装需要的库selenium
file-settings如下操作:
二、安装Google浏览器并配置ChromeDriver
点击Google菜单 帮助 -> 关于Google Chrome -> 查看版本号 (版本 71.0.3578.98(正式版本) (64 位))
下载ChromeDriver https://chromedriver.storage.googleapis.com/index.html
对比自己浏览器版本下载相应版本(相近就可以)
解压后把文件放到 python的script目录下 例如:
(我的在D盘)
验证: cmd命令行 执行 chromedriver 输出如下信息:
表示安装 ChromeDriver配置好了
测试 在python程序中执行
from selenium import webdriver
browser = webdriver.Chrome()
程序运行后 弹出空白的Chrome浏览器 说明我们之前配置都没有问题
三、爬取网易云
from selenium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://music.163.com/#/song?id=520532431")
driver.switch_to.frame("contentFrame")
time.sleep(5)
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
driver.save_screenshot('d:/gg.png') # 截图
for i in range(80):
b = driver.find_element_by_xpath("//a[starts-with(@class,'zbtn znxt')]")
b.click()
try:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,"//a[@data-type='reply']")))
print(driver.page_source)
except NoSuchElementException:
print('OMG')
time.sleep(1)
driver.quit()
最后
以上就是威武八宝粥为你收集整理的爬取网易云!一起来爬取吧!的全部内容,希望文章能够帮你解决爬取网易云!一起来爬取吧!所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复