如果模以POST登录网站

如果模以POST登录网站

各位好,我是PYTHON新手,想请问大家一下,如何用python模拟post登录,因为我在抓取数据的过程中,有些数据登录后才能获取数据,特来这里请教一下大家,非常感谢!
# -*- coding: utf-8 -*-
import httplib
import urllib

user="xinzhi"
pwd="xinzhilv"

params=urllib.urlencode({"Loginname":user,"Loginpass":pwd,"firstlogin":1,"option":"登入论坛"})
headers={"Accept":"text/html","User-Agent":"IE","Content-Type":"application/x-www-form-urlencoded"}
website="www.linuxforum.net"
path="/forum/start_page.php"
conn=httplib.HTTPConnection(website)
conn.request("POST",path,params,headers)
r=conn.getresponse()
print r.status,r.reason
data=r.read()
print data
conn.close()
楼上的,好像没人登录成功,我想打开其编辑页面都说没有正常登录
import httplib
import urllib
user="xinzhi"
pwd="xinzhilv"
params=urllib.urlencode({"Loginname":user,"Loginpass":pwd,"JyDownType":1,"option":"登入论坛"})
headers={"Accept":"text/html","User-Agent":"IE","Content-Type":"application/x-www-form-urlencoded"}
website="www.linuxforum.net"
path="/forum/start_page.php"
conn=httplib.HTTPConnection(website)
conn.request("POST",path,params,headers)
r=conn.getresponse()
print r.status,r.reason
data=r.read()
print data
pg=urllib.urlopen("http://www.linuxforum.net/forum/editbasic.php?Cat=")##获取其编辑的相关数据
line=pg.read()
print "======================"
print line
conn.close()
使用urllib2试试
没有设cookie
# -*- coding: utf-8 -*-
import httplib
import urllib

user="xinzhi"
pwd="xinzhilv"


import urllib2,cookielib

cookie=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
urllib2.install_opener(opener)

params=urllib.urlencode({"Loginname":user,"Loginpass":pwd,"firstlogin":1,"option":"登入论坛"})
headers={"Accept":"text/html","User-Agent":"IE","Content-Type":"application/x-www-form-urlencoded"}
website="www.linuxforum.net"
path="/forum/logout.php?Cat="
conn=httplib.HTTPConnection(website)
conn.request("POST",path,params,headers)
r=conn.getresponse()
print r.status,r.reason
data=r.read()
print data

conn.close()

这个是登录cook吗?怎么转接页面?如果你成功了,请告诉我
俺帖一个可以使用的,模拟新浪邮箱登录的。

def post3():   
# for mail.sina.com.cn
    import urllib,urllib2,cookielib
    cj = cookielib.CookieJar()
    url_login = 'http://mail.sina.com.cn/cgi-bin/login.cgi'
    body = (('logintype','login'), ('u','*****'), ('psw', '******'))
    opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    opener.addheaders = [('User-agent', 'Opera/9.23')]
    urllib2.install_opener(opener)
    req=urllib2.Request(url_login,urllib.urlencode(body))
    u=urllib2.urlopen(req)
    print u.read().decode('utf-8').encode('gbk')


QUOTE:
cookie=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))

如果只是这么写的话..
实际没必要import cookielib
直接
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor())
即可.
这是 urllib2 里定义 HTTPCookieProcessor 类的代码:
class HTTPCookieProcessor(BaseHandler):
    def __init__(self, cookiejar=None):
        import cookielib
        if cookiejar is None:
            cookiejar = cookielib.CookieJar()
        self.cookiejar = cookiejar
每个网站不同的

你需要研究下特定网站的 登陆 form

有必要的话可以登陆一次抓包看看

然后再写