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之关于循环的小伎俩
Oct 02 Python
Python数据结构与算法之图的最短路径(Dijkstra算法)完整实例
Dec 12 Python
Python常见字符串操作函数小结【split()、join()、strip()】
Feb 02 Python
解决Python运行文件出现out of memory框的问题
Dec 03 Python
Django中如何使用sass的方法步骤
Jul 09 Python
Python元组 tuple的概念与基本操作详解【定义、创建、访问、计数、推导式等】
Oct 30 Python
python实现猜数游戏
Mar 27 Python
如何基于windows实现python定时爬虫
May 01 Python
python numpy矩阵信息说明,shape,size,dtype
May 22 Python
python3.9.1环境安装的方法(图文)
Feb 02 Python
matplotlib之pyplot模块坐标轴标签设置使用(xlabel()、ylabel())
Feb 22 Python
新手必备Python开发环境搭建教程
May 28 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&amp;mysql(一)
2006/10/09 PHP
VIM中设置php自动缩进为4个空格的方法详解
2013/06/14 PHP
php中in_array函数用法探究
2014/11/25 PHP
php中array_slice和array_splice函数解析
2016/10/18 PHP
关于PHP定时发送服务的解决办法
2017/04/23 PHP
解析Jquery取得iframe中元素的几种方法
2013/07/04 Javascript
javascript中bind函数的作用实例介绍
2014/09/28 Javascript
jquery实现标签支持图文排列带上下箭头按钮的选项卡
2015/03/14 Javascript
jQuery实现切换页面过渡动画效果
2015/10/29 Javascript
jquery 无限极下拉菜单的简单实例(精简浓缩版)
2016/05/31 Javascript
基于构造函数的五种继承方法小结
2017/07/27 Javascript
Vue基本使用之对象提供的属性功能
2019/04/30 Javascript
微信小程序官方动态自定义底部tabBar的例子
2019/09/04 Javascript
vue动态禁用控件绑定disable的例子
2019/10/28 Javascript
JS实现简单日历特效
2020/01/03 Javascript
解决vux 中popup 组件Mask 遮罩在最上层的问题
2020/11/03 Javascript
[02:26]DOTA2英雄米拉娜基础教程
2013/11/25 DOTA
Python计时相关操作详解【time,datetime】
2017/05/26 Python
python对日志进行处理的实例代码
2018/10/06 Python
如何在Django项目中引入静态文件
2019/07/26 Python
如何通过python实现全排列
2020/02/11 Python
HTML+CSS+JavaScript实现图片3D展览的示例代码
2020/10/12 HTML / CSS
日本最大的眼镜购物网站:Oh My Glasses
2016/11/13 全球购物
施华洛世奇澳大利亚官网:SWAROVSKI澳大利亚
2017/01/06 全球购物
Ralph Lauren拉夫·劳伦美国官网:带有浓郁美国气息的高品味时装品牌
2017/11/01 全球购物
什么是ARP(Address Resolution Protocol)地址解析协议
2013/10/31 面试题
参观接待方案
2014/03/17 职场文书
酒店仓管员岗位职责
2014/04/28 职场文书
建材投资建议书
2014/05/16 职场文书
校园元旦活动总结
2014/07/09 职场文书
2014年房地产销售工作总结
2014/12/01 职场文书
项目合作意向书
2015/05/08 职场文书
2019已经过半,你知道年中工作总结该怎么写吗?
2019/07/03 职场文书
在前女友婚礼上,用Python破解了现场的WIFI还把名称改成了
2021/05/28 Python
使用Djongo模块在Django中使用MongoDB数据库
2021/06/20 Python
SpringBoot整合Minio文件存储
2022/04/03 Java/Android