欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)自動登錄百度空間的方法

 更新時間:2017年06月10日 11:21:57   作者:JoeBlackzqq  
這篇文章主要介紹了Python實現(xiàn)自動登錄百度空間的方法,涉及Python的http請求發(fā)送、獲取響應、cookie操作等相關技巧,需要的朋友可以參考下

本文實例講述了Python實現(xiàn)自動登錄百度空間的方法。分享給大家供大家參考,具體如下:

開發(fā)環(huán)境:Fedora12 + Python2.6.2

#!/usr/bin/python
# coding: GBK
import urllib,urllib2,httplib,cookielib
def auto_login_hi(url,name,pwd):
  url_hi="http://passport.baidu.com/?login"
  #設置cookie
  cookie=cookielib.CookieJar()
  cj=urllib2.HTTPCookieProcessor(cookie)
  #設置登錄參數(shù)
  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_html
if __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" rel="external nofollow" ;
</script>
</BODY>
</HTML>

通過運行結果可以看到,登錄成功了。網頁自動跳轉到 /zhouciming/home

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python Socket編程技巧總結》、《Python URL操作技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程

希望本文所述對大家Python程序設計有所幫助。

相關文章

最新評論