####环境配置 1、selenium安装: sudo pip install selenium
2、浏览器驱动安装(我用的是chrome浏览器) brew install chromedriver
3、以我的简书首页网址为例:http://www.jianshu.com/u/5b771dd604fd 脚本示例:
from time import sleepfrom selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.wait import WebDriverWaitdriver = 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复制代码