基于python的今日头条文章抓取内含signature算法

扫二维码添加微信 备注:爬虫 , 拉你进爬虫交流群 或许你会成为第一个加群的人~ 刚有的创群想法!
在这里插入图片描述

1. 简单文字描述头条爬虫注意点

由于还没有大批量爬取测试,这里只能作为参考!
例如我们现在需要爬取右边的头条链接 https://www.toutiao.com/a6920021307803468302/
我们用python编写代码会发现,返回的并不是文字主题,而已简单的html代码,其中包含了一些script脚本

<html><head><meta charset="UTF-8" /></head><body></body><script src='https://sf1-ttcdn-tos.pstatp.com/obj/rc-web-sdk/acrawler.js'></script><script>function _f1(e,t){if("string"!=typeof t)return;var o,n=e+"=",r=t.split(/[;&]/);for(var e=0;e<r.length;e++){for(o=r[e];" "===o.charAt(0);)o=o.substring(1,o.length);if(0===o.indexOf(n))return o.substring(n.length,o.length)}return""}function _f2(e){return _f1(e,document.cookie)}function _f3(e,t,o){try{o&&(window.sessionStorage&&window.sessionStorage.setItem(e,t),window.localStorage&&window.localStorage.setItem(e,t));var n=31536e6;document.cookie=e+"=; expires=Mon, 20 Sep 1970 00:00:00 UTC; path=/;",document.cookie=e+"="+t+"; expires="+new Date((new Date).getTime()+n).toGMTString()+"; path=/;"}catch(e){}}window.byted_acrawler.init({aid:99999999,dfp:!0});var __ac_nonce=_f2("__ac_nonce"),__ac_signature=window.byted_acrawler.sign("",__ac_nonce);_f3("__ac_signature",__ac_signature),_f3("__ac_referer",document.referrer||"__ac_blank",!0);try{sessionStorage.setItem("__ac_ns",performance.timing.navigationStart)}catch(e){};window.location.reload();</script></html>

其中https://sf1-ttcdn-tos.pstatp.com/obj/rc-web-sdk/acrawler.js这里是计算signature的代码,然后我们还可以看到上面的代码中有下面这2块代码。
它是我们能否得到正确的得到文章数据的关键因素。然后就是扣代码和补全浏览器环境。具体看代码实现吧。

var __ac_nonce=_f2("__ac_nonce"),
__ac_signature=window.byted_acrawler.sign("",__ac_nonce)

2. 附件代码

链接: https://pan.baidu.com/s/1OQMLsamt_Vhhj8zGAiGcRw 提取码: t6zf

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐