由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请各位大牛指点,感激不尽 : Python Script 关于登录 mitbbs 的问题
相关主题
有自信能写出单机1M/s tcp req/response可以扔个简历过来请教一个query CGI 的问题
同主题转寄 (转载)error while building gcc
gmail用啥语言写的?https://mail.google.com/mail/u/0/?shva=1#inbox解析一下。如何GDB调试因pthread_cond_wait()阻塞的线程? (转载)
javascript问题 双黄包求助运行时间比较
一个网页点击link和copy link address再打开得到不同结果Cannot use my own container as the underlying container of a stack? (c++)
快要被Python的兼容性弄疯了Interview question: is the following code OK?
怎么用python download 网上的数据?g++编译了不该过的C++代码
Python 自动登录问题菜鸟求助:怎么自动include header file?
相关话题的讨论汇总
话题: password话题: headers话题: username话题: urllib话题: req
进入Programming版参与讨论
1 (共1页)
b**********8
发帖数: 239
1
请各位大牛指点,感激不尽
想写一个登录mitbbs 的script with following code but failed.
( if trying with mechanize package , I can login successfully)
Is there anything wrong with the function ?
Code:
import urllib, urllib2, cookielib
username = 'username'
password = 'password'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
body = (('id', username),
('password', password),
)
headers={
'User-agent':'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13)
Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13',
'Referer':'http://www.mitbbs.com/newindex/login.php'
}
req=urllib2.Request(
url='http://www.mitbbs.com/mitbbs_login.php',
data=urllib.urlencode(body),
headers=headers
)
opener.open(req)
#open link for mailbox
resp = opener.open('http://www.mitbbs.com/mitbbs_mailbox.php?option=receive&path=r')
print resp.read()
l********a
发帖数: 1154
2
看你也是firefox,怎么没弄个httpfox插件看header呢?
你的代码有2个问题:
1. 提交的时候,密码对应的字段不是password,而是passwd,用httpfox能看到的
2. 登陆页面的request地址有误,不是http://www.mitbbs.com/mitbbs_login.php,而是http://www.mitbbs.com/newindex/mitbbs_bbslogin.php.也是httpfox看的.
下面附上调试通过的代码
import urllib, urllib2, cookielib
username = 'username'
password = 'password'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
body = (('id', username),
('passwd', password),
)
headers={
'User-agent':'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13)
Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13',
'Referer':'http://www.mitbbs.com/newindex/login.php'
}
req=urllib2.Request(
url='http://www.mitbbs.com/newindex/mitbbs_bbslogin.php',
data=urllib.urlencode(body),
headers=headers
)
opener.open(req)
#open link for mailbox
resp = opener.open('http://www.mitbbs.com/mitbbs_mailbox.php?option=receive&path=r')
print resp.read()
b**********8
发帖数: 239
3
多谢高人指点.The updated script works fine under Python 2.7. But it will
still fail if runing it under Python 3.2 after converting. (I guess it
might be related to different handling for header info of request in 3.2.
But I am not sure). Is there anything wrong with following script ?
多谢了!

Code :
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.
error, urllib.parse, http.cookiejar
#from bs4 import *
username = 'id'
password = 'passwd'
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
body = (('id', username),
('passwd', password),
)
headers={
'User-agent':'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13)
Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13',
'Referer':'http://www.mitbbs.com/newindex/login.php'
}
url='http://www.mitbbs.com/newindex/mitbbs_bbslogin.php'
data=urllib.parse.urlencode(body)
req=urllib.request.Request(url,data.encode('utf_8'))
req.add_header('User-agent', r'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.
9.2.13)Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13')
req.add_header('Referer', 'http://www.mitbbs.com/newindex/login.php')
opener.open(req)
#open link for mailbox
resp = opener.open('http://www.mitbbs.com/mitbbs_mailbox.php?option=receive&path=r')
#print(BeautifulSoup(resp.read()))
print(resp.read())

【在 l********a 的大作中提到】
: 看你也是firefox,怎么没弄个httpfox插件看header呢?
: 你的代码有2个问题:
: 1. 提交的时候,密码对应的字段不是password,而是passwd,用httpfox能看到的
: 2. 登陆页面的request地址有误,不是http://www.mitbbs.com/mitbbs_login.php,而是http://www.mitbbs.com/newindex/mitbbs_bbslogin.php.也是httpfox看的.
: 下面附上调试通过的代码
: import urllib, urllib2, cookielib
: username = 'username'
: password = 'password'
: cj = cookielib.CookieJar()
: opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

l********a
发帖数: 1154
4
报错是什么?全部贴上来
b**********8
发帖数: 239
5
Error received:
Traceback (most recent call last):
File "C:\EclipseWorkspaces\csse120\FleaSearch\src\MitMailbox.py", line 22,
in
resp=opener.open(req)
File "C:\Python32\lib\urllib\request.py", line 375, in open
response = meth(req, response)
File "C:\Python32\lib\urllib\request.py", line 487, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python32\lib\urllib\request.py", line 407, in error
result = self._call_chain(*args)
File "C:\Python32\lib\urllib\request.py", line 347, in _call_chain
result = func(*args)
File "C:\Python32\lib\urllib\request.py", line 560, in http_error_302
headers, fp)
urllib.error.HTTPError: HTTP Error 302: Found - Redirection to url '/
newindex/kjjy.php' is not allowed
Here is a similar error reported from stackoverflow(but no solution yet)
http://stackoverflow.com/questions/6805584/python-3-2-dealing-w

【在 l********a 的大作中提到】
: 报错是什么?全部贴上来
p**o
发帖数: 3409
6
py27能跑就继续用嘛,干嘛非得在py3下跑'
话说这import语句看得真蛋疼

【在 b**********8 的大作中提到】
: 多谢高人指点.The updated script works fine under Python 2.7. But it will
: still fail if runing it under Python 3.2 after converting. (I guess it
: might be related to different handling for header info of request in 3.2.
: But I am not sure). Is there anything wrong with following script ?
: 多谢了!
:
: Code :
: #!/usr/bin/env python
: # -*- coding: UTF-8 -*-
: import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.

l********a
发帖数: 1154
7
urllib.error.HTTPError: HTTP Error 302: Found - Redirection to url '/
newindex/kjjy.php' is not allowed
这个错误应该跟cookie有关,自动转向的时候cookie没有传递过去
不确定是否是那py3.x的问题
b**********8
发帖数: 239
8
我想跑一下这个script 在一台装了最新的python机器上. 看着这么多import 确实闹的
慌, 呵呵

【在 p**o 的大作中提到】
: py27能跑就继续用嘛,干嘛非得在py3下跑'
: 话说这import语句看得真蛋疼

b**********8
发帖数: 239
9
又有一个问题, 请大牛指点:
有时登录失败, 会出现一个 javascript pop window with the following message:
你登录的窗口过多,是否踢出多余的窗口?
点确定后,成功登录
如果这样,是否可以点确定 from python script?

【在 b**********8 的大作中提到】
: 我想跑一下这个script 在一台装了最新的python机器上. 看着这么多import 确实闹的
: 慌, 呵呵

i**********e
发帖数: 1145
10
你可以检查一下当 javascript popup 弹出来的时候,你点 OK 之后浏览器所 post 的
data.
事实就是加上这个 kick_multi=1 一起提交上去就好了。
data = [
('id', id),
('passwd', passwd),
('kick_multi', '1')] # confirm login with multi_session.
不久前写了个 python script 自动登录 mitbbs 查俱乐部的从未发表过文章的用户信
息。你可以参考参考:
http://ideone.com/M13Dv

【在 b**********8 的大作中提到】
: 又有一个问题, 请大牛指点:
: 有时登录失败, 会出现一个 javascript pop window with the following message:
: 你登录的窗口过多,是否踢出多余的窗口?
: 点确定后,成功登录
: 如果这样,是否可以点确定 from python script?

b**********8
发帖数: 239
11
Many Thanks . I will try it .

【在 i**********e 的大作中提到】
: 你可以检查一下当 javascript popup 弹出来的时候,你点 OK 之后浏览器所 post 的
: data.
: 事实就是加上这个 kick_multi=1 一起提交上去就好了。
: data = [
: ('id', id),
: ('passwd', passwd),
: ('kick_multi', '1')] # confirm login with multi_session.
: 不久前写了个 python script 自动登录 mitbbs 查俱乐部的从未发表过文章的用户信
: 息。你可以参考参考:
: http://ideone.com/M13Dv

1 (共1页)
进入Programming版参与讨论
相关主题
菜鸟求助:怎么自动include header file?一个网页点击link和copy link address再打开得到不同结果
url header问题快要被Python的兼容性弄疯了
ask a question about compile shared library using libtool怎么用python download 网上的数据?
which header file declares ntohs() in c++ ?Python 自动登录问题
有自信能写出单机1M/s tcp req/response可以扔个简历过来请教一个query CGI 的问题
同主题转寄 (转载)error while building gcc
gmail用啥语言写的?https://mail.google.com/mail/u/0/?shva=1#inbox解析一下。如何GDB调试因pthread_cond_wait()阻塞的线程? (转载)
javascript问题 双黄包求助运行时间比较
相关话题的讨论汇总
话题: password话题: headers话题: username话题: urllib话题: req