python 抓取javascript 动态数据

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()
上一篇:python 重要模块


下一篇:Visual Studio 代码补全功能有时候会失效的原因