Python 自动登录网站(处理Cookie)
2024-08-31 16:15:42
http://digiter.iteye.com/blog/1300884
Python代码
- 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': '2592000', 'handlekey': 'ls', 'password': 'xxx',
- 'quickforward': 'yes', 'username': 'digiter'})
- opener.open(login_url, login_data)
- return opener
返回以后只需要设置url和data就可以Post了。
注意不要在request里设置header,这是因为cookie也是header,如果设置header会导致没有cookie,也就没有登录
Python代码
- 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()
轻松搞定设置Discuz X主题分类的功能(cookie要模拟浏览器,这里是模仿firefox)
Python代码
- # -*- coding: utf-8 -*-
- '''''
- Created on Dec 24, 2011
- @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': '2592000', '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'),
- ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
- ('Accept-Language', 'en-us,en;q=0.5'),
- ('Accept-Encoding', 'gzip, deflate'),
- ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
- ('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': '601', 'formhash': '0cdd1596', 'frommodcp': '', 'handlekey': 'mods',
- 'listextra': 'page%3D62', 'moderate[]': '496146', 'operations[]': 'type', 'reason': '...',
- 'redirect': r'http://zhixing.bjtu.edu.cn/thread-496146-1-1.html', 'typeid': '779'}
- data2 = [(k, v) for k,v in data.iteritems()]
- cnt = 0
- for tid in range(493022, 496146 + 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()]
最新文章
- Nop中的Cache浅析
- java调用sqlldr导入csv文件数据到临时表
- sql 默认id为1000
- About SCCM 2012 UDA(User Device Affinity)
- 今天发现之前瑞乐做的登录和注册居然都是用的get请求,瞬间出了一身冷汗.
- Mac下导出chrome插件
- spark HA
- 清幽傲竹实现kbmMWServer的方法(转)
- CentOS隐藏nginx版本号
- NET平台下的Excel编程|C#操作Excel|Application和ApplicationClass的联系和区别
- Oracle判断语句集合(转载)
- Dice Notation(模拟)
- 基于Spring提供支持不同设备的页面
- 编程算法 - 数字数组中只出现一次 代码(C)
- Mixin模式:带实现的协议
- HTML indexedDB数据库—简单示例
- JavaScript实现省市联动
- 深度解密Go语言之关于 interface 的10个问题
- vue 3.0
- PHP函数memory_get_usage获取PHP内存清耗量
热门文章
- .NET开发时让人头痛的SESSION超时
- socket编程了解
- Python封装与隐藏
- [error:没有解析库]Couldn't find a tree builder with the features you requested: xml. Do you need to install a parser library?
- 连通图 poj2186 最受欢迎的牛(求最受欢迎的牛的数量)
- 【BZOJ2286】消耗战(虚树,DFS序,树形DP)
- hdu 4937 2014 Multi-University Training Contest 7 1003
- 捕获错误并发邮件 register_shutdown_function
- 计算机图形——OpenGL
- CodeForces 380.C Sereja and Brackets