破解js加密--python execjs库 执行js代码

import execjs
import re
import requests
import time

#测试url
url = 'http://gaj.chifeng.gov.cn/default.php?mod=article&fid=230&s63642044_start=0'

session = requests.session()  #存储cookie
response = session.get(url)  #第一次发起请求
print(response.text)  #得到的html文本及js加密代码
text = response.text

f_js = re.findall("javascript\">(.*?)</script>", text)[0]
f_js = f_js.replace('screen.width','500').replace('screen.height','500').replace('window.location.href','"'+url+'"').replace('document.','').replace('self.location =',' return ')  #字符串替换

ctx = execjs.compile(f_js)  #编译
location = ctx.call("YunSuoAutoJump")  #YunSuoAutoJump 要执行的js函数
second_url = "http://gaj.chifeng.gov.cn/" + location  #验证后的url
time.sleep(0.2)

_ = session.get(second_url)  #第二次发起请求,携带cookie信息
response = session.get(url)  #第二次发起请求,得到你想要的结果
print(response.text)

第一次请求后的结果

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate, post-check=0, pre-check=0"/><meta http-equiv="Connection" content="Close"/><script type="text/javascript">function stringToHex(str){var val="";for(var i = 0; i < str.length; i++){if(val == "")val = str.charCodeAt(i).toString(16);else val += str.charCodeAt(i).toString(16);}return val;}function YunSuoAutoJump(){ var width =screen.width; var height=screen.height; var screendate = width + "," + height;var curlocation = window.location.href;if(-1 == curlocation.indexOf("security_verify_")){ document.cookie="srcurl=" + stringToHex(window.location.href) + ";path=/;";}self.location = "/default.php?mod=article&security_verify_data=" + stringToHex(screendate);}</script><script>setTimeout("YunSuoAutoJump()", 50);</script></head><!--2019-08-21 18:55:50--></html>

验证后的URL [second_url]

http://gaj.chifeng.gov.cn//default.php?mod=article&fid=230&s63642044_start=0&security_verify_data=3530302c353030

第三次请求才是我们想要的结果了!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值