import requests
import json
from Crypto.Cipher import AES # 开始解密
from Crypto.Util.Padding import unpad #去填充的逻辑
import base64
url = 'https://app.swguancha.com/client/v1/cPublic/consumer/baseInfo'
data = {
'current': 1,
"dimensionTime": "2019",
"levelType": 2,
"propertyCode": ["DISTRICT_PROP_GJ025_RJDQSCZZ", "DISTRICT_PROP_GJ117_NMSYGGQDCYYCLS", "DISTRICT_PROP_GJ001_NMHJRK"],
"size": 16,
}
headers = {
"Content-Type": "application/json;charset=UTF-8",
"Devicetype": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
response = requests.post(url=url,data=json.dumps(data),headers=headers)
print(response.text)
aes = AES.new(key='QV1f3nHn2qm7i3xrj3Y9K9imDdGTjTu9'.encode('utf-8'),mode=AES.MODE_ECB)
ming_bs = aes.decrypt(base64.b64decode(response.text)) # 先将密文变成字节再解密
ming_bs = unpad(ming_bs,16) # AES处理的长度就是16
ming = ming_bs.decode("utf-8")
print(ming)
json_ming = json.loads(ming) # 转换为json字典格式去提取数据
for i in json_ming['data']['data']:
print(i)
# requests.post传递参数的时候,如果传的是字典,默认会被处理成form data 即current=1&dimensionTime=2019&levelType=2&propertyCode=DISTRICT_PROP_GJ025_RJDQSCZZ&propertyCode=DISTRICT格式
# 但是浏览器传递的是json格式,所以发生了错误,解决的方法,import json,json.dumps(data)转换为json字符串格式,或者json=data
结果展现: