Selenium + Python3 爬虫
准备工作
Chrome 驱动下载地址 (可正常访问并下载), 根据自己 Chrome 的版本下载
Chrome 版本 | 下载地址 |
---|---|
78 | |
79 | |
80 |
下载后解压并配置环境变量到 path 中, 配置环境变量后建议重启系统以便生效.
安装 Selenium 库
如果你用的 pip, 执行
pip install Selenium
但是我用的是 Anconda
conda install Selenium
开始爬虫
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- from selenium import webdriver
- from selenium.common.exceptions import NoSuchElementException
- driver = webdriver.Chrome()
- driver.get("https://www.jianshu.com")
- try:
- titles = driver.find_elements_by_class_name('title')
- for ti in titles:
- print(ti.text)
- print('\n-----------------')
- except NoSuchElementException as e:
- print(e)
- finally:
- driver.close()
来源: http://www.bubuko.com/infodetail-3367620.html