Python automatically logs on to the website (processing cookies)

Source: Internet
Author: User
Python automatically logs on to the website (processing cookies)
    Blog type:

  • Python
Python code
  1. Def login ():
  2. Cj = cookielib. cookiejar ()
  3. Opener = urllib2.build _ opener (urllib2.httpcookieprocessor (CJ ))
  4. Login_url = r'http: // zhixing.bjtu.edu.cn/member.php? MoD = Logging & Action = login & loginsubmit = Yes & infloat = Yes & lssubmit = Yes & inajax = 1'
  5. Login_data = urllib. urlencode ({'cookietime': '000000', 'handlekey': 'Ls', 'Password': 'xxx ',
  6. 'Quickforward ': 'yes', 'username': 'digiter '})
  7. Opener. Open (login_url, login_data)
  8. 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
  1. Request = urllib2.request (
  2. Url = r 'HTTP: // zhixing.bjtu.edu.cn/forum.php? MoD = Post & Action = newthread & FID = 601 & extra = & topicsubmit = Yes ',
  3. Data = Param
  4. )
  5. 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

  1. #-*-Coding: UTF-8 -*-
  2. '''''
  3. Created on Dec 24,201 1
  4.  
  5. @ Author: Rush
  6. '''
  7. Import urllib, urllib2, cookielib
  8. Import OS, time
  9. Headers = []
  10. Def login ():
  11. Cj = cookielib. cookiejar ()
  12. Opener = urllib2.build _ opener (urllib2.httpcookieprocessor (CJ ))
  13. Login_url = r'http: // zhixing.bjtu.edu.cn/member.php? MoD = Logging & Action = login & loginsubmit = Yes & infloat = Yes & lssubmit = Yes & inajax = 1'
  14. Login_data = urllib. urlencode ({'cookietime': '000000', 'handlekey': 'Ls', 'Password': 'xxx ',
  15. 'Quickforward ': 'yes', 'username': 'guoyuan '})
  16. Opener. addheaders = [('host', 'zhixing .bjtu.edu.cn '),
  17. ('User-agent', 'mozilla/5.0 (Ubuntu; X11; Linux i686; RV: 8.0) Gecko/20100101 Firefox/8.0 '),
  18. ('Access', 'text/html, application/XHTML + XML, application/XML; q = 0.9, */*; q = 100 '),
  19. ('Accept-color', 'en-US, en; q = 100 '),
  20. ('Accept-encoding', 'gzip, deflate '),
  21. ('Accept-charset', 'iso-8859-1, UTF-8; q = 0.7, *; q = 100 '),
  22. ('Connection', 'keep-alive '),
  23. ('Referer', 'HTTP: // zhixing.bjtu.edu.cn/forum.php'),]
  24. Opener. Open (login_url, login_data)
  25. Return Opener
  26. If _ name _ = '_ main __':
  27. Opener = login ()
  28. Url = r 'HTTP: // zhixing.bjtu.edu.cn/forum.php? MoD = topicadmin & Action = moderate & optgroup = 2 & modsubmit = Yes & infloat = Yes & inajax = 1'
  29. Data = {'fid': '000000', 'formhash': '0cdd1596 ', 'frommodcp': '', 'handlekey': 'mods ',
  30. 'Listexist': 'page % 3d62', 'moderate [] ': '000000', 'operations []': 'type', 'reason ':'...',
  31. 'Redirect': r'http: // zhixing.bjtu.edu.cn/thread-496146-1-1.html', 'typeid': '20140901 '}
  32. Data2 = [(k, V) for K, V in data. iteritems ()]
  33. CNT = 0
  34. For TID in range (493022,496 146 + 1 ):
  35. CNT + = 1
  36. If CNT % 20 = 0: Print
  37. Print tid,
  38. Data2.append ('moderate [] ', STR (TID )))
  39. If CNT % 40 = 0 or CNT = 496146:
  40. Request = urllib2.request (url = URL, Data = urllib. urlencode (data2 ))
  41. Print opener. Open (request). Read ()
  42. Data2 = [(k, V) for K, V in data. iteritems ()]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.