网站列表页加密:三次请求后返回内容多\r

news/2024/7/19 12:30:46 标签: javascript, python, 爬虫

一、抓包第一次请求

url = 'aHR0cDovL2N5eHcuY24vQ29sdW1uLmFzcHg/Y29saWQ9MTA='

抓包,需要清理浏览器cookie,或者无痕模式打开网址,否则返回的包不全,依照下图中的第一个包进行requests请求

在这里插入图片描述

第一次请求后返回

<!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 = "/Column.aspx?colid=10&security_verify_data=" + stringToHex(screendate);}</script><script>setTimeout("YunSuoAutoJump()", 50);</script></head><!--2023-10-11 11:55:23--></html>

其中stringToHex方法,用于将字符串转换为十六进制表示:

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;
}

python实现

def stringToHex(data):
    valu = ''
    for i in range(0, len(data), 1):
        # 获取字符串中索引为 i 的字符的 Unicode 值,并转换为十六进制字符串表示
        unicode_value = ord(data[i])
        val = hex(unicode_value)[2:]  # [2:] 是为了去掉十六进制字符串前面的 '0x' 前缀
        valu = valu + val                     # 顺序不能反,否则转换的十六进制是倒着的
    return valu

YunSuoAutoJump()方法,设置了cookies中的一个srcurl值,还有第二次请求的url:

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 = "/Column.aspx?colid=10&security_verify_data=" + stringToHex(screendate);
}

screendate是定值,因此

url2 = url + '&security_verify_data=313232302c363836'

二、第二次请求

观察浏览器抓到的第二个请求的cookies:

在这里插入图片描述

发现cookies中除srcurl还有security_verify_data,与第一次请求对比发现,在第一次请求时携带security_verify_data,如下图:

在这里插入图片描述

因此第二次请求的cookies为:

headers1 = resp1.headers.get('Set-Cookie')
result = headers1.split(';')[0]
cookies = {}
key, value = result.split('=')
cookies[key] = value

key = 'srcurl'
value = stringToHex(url)
cookies[key] = value

进行第二次请求,返回

<!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>
	var cookie_custom = {
	hasItem: function(sKey) {
		return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
	},
	removeItem: function(sKey, sPath) {
		if (!sKey || !this.hasItem(sKey)) {
			return false;
		}
		document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sPath ? "; path=" + sPath : "");
		return true;
	}
};
	function YunSuoAutoJump() {
		self.location = "aHR0cDovL2N5eHcuY24vQ29sdW1uLmFzcHg/Y29saWQ9MTA=";
	}
</script>
<script>setTimeout("cookie_custom.removeItem('srcurl');YunSuoAutoJump();", 50);</script></head></html>

由返回可知,第三次请求的url与第一次请求一致,cookies要除去第二次请求的srcurl这个键,再与浏览器中的第三次请求进行对比。

在这里插入图片描述

cookies中多了security_session_mid_verify这个键,同理可知,这个值在第二次请求的Set-Cookie处取得,cookies如下

headers2 = resp2.headers.get('Set-Cookie')
key, value = headers2.split(';')[0].split('=')
cookies[key] = value
cookies.pop('srcurl')    # 删去srcurl键值对

三、第三次请求

resp3 = requests.get(url, headers=headers, verify=False, cookies=cookies).content

此网站如果使用requests.get().text返回的内容不对,所以用content查看返回内容,发现是乱码,部分如下:

\xe5\x9b\xbe\xe7\x89\x87\xe6\x87\x92\xe5\x8a\xa0\xe8\xbd\xbd\r  

先解码,再删去返回中的\r,即返回正文内容

text = resp3.decode().replace('\r', '')

http://www.niftyadmin.cn/n/5086041.html

相关文章

生成Android证书

前提&#xff1a;确保本机安装好了java1.8 第一步 打开CMD keytool -genkey -alias AAAA -keyalg RSA -keysize 2048 -validity 36500 -keystore D:\mygitee\keyStore\szxApp.jksD:\mygitee\keyStore\szxApp.jks是证书要生成的位置 szxApp.jks 是证书名称 D:\mygitee\keySto…

Python装饰器dome

import time from functools import wrapsdef statistical_time(time_type):print(time_type)def gel(fun):wraps(fun) # 不改变使用装饰器原有函数的结构(如__name__, __doc__)def fool(*args, **kwargs):start_time time.time()res fun(*args, **kwargs)end_time time.ti…

竞赛选题 深度学习LSTM新冠数据预测

文章目录 0 前言1 课题简介2 预测算法2.1 Logistic回归模型2.2 基于动力学SEIR模型改进的SEITR模型2.3 LSTM神经网络模型 3 预测效果3.1 Logistic回归模型3.2 SEITR模型3.3 LSTM神经网络模型 4 结论5 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 …

多任务(多进程和多线程dome)

多进程dome from multiprocessing import Process from os import getpid from random import randint import timedef download_task(filen_neme):print(开始下载%s... % filen_neme)time_to_download randint(5, 10)time.sleep(time_to_download)print(%s下载完成&#xff…

WebBrowser 打印设置,打印预览,去页眉和页脚

WebBrowser是IE内置的浏览器控件&#xff0c;无需用户下载. 一、WebBrowser控件   <object IDWebBrowser WIDTH0 HEIGHT0 CLASSIDCLSID:8856F961-340A-11D0-A96B-00C04FD705A2></object> 二、WebBrowder控件的方法 //打印 WebBrowser1.ExecWB(6,1); //打印设置 W…

【AI视野·今日NLP 自然语言处理论文速览 第五十二期】Wed, 11 Oct 2023

AI视野今日CS.NLP 自然语言处理论文速览 Wed, 11 Oct 2023 Totally 81 papers &#x1f449;上期速览✈更多精彩请移步主页 Daily Computation and Language Papers LongLLMLingua: Accelerating and Enhancing LLMs in Long Context Scenarios via Prompt Compression Author…

Apipost连接数据库详解

Apipost提供了数据库连接功能&#xff0c;在接口调试时可以使用数据库获取入参或进行断言校验。目前的Apipost支持&#xff1a;Mysql、SQL Sever、Oracle、Clickhouse、达梦数据库、PostgreSQL、Redis、MongoDB 8种数据库的连接操作 新建数据库连接&#xff1a; 在「项目设置…

【VR】【Unity】白马VR课堂系列-VR开发核心基础03-项目准备-VR项目设置

【内容】 详细说明 在设置Camera Rig前,我们需要针对VR游戏做一些特别的Project设置。 点击Edit菜单,Project Settings,选中最下方的XR Plugin Management,在右边面板点击Install。 安装完成后,我们需要选中相应安卓平台下的Pico VR套件,关于怎么安装PICO VR插件,请参…