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 相关文章推荐
Pycharm学习教程(2) 代码风格
May 02 Python
python遍历一个目录,输出所有的文件名的实例
Apr 23 Python
Python中一些不为人知的基础技巧总结
May 19 Python
numpy判断数值类型、过滤出数值型数据的方法
Jun 09 Python
Python3实现爬取简书首页文章标题和文章链接的方法【测试可用】
Dec 11 Python
Python实现京东秒杀功能代码
May 16 Python
python实现把二维列表变为一维列表的方法分析
Oct 08 Python
Pytorch保存模型用于测试和用于继续训练的区别详解
Jan 10 Python
python3实现往mysql中插入datetime类型的数据
Mar 02 Python
Python3内置函数chr和ord实现进制转换
Jun 05 Python
Opencv中cv2.floodFill算法的使用
Jun 18 Python
Python可变集合和不可变集合的构造方法大全
Dec 06 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 pcntl_fork和pcntl_fork 的用法
2009/04/13 PHP
php文件上传及下载附带显示文件及目录功能
2017/04/27 PHP
laravel实现按月或天或小时统计mysql数据的方法
2019/10/09 PHP
PHP多进程简单实例小结
2019/11/09 PHP
jquery select操作的日期联动实现代码
2009/12/06 Javascript
Document对象内容集合(比较全)
2010/09/06 Javascript
javascript游戏开发之《三国志曹操传》零部件开发(一)让静态人物动起来
2013/01/23 Javascript
原生JavaScript实现动态省市县三级联动下拉框菜单实例代码
2016/02/03 Javascript
详解如何在vue中使用sass
2017/06/21 Javascript
React-Native做一个文本输入框组件的实现代码
2017/08/10 Javascript
nodejs动态创建二维码的方法
2017/08/12 NodeJs
vue-cli构建项目使用 less的方法
2017/10/04 Javascript
js+springMVC 提交数组数据到后台的实例
2019/09/21 Javascript
[36:13]Mineski vs iG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
[45:25]完美世界DOTA2联赛循环赛 PXG vs IO 第一场 11.06
2020/11/09 DOTA
python实现爬虫下载美女图片
2015/07/14 Python
django基础之数据库操作方法(详解)
2017/05/24 Python
Python 调用Java实例详解
2017/06/02 Python
python如何对实例属性进行类型检查
2018/03/20 Python
Python错误处理操作示例
2018/07/18 Python
基于Python的一个自动录入表格的小程序
2020/08/05 Python
Python headers请求头如何实现快速添加
2020/11/03 Python
next在python中返回迭代器的实例方法
2020/12/15 Python
如何用python批量调整视频声音
2020/12/22 Python
python将YUV420P文件转PNG图片格式的两种方法
2021/01/22 Python
AmazeUI 点击元素显示全屏的实现
2020/08/25 HTML / CSS
英国太阳镜品牌:Taylor Morris Eyewear
2018/04/18 全球购物
绩效专员岗位职责
2013/12/02 职场文书
保密承诺书
2014/03/27 职场文书
三方协议书范本
2014/04/22 职场文书
事业单位考核材料
2014/05/21 职场文书
计算机售后服务承诺书
2014/05/30 职场文书
企业负责人任命书
2014/06/05 职场文书
个人整改措施落实情况汇报
2014/10/29 职场文书
财务统计员岗位职责
2015/04/14 职场文书
2015年政务公开工作总结
2015/05/19 职场文书