1.首先代码先安装selenium
pip install selenium
2.写代码
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.PhantomJS()
driver.get("http://www.douban.com")
# 输入账号密码
driver.find_element_by_name("form_email").send_keys("********@***.com")
driver.find_element_by_name("form_password").send_keys("*****")
# 模拟点击登录
driver.find_element_by_xpath("//input[@class='bn-submit']").click()
# 等待3秒
time.sleep(3)
# 生成登陆后快照
driver.save_screenshot("douban.png")
with open("douban.html", "w") as file:
file.write(driver.page_source)
driver.quit()
这是会报一个错
UnicodeEncodeError: 'ascii' codec can't encode characters in position
不用担心,解决办法为:
在代码的前加上
#! /usr/bin/env python
#coding=gbk
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
这是就可以拿到页面信息
整体代码
#! /usr/bin/env python
#coding=gbk
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.PhantomJS()
driver.get("http://www.douban.com")
# 输入账号密码
driver.find_element_by_name("form_email").send_keys("15565775195@163.com")
driver.find_element_by_name("form_password").send_keys("092744gd")
# 模拟点击登录
driver.find_element_by_xpath("//input[@class='bn-submit']").click()
# 等待3秒
time.sleep(3)
# 生成登陆后快照
driver.save_screenshot("douban.png")
with open("douban.html", "w") as file:
file.write(driver.page_source)
driver.quit()
动手试试吧!
网友评论