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实现将16进制字符串转化为ascii字符的方法分析
Jul 21 Python
详谈python3中用for循环删除列表中元素的坑
Apr 19 Python
python高阶爬虫实战分析
Jul 29 Python
pyqt5对用qt designer设计的窗体实现弹出子窗口的示例
Jun 19 Python
浅谈Python中函数的定义及其调用方法
Jul 19 Python
python选取特定列 pandas iloc,loc,icol的使用详解(列切片及行切片)
Aug 06 Python
python使用celery实现异步任务执行的例子
Aug 28 Python
Pytorch Tensor的统计属性实例讲解
Dec 30 Python
python实现拼接图片
Mar 23 Python
Django调用百度AI接口实现人脸注册登录代码实例
Apr 23 Python
python实现一个简单RPC框架的示例
Oct 28 Python
TensorFlow中tf.batch_matmul()的用法
Jun 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+mysql写的留言本
2006/10/09 PHP
discuz authcode 经典php加密解密函数解析
2020/07/12 PHP
Laravel框架中扩展函数、扩展自定义类的方法
2014/09/04 PHP
PHP正则表达式笔记与实例详解
2019/05/09 PHP
Laravel5.1 框架Middleware中间件基本用法实例分析
2020/01/04 PHP
[原创]js与自动伸缩图片 自动缩小图片的多浏览器兼容的方法总结
2007/03/12 Javascript
利用谷歌地图API获取点与点的距离的js代码
2012/10/11 Javascript
用JS提交参数创建form表单在FireFox中遇到的问题
2013/01/16 Javascript
jquery表单验证框架提供的身份证验证方法(示例代码)
2013/12/27 Javascript
用jQuery实现的智能隐藏、滑动效果的返回顶部代码
2014/03/18 Javascript
jQuery+AJAX实现网页无刷新上传
2015/02/22 Javascript
jQuery在页面加载时动态修改图片尺寸的方法
2015/03/20 Javascript
javascript实现百度地图鼠标滑动事件显示、隐藏
2015/04/02 Javascript
JavaScript获取URL汇总
2015/06/08 Javascript
javascript瀑布流式图片懒加载实例解析与优化
2016/02/23 Javascript
AngularJS用户选择器指令实例分析
2016/11/04 Javascript
JavaScript中捕获/阻止捕获、冒泡/阻止冒泡方法
2016/12/07 Javascript
Vue仿今日头条实例详解
2018/02/06 Javascript
bootstrap select2插件用ajax来获取和显示数据的实例
2018/08/09 Javascript
基于vue实现滚动条滚动到指定位置对应位置数字进行tween特效
2019/04/18 Javascript
vue 实现LED数字时钟效果(开箱即用)
2019/12/08 Javascript
Node.js API详解之 util模块用法实例分析
2020/05/09 Javascript
[02:58]魔廷新尊——痛苦女王至宝语音台词节选
2020/06/14 DOTA
python中sets模块的用法实例
2014/09/30 Python
python pandas中DataFrame类型数据操作函数的方法
2018/04/08 Python
Python 批量读取文件中指定字符的实现
2020/03/06 Python
基于Python爬取fofa网页端数据过程解析
2020/07/13 Python
Html5页面中的返回实现的方法
2018/02/26 HTML / CSS
Fossil加拿大官网:化石手表、手袋、首饰及配饰
2019/04/23 全球购物
教师三严三实心得体会
2014/10/11 职场文书
领导班子对照检查剖析材料
2014/10/13 职场文书
升学宴学生答谢词
2015/01/05 职场文书
Python获取江苏疫情实时数据及爬虫分析
2021/08/02 Python
《现实主义勇者的王国再建记》第三弹OST全曲试听片段公开
2022/04/04 日漫
vue实力踩坑之push当前页无效
2022/04/10 Vue.js
实现GO语言对数组切片去重
2022/04/20 Golang