Python automatically logs on to the website (processing cookies)
Python code
- Def login ():
- Cj = cookielib. cookiejar ()
- Opener = urllib2.build _ opener (urllib2.httpcookieprocessor (CJ ))
- Login_url = r'http: // zhixing.bjtu.edu.cn/member.php? MoD = Logging & Action = login & loginsubmit = Yes & infloat = Yes & lssubmit = Yes & inajax = 1'
- Login_data = urllib. urlencode ({'cookietime': '000000', 'handlekey': 'Ls', 'Password': 'xxx ',
- 'Quickforward ': 'yes', 'username': 'digiter '})
- Opener. Open (login_url, login_data)
- Return Opener
After the result is returned, you only need to set the URL and data to post.
Do not set the header in the request. This is because the cookie is also the header. If the header is set, no cookie is found and no logon is performed.
Python code
- Request = urllib2.request (
- Url = r 'HTTP: // zhixing.bjtu.edu.cn/forum.php? MoD = Post & Action = newthread & FID = 601 & extra = & topicsubmit = Yes ',
- Data = Param
- )
- Print opener. Open (request). Read ()
It's easy to set the discuz x topic classification function (Cookies need to simulate the browser, here it is imitating Firefox) Python code
- #-*-Coding: UTF-8 -*-
- '''''
- Created on Dec 24,201 1
-
- @ Author: Rush
- '''
- Import urllib, urllib2, cookielib
- Import OS, time
- Headers = []
- Def login ():
- Cj = cookielib. cookiejar ()
- Opener = urllib2.build _ opener (urllib2.httpcookieprocessor (CJ ))
- Login_url = r'http: // zhixing.bjtu.edu.cn/member.php? MoD = Logging & Action = login & loginsubmit = Yes & infloat = Yes & lssubmit = Yes & inajax = 1'
- Login_data = urllib. urlencode ({'cookietime': '000000', 'handlekey': 'Ls', 'Password': 'xxx ',
- 'Quickforward ': 'yes', 'username': 'guoyuan '})
- Opener. addheaders = [('host', 'zhixing .bjtu.edu.cn '),
- ('User-agent', 'mozilla/5.0 (Ubuntu; X11; Linux i686; RV: 8.0) Gecko/20100101 Firefox/8.0 '),
- ('Access', 'text/html, application/XHTML + XML, application/XML; q = 0.9, */*; q = 100 '),
- ('Accept-color', 'en-US, en; q = 100 '),
- ('Accept-encoding', 'gzip, deflate '),
- ('Accept-charset', 'iso-8859-1, UTF-8; q = 0.7, *; q = 100 '),
- ('Connection', 'keep-alive '),
- ('Referer', 'HTTP: // zhixing.bjtu.edu.cn/forum.php'),]
- Opener. Open (login_url, login_data)
- Return Opener
- If _ name _ = '_ main __':
- Opener = login ()
- Url = r 'HTTP: // zhixing.bjtu.edu.cn/forum.php? MoD = topicadmin & Action = moderate & optgroup = 2 & modsubmit = Yes & infloat = Yes & inajax = 1'
- Data = {'fid': '000000', 'formhash': '0cdd1596 ', 'frommodcp': '', 'handlekey': 'mods ',
- 'Listexist': 'page % 3d62', 'moderate [] ': '000000', 'operations []': 'type', 'reason ':'...',
- 'Redirect': r'http: // zhixing.bjtu.edu.cn/thread-496146-1-1.html', 'typeid': '20140901 '}
- Data2 = [(k, V) for K, V in data. iteritems ()]
- CNT = 0
- For TID in range (493022,496 146 + 1 ):
- CNT + = 1
- If CNT % 20 = 0: Print
- Print tid,
- Data2.append ('moderate [] ', STR (TID )))
- If CNT % 40 = 0 or CNT = 496146:
- Request = urllib2.request (url = URL, Data = urllib. urlencode (data2 ))
- Print opener. Open (request). Read ()
- Data2 = [(k, V) for K, V in data. iteritems ()]