爬虫篇(2)使用pyexecjs破解js中cookies

本文介绍了如何使用pyexecjs来破解网页中的JavaScript设置的cookies。通过解析网页JS,执行stringToHex()和YunSuoAutoJump()函数,处理PhantomJS的screen未定义错误,最终成功获取并使用cookies进行后续请求,从而实现对http://www.landchina.com的爬取。详细步骤包括获取js数据、解析cookies、模拟浏览器环境以及利用requests库发起带cookies的请求。
摘要由CSDN通过智能技术生成

首先下载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设

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值