日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python脚本根据cookies自动登录网站_python实现带验证码网站的自动登陆实现代码...

發布時間:2024/7/23 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python脚本根据cookies自动登录网站_python实现带验证码网站的自动登陆实现代码... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

早聽說用python做網絡爬蟲非常方便,正好這幾天單位也有這樣的需求,需要登陸XX網站下載部分文檔,于是自己親身試驗了一番,效果還不錯。

本例所登錄的某網站需要提供用戶名,密碼和驗證碼,在此使用了python的urllib2直接登錄網站并處理網站的Cookie。

Cookie的工作原理:

Cookie由服務端生成,然后發送給瀏覽器,瀏覽器會將Cookie保存在某個目錄下的文本文件中。在下次請求同一網站時,會發送該Cookie給服務器,這樣服務器就知道該用戶是否合法以及是否需要重新登錄。

Python提供了基本的cookielib庫,在首次訪問某頁面時,cookie便會自動保存下來,之后訪問其它頁面便都會帶有正常登錄的Cookie了。

原理:

(1)激活cookie功能

(2)反“反盜鏈”,偽裝成瀏覽器訪問

(3)訪問驗證碼鏈接,并將驗證碼圖片下載到本地

(4)驗證碼的識別方案網上較多,python也有自己的圖像處理庫,此例調用了火車頭采集器的OCR識別接口。

(5)表單的處理,可用fiddler等抓包工具獲取需要提交的參數

(6)生成需要提交的數據,生成http請求并發送

(7)根據返回的js頁面判斷是否登陸成功

(8)登陸成功后下載其它頁面

此例中使用多個賬號輪詢登陸,每個賬號下載3個頁面。

下載網址因為某些問題,就不透露了。

以下是部分代碼:

#!usr/bin/env python

#-*- coding: utf-8 -*-

import os

import urllib2

import urllib

import cookielib

import xml.etree.ElementTree as ET

#-----------------------------------------------------------------------------

# Login in www.***.com.cn

def ChinaBiddingLogin(url, username, password):

# Enable cookie support for urllib2

cookiejar=cookielib.CookieJar()

urlopener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))

urllib2.install_opener(urlopener)

urlopener.addheaders.append(('Referer', 'http://www.chinabidding.com.cn/zbw/login/login.jsp'))

urlopener.addheaders.append(('Accept-Language', 'zh-CN'))

urlopener.addheaders.append(('Host', 'www.chinabidding.com.cn'))

urlopener.addheaders.append(('User-Agent', 'Mozilla/5.0 (compatible; MISE 9.0; Windows NT 6.1); Trident/5.0'))

urlopener.addheaders.append(('Connection', 'Keep-Alive'))

print 'XXX Login......'

imgurl=r'http://www.*****.com.cn/zbw/login/image.jsp'

DownloadFile(imgurl, urlopener)

authcode=raw_input('Please enter the authcode:')

#authcode=VerifyingCodeRecognization(r"http://192.168.0.106/images/code.jpg")

# Send login/password to the site and get the session cookie

values={'login_id':username, 'opl':'op_login', 'login_passwd':password, 'login_check':authcode}

urlcontent=urlopener.open(urllib2.Request(url, urllib.urlencode(values)))

page=urlcontent.read(500000)

# Make sure we are logged in, check the returned page content

if page.find('login.jsp')!=-1:

print 'Login failed with username=%s, password=%s and authcode=%s' \

% (username, password, authcode)

return False

else:

print 'Login succeeded!'

return True

#-----------------------------------------------------------------------------

# Download from fileUrl then save to fileToSave

# Note: the fileUrl must be a valid file

def DownloadFile(fileUrl, urlopener):

isDownOk=False

try:

if fileUrl:

outfile=open(r'/var/www/images/code.jpg', 'w')

outfile.write(urlopener.open(urllib2.Request(fileUrl)).read())

outfile.close()

isDownOK=True

else:

print 'ERROR: fileUrl is NULL!'

except:

isDownOK=False

return isDownOK

#------------------------------------------------------------------------------

# Verifying code recoginization

def VerifyingCodeRecognization(imgurl):

url=r'http://192.168.0.119:800/api?'

user='admin'

pwd='admin'

model='ocr'

ocrfile='cbi'

values={'user':user, 'pwd':pwd, 'model':model, 'ocrfile':ocrfile, 'imgurl':imgurl}

data=urllib.urlencode(values)

try:

url+=data

urlcontent=urllib2.urlopen(url)

except IOError:

print '***ERROR: invalid URL (%s)' % url

page=urlcontent.read(500000)

# Parse the xml data and get the verifying code

root=ET.fromstring(page)

node_find=root.find('AddField')

authcode=node_find.attrib['data']

return authcode

#------------------------------------------------------------------------------

# Read users from configure file

def ReadUsersFromFile(filename):

users={}

for eachLine in open(filename, 'r'):

info=[w for w in eachLine.strip().split()]

if len(info)==2:

users[info[0]]=info[1]

return users

#------------------------------------------------------------------------------

def main():

login_page=r'http://www.***.com.cnlogin/login.jsp'

download_page=r'http://www.***.com.cn***/***?record_id='

start_id=8593330

end_id=8595000

now_id=start_id

Users=ReadUsersFromFile('users.conf')

while True:

for key in Users:

if ChinaBiddingLogin(login_page, key, Users[key]):

for i in range(3):

pageUrl=download_page+'%d' % now_id

urlcontent=urllib2.urlopen(pageUrl)

filepath='./download/%s.html' % now_id

f=open(filepath, 'w')

f.write(urlcontent.read(500000))

f.close()

now_id+=1

else:

continue

#------------------------------------------------------------------------------

if __name__=='__main__':

main()

總結

以上是生活随笔為你收集整理的python脚本根据cookies自动登录网站_python实现带验证码网站的自动登陆实现代码...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。