python3.3教程之模拟百度登陆

模拟百度的登陆,开发环境使用了python3。

代码:
#-*-coding:utf-8-*-
'''
Created on 2014年1月10日
@author: hhdys
@edit: www.jbxue.com
'''
import urllib.request,http.cookiejar,re
class Baidu:
def login(self):
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36')]
resp=opener.open('http://weigou.baidu.com/')
for c in cj:
print(c.name,"====",c.value)
getapiUrl = "https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true"
resp2=opener.open(getapiUrl)
getapiRespHtml = resp2.read().decode("utf-8")
foundTokenVal = re.search("bdPass\.api\.params\.login_token='(?P<tokenVal>\w+)';", getapiRespHtml)
if foundTokenVal :
tokenVal = foundTokenVal.group("tokenVal")
print(tokenVal)

staticpage = "http://zhixin.baidu.com/Jump/index?module=onesite"
baiduMainLoginUrl = "https://passport.baidu.com/v2/api/?login"
postDict = {
'charset':"utf-8",
'token':tokenVal,
'isPhone':"false",
'index':"0",
'staticpage': staticpage,
'loginType': "1",
'tpl': "mn",
'callback': "parent.bd__pcbs__n1a3bg",
'username':"*****", #用户名
'password':"*****", #密码
'mem_pass':"on",
"apiver":"v3",
"logintype":"basicLogin"
}
postData = urllib.parse.urlencode(postDict);
postData = postData.encode('utf-8')
resp3=opener.open(baiduMainLoginUrl,data=postData)
for c in cj:
print(c.name,"="*6,c.value)


if __name__=="__main__":
print("="*10,"开始")
bd=Baidu()
bd.login()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值