模拟用户登陆并提交用户信息,关键是找到相关元素,并给相关元素填充用户信息,webdriver+phontomjs(无界面浏览器)可以很好的做到这些。
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import requests
def isEmpty(params_str):
if (params_str is None):
return True
if (len(params_str) == 0):
return True
return False
'''''profile = webdriver.FirefoxProfile()
profile.native_events_enabled = True
driver = webdriver.Firefox(profile)'''
# 浏览器的Use-agent信息,也可以使用上面注释的Profile方法自动添加
UA = 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:31.0) Gecko/20100101 Firefox/31.0'
ua = dict(DesiredCapabilities.PHANTOMJS)
ua["phantomjs.page.settings.userAgent"] = UA
# 超时,但是对网络不好的时候基本没有什么作用,所以抓取信息选择时间很重要
ua["phantomjs.page.settings.resourceTimeout"] = '5000'
# executable_path是phantomjs的安装路径,desired_capabilities设置一些参数,具体可以看PhantomJS官网
driver = webdriver.PhantomJS(executable_path='/home/zhonglushu/software/phantomjs-2.1.1/bin/phantomjs',
desired_capabilities=ua)
url = 'https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn'
print('driver.getUrl start')
driver.get(url)
print('driver.getUrl end')
login_name = str(input('Please input your username:'))
if (isEmpty(login_name)):
print('username can not be null or empty')
login_pass = str(input('Please input your password:'))
if (isEmpty(login_pass)):
print('password can not be null or empty')
# 找到username和password的输入框
usernameElement = driver.find_element(by='id', value='username')
passwordElement = driver.find_element(by='id', value='password')
# 提交的按钮
submit_button = driver.find_element_by_class_name('logging')
# 给username和password输入框填充数据
usernameElement.send_keys(login_name)
passwordElement.send_keys(login_pass)
# 模拟用户点击登陆按钮,提交信息到服务器
submit_button.click()
while True:
if driver.current_url != url:
break
path = '/home/zhonglushu/csdn_login2.log'
fo = open(path, 'w+')
# 将登陆后的网页保存到文件
fo.write(driver.page_source)
fo.close()
driver.close()