本文主要是介绍爬虫篇(2)使用pyexecjs破解js中cookies,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先下载pyexecjs:
pip install PyExecJS
如果有需要,自行下载PyV8 , Node.js , PhantomJS等
使用参考:https://github.com/doloopwhile/PyExecJS
PyExecJS文档:https://pypi.org/project/PyExecJS/
此次采集链接:http://www.landchina.com/default.aspx?tabid=226
直接请求:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
url = 'http://www.landchina.com/default.aspx?tabid=226'
headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36',}html = requests.get(url, headers=headers, verify=False)
print("txte:",html.text)with open('数据2.html', 'w',encoding='utf8') as f:f.write(str(html.text) + '\n')
print("数据.html保存完毕!")
打开'数据2.html',获取js数据:
<!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.aspx?tabid=226&security_verify_data=" + stringToHex(screendate);}</script><script>setTimeout("YunSuoAutoJump()", 50);
</script
</head><!--2019-06-20 14:08:07--></html>
可以看到document.cookie=******************,这个就是js设置cookies的代码
使用PyExecJS执行stringToHex()函数看看,代码如下:
这篇关于爬虫篇(2)使用pyexecjs破解js中cookies的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!