博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python爬虫入门教程 62-100 30岁了,想找点文献提高自己,还被反爬了,Python搞起,反爬第2篇...
阅读量:6856 次
发布时间:2019-06-26

本文共 4161 字,大约阅读时间需要 13 分钟。

学术搜索

学习理论的知识少不了去检索文献,好多文献为你的实操提供了合理的支撑,我所在的大学内网默认是有知网账户的,非常NICE

今天要完成的网站是

Google学术搜索是一个文献检索服务,目前主要是提供维普资讯、万方数据等几个学术文献资源库的检索服务。通过Google学术搜索只能够查找到这些学术资料的“报告、摘要及引用内容... 来源百度百科

我们的目标

获取现在访问的链接地址,当你使用谷歌浏览器的开发者工具抓取的时候,得到的是一个js加密函数

注意看上图2的位置,接下来,我们采用上篇博客的方式,去尝试获取visit函数的具体内容

我们要在所有的请求链接中去检索一个visit方法,注意步骤

双击方法名,进入

找到核心方法

function visit(url) {    var newTab = window.open('about:blank');       if(Gword!='') url = strdecode(url);   // var newTab = window.open(url);       newTab.location.href = url;    //newTab.location.reload(true);}复制代码

发现url在跳转前调用了一个strdecode函数,你只需要关注这个函数的实现就可以了

再次查看visit的调用函数,找到参数的生成方式为

onclick="visit(\'' + autourl[b] + '\')"  复制代码

autourl[b] 我们是可以直接用爬虫在HTML页面获取到的

function auto(b) {    t = (tim - ts[b]) / 100;    tt = t.toString().split('.');    if(tt.length==1) t = t.toString() + '.00';    else if(tt[1].length < 2)  t = t.toString() + '0';    if (t > 4) document.getElementById("txt" + b).innerHTML = '连接超时!<\/font>';    else document.getElementById("txt" + b).innerHTML = 'takes ' + t + 's.    现在访问 <\/a>'}function visit(url) {    var newTab = window.open('about:blank');       if(Gword!='') url = strdecode(url);   // var newTab = window.open(url);       newTab.location.href = url;    //newTab.location.reload(true);}复制代码

参数分析

if(Gword!='') url = strdecode(url); 如果Gword为空,调用的是strdecode方法,查阅之后,发现相关代码也在下面

Gword 在上面的一张图片中我们也已经获取到了,可以向上看

strdecode函数分析

  1. 进行base64编码
  2. 通过Gword生成一个key
  3. 计算key的len
  4. 循环string然后将code生成,这个地方注意js里面的fromCharCode函数(Python里面的chr)和charCodeAt函数(Python里面的ord)
//codefunction strdecode(string) {    string = base64decode(string);    key = Gword+'ok ';    len = key.length;    code = '';    for (i = 0; i < string.length; i++) {        var k = i % len;        code += String.fromCharCode(string.charCodeAt(i) ^ key.charCodeAt(k))    }    return base64decode(code)}var base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);function base64decode(str) {    var c1, c2, c3, c4;    var i, len, out;    len = str.length;    i = 0;    out = "";    while (i < len) {        do {            c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff]        } while (i < len && c1 == -1);        if (c1 == -1) break;        do {            c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff]        } while (i < len && c2 == -1);        if (c2 == -1) break;        out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));        do {            c3 = str.charCodeAt(i++) & 0xff;            if (c3 == 61) return out;            c3 = base64DecodeChars[c3]        } while (i < len && c3 == -1);        if (c3 == -1) break;        out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));        do {            c4 = str.charCodeAt(i++) & 0xff;            if (c4 == 61) return out;            c4 = base64DecodeChars[c4]        } while (i < len && c4 == -1);        if (c4 == -1) break;        out += String.fromCharCode(((c3 & 0x03) << 6) | c4)    }    return out}复制代码

这个地方有2个解决方案了

  • 1是用Python重写编写相关逻辑
  • 2通过Python调用JS直接实现

我们采用方案2 将 base64decode 复制到一个文件中,然后通过execjs进行调用

Python 执行JS库 execjs

execjs可以在python中运行javascript代码

官网:

安装:pip install PyExecJS

可以切换清华源

安装成功之后在pycharm中引入一下,不出错误,表示运行成功

我们对JS进行编译

import execjswith open('scmor.js', 'r', encoding='utf-8') as f:    js = f.read()    ctx = execjs.compile(js)  # 对JS进行编译复制代码

核心的方法

def decode(string):    string = ctx.call('base64decode', string)  # base64解码string参数,string参数上面获取到的autourls里面的值    key = " link@scmor.comok "  # Gword的值+ 'ok '   key 在HTML页面中可以获取到    Len = len(key)  # Gword长度    code = ''    for i in range(0, len(string)):        k = i % Len        n = ord(str(string[i])) ^ ord(str(key[k]))        code += chr(n)    return ctx.call('base64decode', code)复制代码

运行结果展示

完整代码下载

关注微信公众账号:非本科程序员,回复0402获取下载地址

转载地址:http://emiyl.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
克隆虚拟机
查看>>
第 4 章 容器 - 029 - 限制容器的 Block IO
查看>>
oracle中的union与union all总结
查看>>
001-mini linux
查看>>
java之动态代理
查看>>
关于HTML5你必须知道的28个新特性,新技巧以及新技
查看>>
你必须知道的.net
查看>>
PHP检测url是否被百度收录
查看>>
squid代理服务器
查看>>
4.10/4.11/4.12 lvm讲解 4.13 磁盘故障小案例
查看>>
扩展的几个应用 、 vim编辑技巧 、 发布网络YUM源 、 源码编译安装
查看>>
Java的新项目学成在线笔记-day2(四)
查看>>
U大师安装系统后,Chrome主页被7654导航劫持解决方法
查看>>
python气象分析
查看>>
Ansible服务
查看>>
配置MSTP及负载均衡
查看>>
高可用高并发的 9 种技术架构!
查看>>