最新今日头条详情页加密参数 __ac_nonce和__ac_signature 解决方案

声明:给你face不要,还要过来说我抄袭~~ 渣渣
踩坑:就算参数得到正常访问但是量一大就会抛出broke包,导致整个ip段无法访问,解决方案当然是加代理ip,但并不要使用短效自动切的代理,比如几秒切换的,因为我们得到的__ac_nonce和请求详情的ip必须保持在同一个IP才能获取,猜测__ac_nonce是一段保存了ip的加密参,好了进入主题。

我这里只举个列子,其他的替换:

import requests

url = "https://www.toutiao.com/a6824014300391145991/"

payload = {}
headers = {
    'Host': 'www.toutiao.com',
    # 'Cookie': 'ttcid=b049570fb2b24552b3e54a1a32717eaa18; SLARDAR_WEB_ID=9ab4174c-6188-4018-93ac-c417d3cb2b9a; WEATHER_CITY=%E5%8C%97%E4%BA%AC; csrftoken=db8eb2d6e3b134d7b70c2f41840b02a7; _ga=GA1.2.958109132.1592572103; s_v_web_id=verify_kbpu1aw7_K511yd6l_hh0a_47AN_9MFY_nHx4GC3dymt3; _gid=GA1.2.1847371657.1592795246; tt_webid=6841046011621737998; tt_webid=6841046011621737998; __tasessionId=pw1cr5eg81592809015053;__ac_nonce=05ef0593e00e64616a1f;__ac_signature=XLyE-AAgEBCa6z11sAaZyly.heAAAJNW8GnYMkOtHTehnZBpsIq32os.NVyt29Y23z8..DD.7JPJmRySg9akUtPt8jfnuKciZ63WG0PaEOXgTDFkdkTWfCDkERAghsmuxnk',
    'cache-control': 'max-age=0',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36',
    'referer': 'https://www.toutiao.com/a6826538503917011470/',
    'accept-language': 'zh-CN,zh;q=0.9'
}


s = requests.Session()
resp = s.get(url, headers=headers, verify=False, data=payload)
#print(resp.text)
resp_cookie = resp.cookies.get_dict()
print(resp_cookie)
x = resp_cookie['__ac_nonce']
print(x)

通过请求详情页链接得到 cookie中的__ac_nonce的值:
在这里插入图片描述
有js基础的观察下面不难发现这个cookie的来源就是window.byted_acrawler.sign("",f),至于b嘛,其实就是__ac_nonce加密后的值:

var f=b;
var g=window.byted_acrawler.sign("",f);
document.cookie="__ac_signature="+g+";

<script src='https://sf1-ttcdn-tos.pstatp.com/obj/rc-web-sdk/acrawler.js'></script><script>window.byted_acrawler.init({aid:99999999,dfp:!0});var b;a:{for(var c=document.cookie.split(/[;&]/),d,e=0;e<c.length;e++){for(d=c[e];" "===d.charAt(0);)d=d.substring(1,d.length);if(0===d.indexOf("__ac_nonce=")){b=d.substring(11,d.length);break a}}b=""}var f=b;var g=window.byted_acrawler.sign("",f);document.cookie="__ac_signature=; expires=Mon, 20 Sep 1970 00:00:00 UTC; path=/;";
document.cookie="__ac_signature="+g+"; expires="+(new Date((new Date).getTime()+18E5)).toGMTString()+"; path=/;";window.location.reload();</script>

还是老样子喽,我尝试用nodejs 加PyExecJS 这样就不用使用selenium去获取参数了,但是nodejs是真的一点不会,搞了半天还是没有引入acrawler.js 最终选择放弃 使用常规擅长的flask web 然后selenium 请求得到参数 多的不说上代码:

使用flask 请求下面这个页面得到参数,如:
http://127.0.0.1:9899/?ac_none=05ef7f8fe002acc3a3474
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
         <script src="https://sf1-ttcdn-tos.pstatp.com/obj/rc-web-sdk/acrawler.js"></script>

	    <title>Decode API</title>
    </head>
    <body>
    <p id="ac_none">{{res.ac_none}}</p>
    <p id="ac_signature"></p>
<script>
function vmx(t) {
XMLHttpRequest = window.XMLHttpRequest;
window.byted_acrawler.init({aid:99999999,dfp:!0});

return window.byted_acrawler.sign("",t)
    }
    var ac_none=document.getElementById('ac_none').innerHTML+''
    console.log(ac_none)
document.getElementById("ac_signature").innerHTML=vmx(ac_none)
</script>
    </body>
</html>

将页面获取到的参数带入python代码:

url = "https://www.toutiao.com/a6288186179145875713/"

payload = {}
headers = {
  'Host': 'www.toutiao.com',
  'Cookie': '__ac_nonce=05ef7f8fe002acc3a3474; __ac_signature=_02B4Z6wo00f01pqSLbwAAIBBg8zLiTQbh16anikAAPhiVQiMcglhHh0EcXbSltVEV41v8Z3AXPaN8QXGnmm4CV0GXhLcxaKHSPxt6MzFGJT.ZnEmWPfbw7oY3g.7mfXH7P-HDWYx6wLx6BXD21',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36',
  'referer': 'https://www.toutiao.com/a6826538503917011470/'
}

response = requests.request("GET", url, verify=False, headers=headers, data=payload)

print(response.text)

效果如下就成功了:
在这里插入图片描述
**

注明:此文章仅用作学习切勿使用非法途径,否则后果自负!

**

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孤独的三毛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值