b**********8 发帖数: 239 | 1 请各位大牛指点,感激不尽
想写一个登录mitbbs 的script by using urllib2 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() | b**********8 发帖数: 239 | 2 请各位大牛指点,感激不尽
想写一个登录mitbbs 的script by using urllib2 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() | f*********s 发帖数: 3525 | | f*********s 发帖数: 3525 | | f*********s 发帖数: 3525 | | f*********s 发帖数: 3525 | |
|