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在命令行下使用google翻译(带语音)
Jan 16 Python
利用Python实现颜色色值转换的小工具
Oct 27 Python
Python IDLE清空窗口的实例
Jun 25 Python
python用post访问restful服务接口的方法
Dec 07 Python
如何用C代码给Python写扩展库(Cython)
May 17 Python
python实现月食效果实例代码
Jun 18 Python
python+openCV利用摄像头实现人员活动检测
Jun 22 Python
用python写测试数据文件过程解析
Sep 25 Python
使用 tf.nn.dynamic_rnn 展开时间维度方式
Jan 21 Python
打包PyQt5应用时的注意事项
Feb 14 Python
Python爬取YY评级分数并保存数据实现过程解析
Jun 01 Python
使用qt quick-ListView仿微信好友列表和聊天列表的示例代码
Jun 13 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的Yii框架中的缓存功能
2016/03/29 PHP
Apply an AutoFormat to an Excel Spreadsheet
2007/06/12 Javascript
Javascript 表单之间的数据传递代码
2008/12/04 Javascript
Javascript 面向对象 继承
2010/05/13 Javascript
jquery遍历select元素(实例讲解)
2013/12/31 Javascript
javascript实现无限级select联动菜单
2015/01/02 Javascript
编写自己的jQuery提示框(Tip)插件
2015/02/05 Javascript
js读取并解析JSON类型数据的方法
2015/11/14 Javascript
分享js粘帖屏幕截图到web页面插件screenshot-paste
2020/08/21 Javascript
开启BootStrap学习之旅
2016/05/04 Javascript
vuejs指令详解
2017/02/07 Javascript
浅谈SpringMVC中post checkbox 多选框value的值(隐藏域方式)
2018/01/08 Javascript
axios全局请求参数设置,请求及返回拦截器的方法
2018/03/05 Javascript
详解如何在微信小程序开发中正确的使用vant ui组件
2018/09/13 Javascript
axios+Vue实现上传文件显示进度功能
2019/04/14 Javascript
浅谈vuex的基本用法和mapaction传值问题
2019/11/08 Javascript
JavaScript(js)处理的HTML事件、键盘事件、鼠标事件简单示例
2019/11/19 Javascript
微信小程序实现页面浮动导航
2020/01/08 Javascript
Vue触发input选取文件点击事件操作
2020/08/07 Javascript
vant中的toast轻提示实现代码
2020/11/04 Javascript
vue print.js打印支持Echarts图表操作
2020/11/13 Javascript
[01:01]青春无憾,一战成名——DOTA2全国高校联赛开启
2018/02/25 DOTA
[01:05:30]VP vs TNC 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
python动态监控日志内容的示例
2014/02/16 Python
python读写json文件的简单实现
2017/04/11 Python
python图片二值化提高识别率代码实例
2019/08/24 Python
为什么说python适合写爬虫
2020/06/11 Python
Python爬虫之Selenium鼠标事件的实现
2020/12/04 Python
美国家喻户晓的保健品品牌:Vitamin World(维他命世界)
2016/08/19 全球购物
Champion澳大利亚官网:美国冠军运动服装
2018/05/07 全球购物
高中化学教学反思
2014/01/13 职场文书
少儿节目主持串词
2014/04/02 职场文书
思想作风建设心得体会
2014/10/22 职场文书
降价通知函
2015/04/23 职场文书
2016年“世界环境日”校园广播稿
2015/12/18 职场文书
浅谈MySQL之浅入深出页原理
2021/06/23 MySQL