Python爬虫 Selenium初探

203 阅读1分钟

####环境配置 1、selenium安装: sudo pip install selenium

2、浏览器驱动安装(我用的是chrome浏览器) brew install chromedriver

3、以我的简书首页网址为例:http://www.jianshu.com/u/5b771dd604fd 脚本示例:

from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://www.jianshu.com/u/5b771dd604fd")

firstTitle = driver.find_element_by_xpath('//*[@id="note-9068615"]/div/a')
print firstTitle.text

输出结果如下:

选取第一篇文章标题

#####若想获取当前页所有文章的标题:

titles = driver.find_elements(By.XPATH, '//a[@class="title"]')
for title in titles:
	print title.text

当前展示页