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 | | 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
|
|