python自动登录百度空间
生活随笔
收集整理的這篇文章主要介紹了
python自动登录百度空间
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
參考:?http://blog.163.com/sunzs20113@126/blog/static/3706880920120105131652/
開發(fā)環(huán)境: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"#設(shè)置cookiecookie=cookielib.CookieJar()cj=urllib2.HTTPCookieProcessor(cookie)#設(shè)置登錄參數(shù)postdata=urllib.urlencode({'username':name,'password':pwd})#生成請(qǐng)求request=urllib2.Request(url_hi,postdata)#登錄百度#opener=urllib2.build_opener(request,cj)opener=urllib2.build_opener(cj)f=opener.open(request)#打開百度HI空間頁(yè)面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()運(yùn)行結(jié)果: [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>個(gè)人主頁(yè) </TITLE> </HEAD> <BODY> <script language="javascript"> location.href="/zhouciming/home"; </script> </BODY> </HTML>
通過(guò)運(yùn)行結(jié)果可以看到,登錄成功了。網(wǎng)頁(yè)自動(dòng)跳轉(zhuǎn)到?/zhouciming/home
總結(jié)
以上是生活随笔為你收集整理的python自动登录百度空间的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 5.1 入门整合案例(SpringBoo
- 下一篇: [Windows]python+PyQT