python自动登录百度空间

来源:互联网 发布:sqlserver创建用户语句 编辑:程序博客网 时间:2024/04/27 17:05

参考: http://blog.163.com/sunzs20113@126/blog/static/3706880920120105131652/

开发环境:Fedora12 + Python2.6.2

#!/usr/bin/python# coding: GBKimport urllib,urllib2,httplib,cookielibdef auto_login_hi(url,name,pwd):    url_hi="http://passport.baidu.com/?login"    #设置cookie    cookie=cookielib.CookieJar()    cj=urllib2.HTTPCookieProcessor(cookie)    #设置登录参数    postdata=urllib.urlencode({'username':name,'password':pwd})    #生成请求    request=urllib2.Request(url_hi,postdata)    #登录百度    #opener=urllib2.build_opener(request,cj)    opener=urllib2.build_opener(cj)    f=opener.open(request)    #打开百度HI空间页面    hi_html=opener.open(url)    return hi_htmlif __name__=='__main__':    name='zhouciming'    password='xxx'    url='http://hi.baidu.com/zhouciming'    h=auto_login_hi(url,name,password)    print h.read()

运行结果:

[zcm@python #33]$./bai.py<!DOCTYPE html><HTML XMLNS="http://www.w3.org/1999/xhtml" LANG="zh-CN"><HEAD><!--STATUS OK--><META http-equiv="Content-Type" CONTENT="text/html;charset=GBK" /><TITLE>个人主页                </TITLE></HEAD><BODY><script language="javascript">location.href="/zhouciming/home";</script></BODY></HTML>

通过运行结果可以看到,登录成功了。网页自动跳转到 /zhouciming/home

原创粉丝点击