爬虫逆向,瑞数6,补环境,国家专利

本案例仅供学习和参考,禁止商业用途哈

1,清空cookie

2,打开事件监听中脚本监听

3,刷新页面,就会看到这个代码,这个是解密前的代码,也就是加密代码,我们需要这个拿下来

拿下来放这

点击F8后就会看到这个代码,这个是解密后的代码,这个代码是可以拿到动态变化的cookie值的

拿到放这里

然后把代码框架搭建好,准备补环境,插入补环境的脚本

4,根据报错信息将环境补齐,补环境的时候需要根据这个页面的结构

最后代码是这样的

javascript 复制代码
window = global
top = self = window
window.ActiveXObject = undefined
window.addEventListener = function(){};
window.attachEvent = undefined;
div = {
    getElementsByTagName:function(tag_name){
        console.log("div getElementsByTagName ->",tag_name)
        if(tag_name === "i"){
            return []
        }
    }
}
head = {
    removeChild:function(child){
        console.log("head removeChild ->",child)
    }
}
script = {
    getAttribute:function(attr){
        console.log("script getAttribute ->", attr)
        if (attr === "r"){
            return "m"
        }
    },
    parentElement:head
}
document = {
    createElement:function(tag_name){
        console.log("document createElement ->",tag_name)
        if(tag_name === "div"){
            return  div
        }
        if(tag_name === 'a'){
            return []
        }
    },
    getElementsByTagName:function(tag_name){
        console.log("document getElementsByTagName ->",tag_name)
        if(tag_name === 'script'){
            return [script,script,script,script,script,script]
        }
        if(tag_name === 'base'){
            return []
        }
    },
    getElementById:function(ele_id){
        console.log("document getElementById ->",ele_id)
        if(ele_id === 'a'){
            return []
        }
    }
}
location = {
    "ancestorOrigins": {},
    "href": "http://epub.cnipa.gov.cn/",
    "origin": "http://epub.cnipa.gov.cn",
    "protocol": "http:",
    "host": "epub.cnipa.gov.cn",
    "hostname": "epub.cnipa.gov.cn",
    "port": "",
    "pathname": "/",
    "search": "",
    "hash": ""
}
navigator = {
    userAgent:'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36',
    platform:'Win32'
}
setInterval = function(){}
setImmediate = function(){}
'encrypt_js_code'; // 动态变化的,需要被替换
'decrypt_js_run_code'; // 动态变化的,需要进行替换
function get_cookie(){
    return document.cookie;
}
console.log(get_cookie())
python 复制代码
import requests
import subprocess
import urllib.parse
from lxml import etree

from functools import partial

subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')
import execjs

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36',
}

# 网站地址最后不能加斜杠, 之后会通过这个地址拼接外链的js地址
url = 'http://epub.cnipa.gov.cn'
session = requests.session()
response = session.get(url, headers=headers, verify=False)
response.encoding = 'utf-8'
html = etree.HTML(response.text)
meta_content = html.xpath('//meta/@content')[-1]
print(meta_content)
encrypt_js_code = html.xpath('//script/text()')[0]
decrypt_js_code_url = url + html.xpath('//script[2]/@src')[0]
decrypt_js_code = session.get(decrypt_js_code_url, headers=headers, verify=False).text

with open('国家专利.js', 'r', encoding='utf-8') as f:
    js_code = f.read()
    js_code = js_code.replace('meta_content', meta_content).replace("'encrypt_js_code'", encrypt_js_code).replace(
        "'decrypt_js_run_code'", decrypt_js_code)

ctx = execjs.compile(js_code)
cookie_t = ctx.call('get_cookie').split(';')[0].split('=')
print(cookie_t)
session.cookies.update({cookie_t[0]: cookie_t[1]})
response = session.get(url, headers=headers, verify=False)
print(response)
相关推荐
小兔崽子去哪了2 小时前
机器学习,KNN 算法
后端·python·机器学习
laocooon5238578862 小时前
相对名次算法的处理python
开发语言·python·算法
星火开发设计2 小时前
Python冒泡排序详解:从原理到代码实现与优化
开发语言·笔记·python·开源·排序算法·课程设计
小智RE0-走在路上2 小时前
Python学习笔记(9) --文件操作
笔记·python·学习
愈努力俞幸运2 小时前
Python heapq (堆/优先队列)
python
SHolmes18542 小时前
给定某日的上班时间段,计算当日的工作时间总时长(Python)
开发语言·前端·python
掘金安东尼2 小时前
顶层元素问题:popover vs. dialog
前端·javascript·面试
C嘎嘎嵌入式开发2 小时前
NLP 入门:从原理到实战的个人经验总结
人工智能·python·自然语言处理·nlp
咖啡の猫2 小时前
Python字典元素的增、删、改操作
java·开发语言·python