python 模拟登陆csdn

模拟用户登陆并提交用户信息,关键是找到相关元素,并给相关元素填充用户信息,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()


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值