這篇文章給大家分享的是有關python實現自動登錄的方法的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
利用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+=12.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實現自動登錄的方法”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。