当前位置: 移动技术网 > IT编程>脚本编程>Python > python实现自动登录

python实现自动登录

2018年09月23日  | 移动技术网IT编程  | 我要评论

末世谈txt下载,济宁郑媛媛老师照片,凤凰腾

利用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))

效果:

官网:

官方示例程序:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网