python3.3教程之模拟百度登陆代码分享

yipeiwu_com5年前Python基础

复制代码 代码如下:

#-*-coding:utf-8-*-
'''
Created on 2014年1月10日

@author: hhdys
'''
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()

相关文章

Python实现list反转实例汇总

本文实例总结了Python实现list反转的方法。分享给大家供大家参考。具体实现方法如下: 下面有几个不同实现的函数 复制代码 代码如下:import math   def r...

python动态参数用法实例分析

本文实例讲述了python动态参数用法。分享给大家供大家参考。具体分析如下: 先来看一段代码: class Person: def __init__(self,*pros,*...

解决python升级引起的pip执行错误的问题

centos6.x默认安装的python为2.6版本,今天换成了3.5版本 这里不再讲如何升级python版本 在安装完新的版本后,之前安装的插件都不能使用了,再用pip进行安装提示已经...

pytorch实现用CNN和LSTM对文本进行分类方式

model.py: #!/usr/bin/python # -*- coding: utf-8 -*- import torch from torch import nn imp...

TensorFlow tf.nn.max_pool实现池化操作方式

TensorFlow tf.nn.max_pool实现池化操作方式

max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似 有些地方可以从卷积去参考【TensorFlow】 tf.nn.conv2d实现卷积的方式 tf.nn.max_p...