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实现网站注册验证码生成类
Jun 08 Python
python+opencv识别图片中的圆形
Mar 25 Python
Python使用numpy模块创建数组操作示例
Jun 20 Python
python算法题 链表反转详解
Jul 02 Python
python利用re,bs4,requests模块获取股票数据
Jul 29 Python
python 使用socket传输图片视频等文件的实现方式
Aug 07 Python
使用 pytorch 创建神经网络拟合sin函数的实现
Feb 24 Python
django rest framework serializer返回时间自动格式化方法
Mar 31 Python
python 异步async库的使用说明
May 04 Python
.img/.hdr格式转.nii格式的操作
Jul 01 Python
详解Python直接赋值,深拷贝和浅拷贝
Jul 09 Python
python3.8动态人脸识别的实现示例
Sep 21 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 调用远程url的六种方法小结
2009/11/02 PHP
php实现首页链接查询 友情链接检查的代码
2010/01/05 PHP
解析如何用php screw加密php源代码
2013/06/20 PHP
解析php中用PHPMailer来发送邮件的示例(126.com的例子)
2013/06/24 PHP
Ajax+PHP快速上手及简单应用说明
2013/07/24 PHP
使用php语句将数据库*.sql文件导入数据库
2014/05/05 PHP
PHP获取数组中重复最多的元素的实现方法
2014/11/11 PHP
thinkPHP数据查询常用方法总结【select,find,getField,query】
2017/03/15 PHP
redis+php实现微博(二)发布与关注功能详解
2019/09/23 PHP
Javascript 强制类型转换函数
2009/05/17 Javascript
超级酷和最实用的jQuery实例收集(20个)
2010/04/21 Javascript
JS中Iframe之间传值及子页面与父页面应用
2013/03/11 Javascript
JavaScript编程中容易出BUG的几点小知识
2015/01/31 Javascript
.NET微信公众号开发之创建自定义菜单
2015/07/16 Javascript
jquery实现简洁文件上传表单样式
2015/11/02 Javascript
Javascript 字符串模板的简单实现
2016/02/13 Javascript
原生JS实现的放大镜效果实例代码
2016/10/15 Javascript
JS实现滑动门效果的方法详解
2016/12/19 Javascript
手动初始化Angular的模块与控制器
2016/12/26 Javascript
JS作用域深度解析
2016/12/29 Javascript
Vue+Webpack完美整合富文本编辑器TinyMce的方法
2018/11/30 Javascript
Python利用多进程将大量数据放入有限内存的教程
2015/04/01 Python
python 远程统计文件代码分享
2015/05/14 Python
Python使用urllib2模块实现断点续传下载的方法
2015/06/17 Python
Python数据类型详解(一)字符串
2016/05/08 Python
Python制作exe文件简单流程
2019/01/24 Python
Python面向对象程序设计类变量与成员变量、类方法与成员方法用法分析
2019/04/12 Python
Python时间序列缺失值的处理方法(日期缺失填充)
2019/08/11 Python
Python callable内置函数原理解析
2020/03/05 Python
Python如何使用队列方式实现多线程爬虫
2020/05/12 Python
资产评估专业大学生求职信
2013/09/29 职场文书
大学生志愿者活动总结
2014/06/27 职场文书
2014银行领导班子群众路线对照检查材料思想汇报
2014/09/17 职场文书
学会感恩主题班会
2015/08/12 职场文书
幼师必备:幼儿园期末教师评语50条
2019/11/01 职场文书
PostgreSQL常用字符串分割函数整理汇总
2022/07/07 PostgreSQL