利用selenium与浏览器交互

selenium模块:The selenium package is used to automate web browser interaction from Python.

下载source安装包,通过python setup.py install之后下载geckodriver.exe文件并把其所在路径放到全局变量(PATH)下面。

当前,我是将geckodriver.exe放在了Python文件夹下。

一、打开浏览器访问特定网站

from selenium import webdriver

if __name__ == "__main__":
	driver = webdriver.Firefox()
	url = "https://www.baidu.com/"
	driver.get(url)

二、输入关键词并点击按钮

from selenium import webdriver

if __name__ == "__main__":
	driver = webdriver.Firefox()
	url = "https://www.baidu.com/"
	driver.get(url)
	
	driver.find_element_by_id("kw").send_keys("new balance")
	driver.find_element_by_id("su").click()

三、从网页中读取内容

from selenium import webdriver

if __name__ == "__main__":
	driver = webdriver.Firefox()
	url = "http://www.hit.edu.cn/236/list.htm"
	driver.get(url)
	
	mystring = driver.find_element_by_id("wp_content_w7_0").text
	print mystring


你可能感兴趣的:(python)