2021-01-29

文章目录

  • 前言
  • 一、定义时间戳
  • 二、加密
    • 1.引入库
  • 总结


前言

我开始学爬虫的一个小愿望就是能实现自动打卡健康防疫系统(每天熬夜,早起打卡让我变得不健康了。。。。),昨天学了一下模拟登陆,但还是没成功,今天继续


一、定义时间戳

今天的目标是有道翻译,先重复老步骤
2021-01-29_第1张图片
指定url,headers,这里headers里要包括cookie,user-agent,referer。具体什么时候要加入referer和cookie后续我会添加进来。可以看到,请求的方式不再是get了,而变成了post。因为翻译不同于图片的下载,存在一个动态的请求,所以可以去XHR来搜索我么要的东西。2021-01-29_第2张图片从这张图可以看到,返回类型是json,这里我们将表单数据用data{}括起来。注意!除了变量之外都要用单引号引起来!
‘salt’: ‘16077564766152’ #是时间戳乘以1000然后再取整然(lts)后再加一个随机数 时间戳的概念在我昨天的文章里有介绍
‘i’ 就是我们要查的那个,也是变量

data={
     
    'i': i,
    'from': 'AUTO',
    'to': 'AUTO',
    'smartresult': 'dict',
    'client': 'fanyideskweb',
    'salt': d,
    'sign': sign,  #sign为加密内容
    'lts': str(b),
    'bv': 'cb2ef3d3d7314f7dee099d6b7353a673',
    'doctype': 'json',   #文档类型
    'version': '2.1',
    'keyfrom': 'fanyi.web',
    'action': 'FY_BY_REALTlME'  #对应的值除了变量也加单引号
}

那么根据往前推我们要定义时间戳和i
i=input("要翻译的内容 ")

# 获取时间戳
a=time.time()
b=int(a*1000)  #lts
c=int(random.random()*10)
d=str(b)+str(c)   #salt 根据网页上的数据分析,salt是lts的基础上加一个随机数获得的

二、加密

1.引入库

直接抓包工具按Ctrl+Shift+F,后搜索sign2021-01-29_第3张图片

可以看到这个图,找到有可能是加密方式的那句话点过去加上断点再输入一个单词翻译验证,可以发现加密是这句话:sign: n.md5(“fanyideskweb” + e + i + “Tbh5E8=q6U3EXe+&L[4c@”)
我的是在8376行,然后早找到前面的一部分

var n = function(e, t) {
     
        return e << t | e >>> 32 - t
    }
      , r = function(e, t) {
     
        var n, r, i, a, o;
        return i = 2147483648 & e,
        a = 2147483648 & t,
        n = 1073741824 & e,
        r = 1073741824 & t,
        o = (1073741823 & e) + (1073741823 & t),
        n & r ? 2147483648 ^ o ^ i ^ a : n | r ? 1073741824 & o ? 3221225472 ^ o ^ i ^ a : 1073741824 ^ o ^ i ^ a : o ^ i ^ a
    }
      , i = function(e, t, n) {
     
        return e & t | ~e & n
    }
      , a = function(e, t, n) {
     
        return e & n | t & ~n
    }
      , o = function(e, t, n) {
     
        return e ^ t ^ n
    }
      , s = function(e, t, n) {
     
        return t ^ (e | ~n)
    }
      , l = function(e, t, a, o, s, l, c) {
     
        return e = r(e, r(r(i(t, a, o), s), c)),
        r(n(e, l), t)
    }
      , c = function(e, t, i, o, s, l, c) {
     
        return e = r(e, r(r(a(t, i, o), s), c)),
        r(n(e, l), t)
    }
      , u = function(e, t, i, a, s, l, c) {
     
        return e = r(e, r(r(o(t, i, a), s), c)),
        r(n(e, l), t)
    }
      , d = function(e, t, i, a, o, l, c) {
     
        return e = r(e, r(r(s(t, i, a), o), c)),
        r(n(e, l), t)
    }
      , f = function(e) {
     
        for (var t, n = e.length, r = n + 8, i = 16 * ((r - r % 64) / 64 + 1), a = Array(i - 1), o = 0, s = 0; s < n; )
            o = s % 4 * 8,
            a[t = (s - s % 4) / 4] = a[t] | e.charCodeAt(s) << o,
            s++;
        return t = (s - s % 4) / 4,
        o = s % 4 * 8,
        a[t] = a[t] | 128 << o,
        a[i - 2] = n << 3,
        a[i - 1] = n >>> 29,
        a
    }
      , p = function(e) {
     
        var t, n = "", r = "";
        for (t = 0; t <= 3; t++)
            n += (r = "0" + (e >>> 8 * t & 255).toString(16)).substr(r.length - 2, 2);
        return n
    }
      , h = function(e) {
     
        e = e.replace(/\x0d\x0a/g, "\n");
        for (var t = "", n = 0; n < e.length; n++) {
     
            var r = e.charCodeAt(n);
            if (r < 128)
                t += String.fromCharCode(r);
            else if (r > 127 && r < 2048)
                t += String.fromCharCode(r >> 6 | 192),
                t += String.fromCharCode(63 & r | 128);
            else if (r >= 55296 && r <= 56319) {
     
                if (n + 1 < e.length) {
     
                    var i = e.charCodeAt(n + 1);
                    if (i >= 56320 && i <= 57343) {
     
                        var a = 1024 * (r - 55296) + (i - 56320) + 65536;
                        t += String.fromCharCode(240 | a >> 18 & 7),
                        t += String.fromCharCode(128 | a >> 12 & 63),
                        t += String.fromCharCode(128 | a >> 6 & 63),
                        t += String.fromCharCode(128 | 63 & a),
                        n++
                    }
                }
            } else
                t += String.fromCharCode(r >> 12 | 224),
                t += String.fromCharCode(r >> 6 & 63 | 128),
                t += String.fromCharCode(63 & r | 128)
        }
        return t
    };
 md5=function(e) {
     
            var t, n, i, a, o, s, m, g, v, y = Array();
            for (e = h(e),
            y = f(e),
            s = 1732584193,
            m = 4023233417,
            g = 2562383102,
            v = 271733878,
            t = 0; t < y.length; t += 16)
                n = s,
                i = m,
                a = g,
                o = v,
                s = l(s, m, g, v, y[t + 0], 7, 3614090360),
                v = l(v, s, m, g, y[t + 1], 12, 3905402710),
                g = l(g, v, s, m, y[t + 2], 17, 606105819),
                m = l(m, g, v, s, y[t + 3], 22, 3250441966),
                s = l(s, m, g, v, y[t + 4], 7, 4118548399),
                v = l(v, s, m, g, y[t + 5], 12, 1200080426),
                g = l(g, v, s, m, y[t + 6], 17, 2821735955),
                m = l(m, g, v, s, y[t + 7], 22, 4249261313),
                s = l(s, m, g, v, y[t + 8], 7, 1770035416),
                v = l(v, s, m, g, y[t + 9], 12, 2336552879),
                g = l(g, v, s, m, y[t + 10], 17, 4294925233),
                m = l(m, g, v, s, y[t + 11], 22, 2304563134),
                s = l(s, m, g, v, y[t + 12], 7, 1804603682),
                v = l(v, s, m, g, y[t + 13], 12, 4254626195),
                g = l(g, v, s, m, y[t + 14], 17, 2792965006),
                m = l(m, g, v, s, y[t + 15], 22, 1236535329),
                s = c(s, m, g, v, y[t + 1], 5, 4129170786),
                v = c(v, s, m, g, y[t + 6], 9, 3225465664),
                g = c(g, v, s, m, y[t + 11], 14, 643717713),
                m = c(m, g, v, s, y[t + 0], 20, 3921069994),
                s = c(s, m, g, v, y[t + 5], 5, 3593408605),
                v = c(v, s, m, g, y[t + 10], 9, 38016083),
                g = c(g, v, s, m, y[t + 15], 14, 3634488961),
                m = c(m, g, v, s, y[t + 4], 20, 3889429448),
                s = c(s, m, g, v, y[t + 9], 5, 568446438),
                v = c(v, s, m, g, y[t + 14], 9, 3275163606),
                g = c(g, v, s, m, y[t + 3], 14, 4107603335),
                m = c(m, g, v, s, y[t + 8], 20, 1163531501),
                s = c(s, m, g, v, y[t + 13], 5, 2850285829),
                v = c(v, s, m, g, y[t + 2], 9, 4243563512),
                g = c(g, v, s, m, y[t + 7], 14, 1735328473),
                m = c(m, g, v, s, y[t + 12], 20, 2368359562),
                s = u(s, m, g, v, y[t + 5], 4, 4294588738),
                v = u(v, s, m, g, y[t + 8], 11, 2272392833),
                g = u(g, v, s, m, y[t + 11], 16, 1839030562),
                m = u(m, g, v, s, y[t + 14], 23, 4259657740),
                s = u(s, m, g, v, y[t + 1], 4, 2763975236),
                v = u(v, s, m, g, y[t + 4], 11, 1272893353),
                g = u(g, v, s, m, y[t + 7], 16, 4139469664),
                m = u(m, g, v, s, y[t + 10], 23, 3200236656),
                s = u(s, m, g, v, y[t + 13], 4, 681279174),
                v = u(v, s, m, g, y[t + 0], 11, 3936430074),
                g = u(g, v, s, m, y[t + 3], 16, 3572445317),
                m = u(m, g, v, s, y[t + 6], 23, 76029189),
                s = u(s, m, g, v, y[t + 9], 4, 3654602809),
                v = u(v, s, m, g, y[t + 12], 11, 3873151461),
                g = u(g, v, s, m, y[t + 15], 16, 530742520),
                m = u(m, g, v, s, y[t + 2], 23, 3299628645),
                s = d(s, m, g, v, y[t + 0], 6, 4096336452),
                v = d(v, s, m, g, y[t + 7], 10, 1126891415),
                g = d(g, v, s, m, y[t + 14], 15, 2878612391),
                m = d(m, g, v, s, y[t + 5], 21, 4237533241),
                s = d(s, m, g, v, y[t + 12], 6, 1700485571),
                v = d(v, s, m, g, y[t + 3], 10, 2399980690),
                g = d(g, v, s, m, y[t + 10], 15, 4293915773),
                m = d(m, g, v, s, y[t + 1], 21, 2240044497),
                s = d(s, m, g, v, y[t + 8], 6, 1873313359),
                v = d(v, s, m, g, y[t + 15], 10, 4264355552),
                g = d(g, v, s, m, y[t + 6], 15, 2734768916),
                m = d(m, g, v, s, y[t + 13], 21, 1309151649),
                s = d(s, m, g, v, y[t + 4], 6, 4149444226),
                v = d(v, s, m, g, y[t + 11], 10, 3174756917),
                g = d(g, v, s, m, y[t + 2], 15, 718787259),
                m = d(m, g, v, s, y[t + 9], 21, 3951481745),
                s = r(s, n),
                m = r(m, i),
                g = r(g, a),
                v = r(v, o);
            return (p(s) + p(m) + p(g) + p(v)).toLowerCase()
        }

我们刚刚找到的那句代码到这个代码块的最后一句就是md5的加密方式,而前面的段是它的对象,把这段代码命名为get_sign.js,这段代码要放的应用明天我放在评论区。

import requests
import time 
import random
import execjs
url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'  #使用网页所提供的请求URL
headers={
     
    'Cookie': '[email protected]; OUTFOX_SEARCH_USER_ID_NCOO=302812442.2493042; JSESSIONID=aaadiUSBQLdhe5_m5Rvzx; ___rl__test__cookies=1607756476599',
    'Referer': 'http://fanyi.youdao.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
i=input("要翻译的内容 ")
# 获取时间戳
a=time.time()
b=int(a*1000)  #lts
c=int(random.random()*10)
d=str(b)+str(c)   #salt 根据网页上的数据分析,salt是lts的基础上加一个随机数获得的
# 没被md5加密前的sign
sign_md5="fanyideskweb" + i + d + "Tbh5E8=q6U3EXe+&L[4c@"  #IndentationError: unexpected indent  缩进错误
 #初始化execjs板块
node=execjs.get()
 #打开文件
ctx=node.compile(open('./get_sign.js',encoding='utf-8').read())
 #
funcName = "md5('{0}')".format(sign_md5)
sign=ctx.eval(funcName)
data={
     
    'i': i,
    'from': 'AUTO',
    'to': 'AUTO',
    'smartresult': 'dict',
    'client': 'fanyideskweb',
    'salt': d,
    'sign': sign,  #sign为加密内容
    'lts': str(b),
    'bv': 'cb2ef3d3d7314f7dee099d6b7353a673',
    'doctype': 'json',   #文档类型
    'version': '2.1',
    'keyfrom': 'fanyi.web',
    'action': 'FY_BY_REALTlME'  #对应的值除了变量也加单引号
}
res = requests.post(url=url,headers=headers,data=data).json()

translateResult = res['translateResult'][0][0]['tgt']
print("翻译完成"+translateResult)
print(res)

这是总的代码

总结

越学到后面发现东西越多,不会的越多,不知者无畏,坚持下去!加油!

你可能感兴趣的:(爬虫学习,python)