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 相关文章推荐
举例讲解Django中数据模型访问外键值的方法
Jul 21 Python
Python数组遍历的简单实现方法小结
Apr 27 Python
Python创建二维数组实例(关于list的一个小坑)
Nov 07 Python
python3.6的venv模块使用详解
Aug 01 Python
Python批处理更改文件名os.rename的方法
Oct 26 Python
详解python中@的用法
Mar 27 Python
Python:slice与indices的用法
Nov 25 Python
opencv-python 提取sift特征并匹配的实例
Dec 09 Python
pycharm设置默认的UTF-8编码模式的方法详解
Jun 01 Python
pycharm 使用tab跳出正在编辑的括号(){}{}等问题
Feb 26 Python
在 Golang 中实现 Cache::remember 方法详解
Mar 30 Python
Python读取文件夹下的所有文件实例代码
Apr 02 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
PHP新手上路(十三)
2006/10/09 PHP
第七节--类的静态成员
2006/11/16 PHP
PHP产生不重复随机数的5个方法总结
2014/11/12 PHP
PHP中预定义的6种接口介绍
2015/05/12 PHP
PHP调用存储过程返回值不一致问题的解决方法分析
2016/04/26 PHP
PHP正则表达式过滤html标签属性(DEMO)
2016/05/04 PHP
PHP 返回13位时间戳的实现代码
2016/05/13 PHP
PHP实现时间比较和时间差计算的方法示例
2017/07/24 PHP
Jquery UI实现一次拖拽多个选中的元素操作
2020/12/01 Javascript
vue.js学习笔记之绑定style样式和class列表
2016/10/31 Javascript
DOM 事件的深入浅出(一)
2016/12/05 Javascript
微信小程序(三):网络请求
2017/01/13 Javascript
详谈js中数组(array)和对象(object)的区别
2017/02/27 Javascript
利用ES6的Promise.all实现至少请求多长时间的实例
2017/08/28 Javascript
vue多种弹框的弹出形式的示例代码
2017/09/18 Javascript
js实现鼠标单击Tab表单切换效果
2018/05/16 Javascript
vue通过style或者class改变样式的实例代码
2018/10/30 Javascript
详解实现vue的数据响应式原理
2021/01/20 Vue.js
Python获取服务器信息的最简单实现方法
2015/03/05 Python
Python 类与元类的深度挖掘 I【经验】
2016/05/06 Python
Python+Pika+RabbitMQ环境部署及实现工作队列的实例教程
2016/06/29 Python
Python中的字符串操作和编码Unicode详解
2017/01/18 Python
python 3.6.5 安装配置方法图文教程
2018/09/18 Python
Jupyter Notebook打开任意文件夹操作
2020/04/14 Python
python的Jenkins接口调用方式
2020/05/12 Python
python3.6.5基于kerberos认证的hive和hdfs连接调用方式
2020/06/06 Python
HTML5实现晶莹剔透的雨滴特效
2014/05/14 HTML / CSS
Html5实现首页动态视频背景的示例代码
2019/09/25 HTML / CSS
吃透移动端 Html5 响应式布局
2019/12/16 HTML / CSS
护士的岗位职责
2013/12/04 职场文书
洗车工岗位职责
2014/03/15 职场文书
不忘国耻振兴中华演讲稿
2014/05/14 职场文书
舞蹈兴趣小组活动总结
2014/07/07 职场文书
政府个人对照检查材料思想汇报
2014/10/08 职场文书
简短的36句中秋节祝福信息语句
2019/09/09 职场文书
Jackson 反序列化时实现大小写不敏感设置
2021/06/29 Java/Android