如何利用python模擬登錄(附源碼)

1.關於python模擬登錄,本質上是利用python腳本模擬瀏覽器登錄,沒有任何安全性問題

2.關於一些python知識,我就不多說,網上應該有很多,讀者可以在網上找到很多

3.在這裏附上我的人人登錄,並講一下實現步驟

#!/bin/python
#encoding=utf-8
import HTMLParser
import urlparse
import urllib
import urllib2
import cookielib
import string
import re


#登錄界面的主頁
hosturl="http://www.renren.com"

#這裏是將用戶名和密碼等發送到的頁面上,這裏需要抓包(我的方法是打開chrome瀏覽器,打開到登錄頁面,打開“審查元素”,在裏面的網絡裏面查到的)
posturl="http://www.renren.com/ajaxLogin/login?"

#生成cookie
cj=cookielib.LWPCookieJar()
cookie_support=urllib2.HTTPCookieProcessor(cj)
opener=urllib2.build_opener(cookie_support,urllib2.HTTPHandler)
urllib2.install_opener(opener)

#打開登錄界面,獲取cookie,並將該cookie保存下來
h=urllib2.urlopen(hosturl)

#構造頭,這方法和上面獲取posturl的方法是一樣的
headers={
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17'
,'Referer':'http://www.renren.com/SysHome.do'
}

#發送的數據,方法同上
postdata={
'captcha_type' : 'web_login',
'domain' : 'renren.com',
'email' : '********',  #用戶名
'icode': '',
'key_id': '1',
'origURL': 'http://www.renren.com/home',
'password': '*******************', #密碼,這裏是密文
'rkey': 'd0cf42c2d3d337f9e5d14083f2d52cb2'
}
 
#將數據進行編碼
postdata=urllib.urlencode(postdata)


#構造一個請求消息
request=urllib2.Request(posturl,postdata,headers)
print "request:%s " %request


#發送一個請求消息
response=urllib2.urlopen(request)
text=response.read()


print "text:%s" %text


listvalue=text.split(",")

#獲取到人人登錄的主頁,這裏每個人也許會不一樣,每個人需要根據自己的text的裏面數據來解析
renrenhttp="http:"+listvalue[1].split(":")[2];
print "renrenhttp:%s" %renrenhttp


print urllib2.urlopen(renrenhttp).read()


4.接下來在附上自己蘇大網關的python,多練幾個來練手(這裏就不添加註釋了,註釋同上)

#!/bin/python
#encoding=utf-8
import HTMLParser
import urlparse
import urllib2
import urllib
import cookielib
import string
import re


hosturl="http://wg.suda.edu.cn/index.aspx"


posturl="http://wg.suda.edu.cn/index.aspx"


cj=cookielib.LWPCookieJar()
cookie_support=urllib2.HTTPCookieProcessor(cj)
opener=urllib2.build_opener(cookie_support,urllib2.HTTPHandler)
urllib2.install_opener(opener)


h=urllib2.urlopen(hosturl)


headers={
"Referer":"http://wg.suda.edu.cn/index.aspx",
"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17"
}


postdata={
'TextBox1':'*******',  #用戶名,這裏都是明文
'TextBox2':'*******', #密碼
'nw':'RadioButton1',
'tm':'RadioButton6',
'Button1':'登錄網關'
 }
 


postdata=urllib.urlencode(postdata)


request=urllib2.Request(posturl,postdata,headers)
print "request:%s" %request


response=urllib2.urlopen(request)
text=response.read()
#print "text:%s" %text


5.推薦一些網站

        我也是借鑑了網上的大神的文章才完成的,在這裏推薦幾個大神的文章

    

      http://blog.csdn.net/lmh12506/article/details/7818306   這裏是關於如何實現模擬登錄的,我主要借鑑了這篇文章

      http://docs.python.org/2/library/htmlparser.html  這裏的文章是講解如何解析html,便於實現自己的操作,如download 圖片,這個在實現自己登錄後,有很大的操作空間




每個人在轉載是希望表明轉載出處 ,謝謝!



















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章