Beginner Python has a period of time, winter vacation and read some knowledge of Web programming, wrote a BBS to achieve automatic login and post the Python script
Because of my school BBS home use JS load, seemingly through the first page login is not feasible, and then analyze the form, found that by post some data to a specific address, after
Gets the returned cookie. This is good to do, nonsense not much to say, on the code. (Beginner, not pythonic, forgive)
#-*-coding:utf-8-*-#lilybbs. py #Author: Sky_money #Python实现自动登录BBS并发帖 import urllib,urllib2 #帐号和密码, you know Username= ' password= ' baseurl= ' http://bbs.nju.edu.cn/vd45734/bbslogin?type=2 ' Postdata=urllib.urlencode ({' id ': username, ' p W ':p assword, ' Lasturl ': '} ' req=urllib2. Request (Url=baseurl, Data=postdata) Result=urllib2.urlopen (req). Read () #从返回的Content中抓取Cookie start=result.find (" (' ") +2 end=result.find (" ') ") Cookie=result[start:end] split1=cookie.find (' N ') split2=cookie.find (' + ') u_num=int ( COOKIE[0:SPLIT1]) U_id=cookie[split1+1:split2] U_key=int (cookie[split2+1:]) footkey=217872412 #下面是发帖验证 testUrl= ' Http://bbs.nju.edu.cn/vd78013/bbssnd?board=test ' sendcookie= ' _u_num=%d;_u_uid=%s;_u_key=%d;
footkey=%d "% (u_num+2,u_id, U_key-2,footkey) #发送的Headers, must have cookies Sendheaders = {' Host ': ' bbs.nju.edu.cn ', ' Connection ': ' keep-alive ', ' user-agent ': ' mozilla/5.0 ( Windows NT 6.1) applewebkit/535.2 (khtml,like Gecko) chrome/15.0.874.121 safari/535.2 ', ' Accept ': ' Text/html,application/xhtml+xml,application/xml ; q=0.9,*/*;q=0.8 ', ' accept-encoding ': ' gzip,deflate,sdch ', ' accept-language ': ' zh-cn,zh;q=0.8 ', ' A Ccept-charset ': ' gbk,utf-8;q=0.7,*;q=0.3 ',} sendheaders[' Cookie ']=sendcookie #发帖的data主体 body=urllib.urlencode ({' Ti Tle ': ' Test ', ' pid ': ' 0 ', ' Reid ': ' 0 ', ' signature ': ' 1 ', ' AUTOCR ': ' On ', ' text ': ' Oh yeah '} retur Nedreq=urllib2. Request (Url=testurl, Data=body, headers=sendheaders) Returnedresult=urllib2.urlopen (returnedreq). Read () Print Returnedresult
Code all the way to the end, more ugly, just look at the
The main use of Urllib and URLLIB2 modules.
First login issues, the need to post the data can be viewed through the Ff+httpfox, this is more convenient. After you know the data you need, dots it, and then simulate it.
The URL sent in the/VD followed by a string of numbers is a random assignment, this can be distributed by random number, there is no specific requirements.
After fetching the cookie to get the post ' token ', where the cookie is divided into four parts, _u_num,_u_uid,_u_key,footkey, where _u_uid is the username, _u_num and _u_key are returned a series of values that need to be noted , the post is a cookie need to _u_num value plus 2,_u_key value minus 2, this is more strange, may be the school BBS set up the problem. Footkey seems to be randomly assigned to a string of numbers.
The above is automatic login and post a simple script, now found Python write this thing is quite fun, and then want to consider how to login webqq, this seemingly need to understand the QQ Communication protocol TX ....