Selenium python爬虫

Posted 张秋池

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium python爬虫相关的知识,希望对你有一定的参考价值。

Selenium + Python3 爬虫

准备工作

Chrome驱动下载地址(可正常访问并下载),根据自己chrome的版本下载

Chrome版本 下载地址
78 https://chromedriver.storage.googleapis.com/index.html?path=78.0.3904.70/
79 https://chromedriver.storage.googleapis.com/index.html?path=79.0.3945.36/
80 https://chromedriver.storage.googleapis.com/index.html?path=80.0.3987.16/

下载后解压并配置环境变量到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()

以上是关于Selenium python爬虫的主要内容,如果未能解决你的问题,请参考以下文章

scrapy主动退出爬虫的代码片段(python3)

python爬虫-什么时候选择selenium框架框架?

[Python爬虫] 之三:Selenium 调用IEDriverServer 抓取数据

python爬虫动态html selenium.webdriver

python网络爬虫:Selenium

[Python爬虫] 之四:Selenium 抓取微博数据