zoukankan      html  css  js  c++  java
  • Python 自动登录网站(处理Cookie)

    http://digiter.iteye.com/blog/1300884
    Python代码  收藏代码
    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': '2592000', 'handlekey': 'ls', 'password': 'xxx',
    6.             'quickforward': 'yes', 'username': 'digiter'})
    7.     opener.open(login_url, login_data)
    8.     return opener
    返回以后只需要设置url和data就可以Post了。 注意不要在request里设置header,这是因为cookie也是header,如果设置header会导致没有cookie,也就没有登录
    Python代码  收藏代码
    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()
    轻松搞定设置Discuz X主题分类的功能(cookie要模拟浏览器,这里是模仿firefox)
    Python代码  收藏代码
    1. # -*- coding: utf-8 -*-
    2. '''''
    3. Created on Dec 24, 2011
    4. @author: rush
    5. '''
    6. import urllib, urllib2, cookielib
    7. import os, time
    8. headers = []
    9. def login():
    10.     cj = cookielib.CookieJar()
    11.     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    12.     login_url = r'http://zhixing.bjtu.edu.cn/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1'
    13.     login_data = urllib.urlencode({'cookietime': '2592000', 'handlekey': 'ls', 'password': 'xxx',
    14.             'quickforward': 'yes', 'username': 'GuoYuan'})
    15.     opener.addheaders = [('Host', 'zhixing.bjtu.edu.cn'),
    16.                        ('User-Agent', 'Mozilla/5.0 (Ubuntu; X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0'),
    17.                        ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
    18.                        ('Accept-Language', 'en-us,en;q=0.5'),
    19.                        ('Accept-Encoding', 'gzip, deflate'),
    20.                        ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
    21.                        ('Connection', 'keep-alive'),
    22.                        ('Referer', 'http://zhixing.bjtu.edu.cn/forum.php'),]
    23.     opener.open(login_url, login_data)
    24.     return opener
    25. if __name__ == '__main__':
    26.     opener = login()
    27.     url = r'http://zhixing.bjtu.edu.cn/forum.php?mod=topicadmin&action=moderate&optgroup=2&modsubmit=yes&infloat=yes&inajax=1'
    28.     data = {'fid': '601', 'formhash': '0cdd1596', 'frommodcp': '', 'handlekey': 'mods',
    29.              'listextra': 'page%3D62', 'moderate[]': '496146', 'operations[]': 'type', 'reason': '...',
    30.              'redirect': r'http://zhixing.bjtu.edu.cn/thread-496146-1-1.html', 'typeid': '779'}
    31.     data2 = [(k, v) for k,v in data.iteritems()]
    32.     cnt = 0
    33.     for tid in range(493022, 496146 + 1):
    34.         cnt += 1
    35.         if cnt % 20 == 0: print
    36.         print tid,
    37.         data2.append(('moderate[]', str(tid)))
    38.         if cnt % 40 == 0 or cnt == 496146:
    39.             request = urllib2.Request(url=url, data=urllib.urlencode(data2))
    40.             print opener.open(request).read()
    41.             data2 = [(k, v) for k,v in data.iteritems()]
  • 相关阅读:
    C# fields Study
    单词1
    C# Base Class Study
    C# Type Parameters Study
    C#3.0学习系列类的成员
    美国式教育
    单词2
    项目添加Struts2支持,并修改默认后缀
    Java多线程的学习和应用
    SQL PIVOT
  • 原文地址:https://www.cnblogs.com/adodo1/p/4327580.html
Copyright © 2011-2022 走看看