美文网首页码农的世界
都说微博数据是最有价值的,那么我今天实战python模拟登录微博

都说微博数据是最有价值的,那么我今天实战python模拟登录微博

作者: b4a0155c6514 | 来源:发表于2019-01-07 14:09 被阅读0次

    今天想做一个微博爬个人页面的工具,满足一些不可告人的秘密。那么首先就要做那件必做之事!模拟登陆……

    都说微博数据是最有价值的,那么我今天实战python模拟登录微博!

    PC 登录新浪微博时, 在客户端用JS预先对用户名、密码都进行了加密,而且在POST之前会GET 一组参数,这也将作为POST_DATA 的一部分。这样,就不能用通常的那种简单方法来模拟POST 登录( 比如人人网 )。

    都说微博数据是最有价值的,那么我今天实战python模拟登录微博!

    在提交POST请求之前,需要GET 获取两个参数。地址是:http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.18)

    得到的数据中有 servertime 和 nonce 的值, 是随机的,其他值貌似没什么用。

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">def get_servertime():
    url = 'http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=dW5kZWZpbmVk&client=ssologin.js(v1.3.18)&_=1329806375939'

    返回出来的是一个Response对象,无法直接获取,text后,可以通过正则匹配到

    大概长这样子的:sinaSSOController.preloginCallBack({"retcode":0,"servertime":1545606770, ...})

    data = requests.request('GET', url).text
    p = re.compile('((.*))')
    try:
    json_data = p.search(data).group(1)
    data = json.loads(json_data)
    servertime = str(data['servertime'])
    nonce = data['nonce']
    return servertime, nonce
    except:
    print('获取 severtime 失败!')
    return None
    </pre>

    都说微博数据是最有价值的,那么我今天实战python模拟登录微博!

    通过 httpfox 观察 POST 的数据,参数较复杂,其中“su" 是加密后的username, sp 是加密后的password,servertime 和 nonce 是上一步得到的,其他参数是不变的。

    username 经过了 BASE64 计算:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">username = base64.encodestring( urllib.quote(username) )[:-1]
    </pre>

    password 经过了三次SHA1 加密,且其中加入了 servertime 和 nonce 的值来干扰。即:两次SHA1加密后,将结果加上 servertime 和 nonce 的值,再SHA1 算一次。

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">def get_pwd(pwd, servertime, nonce):

    第一次计算,注意Python3 的加密需要encode,使用bytes

    pwd1 = hashlib.sha1(pwd.encode()).hexdigest()

    使用pwd1的结果在计算第二次

    pwd2 = hashlib.sha1(pwd1.encode()).hexdigest()

    使用第二次的结果再加上之前计算好的servertime和nonce值,hash一次

    pwd3_ = pwd2 + servertime + nonce
    pwd3 = hashlib.sha1(pwd3_.encode()).hexdigest()
    return pwd3
    def get_user(username):

    将@符号转换成url中能够识别的字符

    _username = urllib.request.quote(username)

    Python3中的base64计算也是要字节

    base64出来后,最后有一个换行符,所以用了切片去了最后一个字符

    username = base64.encodebytes(_username.encode())[:-1]
    return username
    </pre>

    都说微博数据是最有价值的,那么我今天实战python模拟登录微博!

    将参数组织好,POST请求。这之后还没有登录成功,POST后得到的内容中包含一句:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">location.replace("http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack&retcode=101&reason=%B5%C7%C2%BC%C3%FB%BB%F2%C3%DC%C2%EB%B4%ED%CE%F3")
    </pre>

    这是登录失败时的结果,登录成功后结果与之类似,不过 retcode 的值是0。接下来再请求这个URL,这样就成功登录到微博了。记得要提前build 缓存。

    相关文章

      网友评论

        本文标题:都说微博数据是最有价值的,那么我今天实战python模拟登录微博

        本文链接:https://www.haomeiwen.com/subject/whdurqtx.html