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使用PyFetion来发送短信的例子
Apr 22 Python
Python的装饰器模式与面向切面编程详解
Jun 21 Python
Python生成随机验证码的两种方法
Dec 22 Python
用yum安装MySQLdb模块的步骤方法
Dec 15 Python
在python中安装basemap的教程
Sep 20 Python
python3.6利用pyinstall打包py为exe的操作实例
Oct 31 Python
Pytorch 抽取vgg各层并进行定制化处理的方法
Aug 20 Python
解决pycharm 安装numpy失败的问题
Dec 05 Python
使用python实现哈希表、字典、集合操作
Dec 22 Python
PIP和conda 更换国内安装源的方法步骤
Sep 21 Python
Numpy数组的广播机制的实现
Nov 03 Python
Python 数据分析之逐块读取文本的实现
Dec 14 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 jsonp单引号转义
2014/11/23 PHP
php使用Jpgraph绘制柱形图的方法
2015/06/10 PHP
帝国cms常用标签汇总
2015/07/06 PHP
CodeIgniter开发实现支付宝接口调用的方法示例
2016/11/14 PHP
PHP中如何使用Redis接管文件存储Session详解
2018/11/28 PHP
浅析PHP7 的垃圾回收机制
2019/09/06 PHP
TNC vs IO BO3 第二场2.13
2021/03/10 DOTA
一个选择最快的服务器转向代码
2009/04/27 Javascript
js控制div及网页相关属性的代码
2009/12/19 Javascript
基于jQuery全屏焦点图左右切换插件responsiveslides
2015/09/07 Javascript
JavaScript中Function函数与Object对象的关系
2015/12/17 Javascript
js实现界面向原生界面发消息并跳转功能
2016/11/22 Javascript
Nodejs中Express 常用中间件 body-parser 实现解析
2017/05/22 NodeJs
关于HTML5的data-*自定义属性的总结
2018/05/05 Javascript
JavaScript内置对象math,global功能与用法实例分析
2019/06/10 Javascript
用jQuery实现抽奖程序
2020/04/12 jQuery
请求时token过期自动刷新token操作
2020/09/11 Javascript
[36:33]Ti4 循环赛第四日 附加赛NEWBEE vs Mouz
2014/07/13 DOTA
python算法学习之计数排序实例
2013/12/18 Python
Python实现将绝对URL替换成相对URL的方法
2015/06/28 Python
举例讲解Python中的迭代器、生成器与列表解析用法
2016/03/20 Python
详解Python中heapq模块的用法
2016/06/28 Python
Python numpy实现数组合并实例(vstack,hstack)
2018/01/09 Python
基于python log取对数详解
2018/06/08 Python
python中字典按键或键值排序的实现代码
2019/08/27 Python
python数据持久存储 pickle模块的基本使用方法解析
2019/08/30 Python
在Mac中PyCharm配置python Anaconda环境过程图解
2020/03/11 Python
基于Django signals 信号作用及用法详解
2020/03/28 Python
python filecmp.dircmp实现递归比对两个目录的方法
2020/05/22 Python
Python中openpyxl实现vlookup函数的实例
2020/10/28 Python
使用phonegap进行本地存储的实现方法
2017/03/31 HTML / CSS
携程旅行网:中国领先的在线旅行服务公司
2017/02/17 全球购物
yy结婚证婚词
2014/01/10 职场文书
会计学毕业生求职信
2014/06/25 职场文书
学校党支部承诺书
2015/04/30 职场文书
OpenCV全景图像拼接的实现示例
2021/06/05 Python