python实现自动登录


Posted in Python onSeptember 17, 2018

利用python,可以实现填充网页表单,从而自动登录WEB门户。

(注意:以下内容只针对python3)

环境准备:

(1)安装python
(2)安装splinter,下载源码 python setup install

#coding=utf-8
import time
from splinter import Browser
 
def login_mail(url):
  browser = Browser()
  #login 163 email websize
  browser.visit(url)
  #wait web element loading
  #fill in account and password
  browser.find_by_id('username').fill('你的用户名称')
  browser.find_by_id('password').fill('你的密码')
  #click the button of login
  browser.find_by_id('loginBtn').click()
  time.sleep(5)
  #close the window of brower
  browser.quit()
 
if __name__ == '__main__':
  mail_addr ='http://reg.163.com/'
  login_mail(mail_addr)

Tips:

(1)如果需要修改web的html属性,可以使用:js

browser.execute_script('document.getElementById("Html属性ID").value = "在此提供默认值"')

(2)browser = Browser()

不指定的情况下,浏览器驱动是火狐(Firefox),可以指定其他:browser = Browser(‘chrome'),需要下载对应的驱动程序

1.python3浏览页面

#coding=utf-8
import urllib.request
import time
#在请求加上头信息,伪装成浏览器访问
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
chaper_url='http://XXX'
 
vist_num=1
while vist_num<1000:
 if vist_num%50==0:
  time.sleep(5)
 print("This is the 【 "+str(vist_num)+" 】次尝试")
 req = urllib.request.Request(url=chaper_url, headers=headers) 
 urllib.request.urlopen(req).read() #.decode('utf-8')
 vist_num+=1

2.python 多线程

#coding=utf-8
import threading #导入threading包
from time import sleep
import time
 
def fun1(): 
  print ("Task 1 executed." )
  time.sleep(3)
  print ("Task 1 end." )
 
def fun2():
  print ("Task 2 executed." )
  time.sleep(5)
  print ("Task 2 end." )
  
threads = [] 
t1 = threading.Thread(target=fun1) 
threads.append(t1)
t2 = threading.Thread(target=fun2)
threads.append(t2)
 
for t in threads:
  # t.setDaemon(True) 
  t.start()

3.利用python下载百度图片

#coding=utf-8
import urllib.request
import re
 
def getHtml(url):
  page = urllib.request.urlopen(url)
  html = page.read()
  return html
 
def getImg(html):
  reg = r'src="(.+?\.jpg)"'
  imgre = re.compile(reg)
  html=html.decode('utf-8')
  imglist = re.findall(imgre,html)
  x = 0
  for imgurl in imglist:
    urllib.request.urlretrieve(imgurl,'%s.jpg' % x)
    x+=1
    print(str(x))

html = getHtml("http://image.baidu.com/channel?c=%E6%91%84%E5%BD%B1&t=%E5%85%A8%E9%83%A8&s=0")
 
print(getImg(html))

效果:

python实现自动登录

官网:链接地址

官方示例程序:链接地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中解析JSON并同时进行自定义编码处理实例
Feb 08 Python
介绍Python的Django框架中的静态资源管理器django-pipeline
Apr 25 Python
快速了解Python中的装饰器
Jan 11 Python
pytorch 调整某一维度数据顺序的方法
Dec 08 Python
selenium+Chrome滑动验证码破解二(某某网站)
Dec 17 Python
python如何通过pyqt5实现进度条
Jan 20 Python
Python 写了个新型冠状病毒疫情传播模拟程序
Feb 14 Python
python 中的paramiko模块简介及安装过程
Feb 29 Python
django-csrf使用和禁用方式
Mar 13 Python
Python实现CAN报文转换工具教程
May 05 Python
Django ORM filter() 的运用详解
May 14 Python
使用 prometheus python 库编写自定义指标的方法(完整代码)
Jun 29 Python
python发送告警邮件脚本
Sep 17 #Python
python实现zabbix发送短信脚本
Sep 17 #Python
python通过zabbix api获取主机
Sep 17 #Python
Python从ZabbixAPI获取信息及实现Zabbix-API 监控的方法
Sep 17 #Python
python实现Zabbix-API监控
Sep 17 #Python
centos6.8安装python3.7无法import _ssl的解决方法
Sep 17 #Python
Python从使用线程到使用async/await的深入讲解
Sep 16 #Python
You might like
如何将数据从文本导入到mysql
2006/10/09 PHP
phpmyadmin出现Cannot start session without errors问题解决方法
2014/08/14 PHP
php基于Snoopy解析网页html的方法
2015/07/09 PHP
PHP定义字符串的四种方式详解
2018/02/06 PHP
php中输出json对象的值(实现方法)
2018/03/07 PHP
php使用filter_var函数判断邮箱,url,ip格式示例
2019/07/06 PHP
PHP中迭代器的简单实现及Yii框架中的迭代器实现方法示例
2020/04/26 PHP
解析DHTML,JavaScript,DOM,BOM以及WEB标准的描述
2013/06/19 Javascript
javascript:void(0)是什么意思示例介绍
2013/11/17 Javascript
js的[defer]和[async]属性
2014/11/24 Javascript
DOM操作原生js 的bug,使用jQuery 可以消除的解决方法
2016/09/04 Javascript
vue中axios解决跨域问题和拦截器的使用方法
2018/03/07 Javascript
解决eclipse中没有js代码提示的问题
2018/10/10 Javascript
微信小程序 textarea 层级过高问题简单解决方案
2019/10/14 Javascript
判断JavaScript中的两个变量是否相等的操作符
2019/12/21 Javascript
python采集博客中上传的QQ截图文件
2014/07/18 Python
python虚拟环境virualenv的安装与使用
2016/12/18 Python
python实现写数字文件名的递增保存文件方法
2018/10/25 Python
Python 运行 shell 获取输出结果的实例
2019/01/07 Python
python实现字符串加密成纯数字
2019/03/19 Python
解决pycharm下pyuic工具使用的问题
2020/04/08 Python
便携式太阳能系统的创新者:GOAL ZERO
2018/02/04 全球购物
Rodd & Gunn澳大利亚官网:新西兰男装品牌
2018/09/25 全球购物
会话Bean的种类
2013/11/07 面试题
数据库笔试题
2013/05/09 面试题
优秀学生干部推荐材料
2014/02/03 职场文书
社区志愿者活动总结
2014/06/26 职场文书
2014单位领导班子四风对照检查材料思想汇报
2014/09/25 职场文书
英文产品推荐信
2015/03/27 职场文书
2015年度企业工作总结
2015/05/21 职场文书
倡议书怎么写?
2019/04/11 职场文书
Python实现Telnet自动连接检测密码的示例
2021/04/16 Python
浅谈Python从全局与局部变量到装饰器的相关知识
2021/06/21 Python
AJAX实现省市县三级联动效果
2021/10/16 Javascript
javascript对象3个属性特征
2021/11/17 Javascript
Mysql 数据库中的 redo log 和 binlog 写入策略
2022/04/26 MySQL