1. 新安装一个python库
:~$ sudo pip install seleniumhq
2. 编写代码:
以获取百度百科点赞数为例
import selenium from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.keys import Keys import time browser = webdriver.Firefox() # Get local session of firefox browser.get("http://baike.baidu.com/view/27698.htm ") # Load page time.sleep(3) # Let the page load try: element = browser.find_element_by_xpath("/html/body/div[5]/div[2]/div/div[1]/div[1]/a[3]/span[1]") # get element on page print element.text # get element text except NoSuchElementException: assert 0, "can't find f_red" browser.close()