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,執行html

pip install Selenium

可是我用的是Ancondapython

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()
相關文章
相關標籤/搜索