标签:加密,python,self,ts,url,print,某猫,data,page From: https://www.cnblogs.com/huanghaobing/p/16882738.html
import hashlib
import json
import random
import time
import requests
sha256 = hashlib.sha256()
'''
某猫投诉
https://jiangsu.tousu.sina.com.cn/
'''
class BlackCatComplaints(object):
def __init__(self):
self.headers = {
'referer': 'https://jiangsu.tousu.sina.com.cn/',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
'x-requested-with': 'XMLHttpRequest',
}
def get_url(self, page_num): # 解码URL
c = str(int(time.time() * 1000)) # 13位时间戳
a = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
h = ''.join(random.choice(a) for i in range(16)) # 随机16个字符
d = '$d6eb7ff91ee257475%' # 默认值
e = '2' # 最新信息为2
u = '10' # 每页数量
page = str(page_num) # 页码
ts = c
rs = h
bb = [d, u, c, e, page, h]
bb.sort()
signature = hashlib.sha256((''.join(bb)).encode('utf-8')).hexdigest()
url = 'https://jiangsu.tousu.sina.com.cn/api/index/feed?ts={}&rs={}&signature={}&callback=jQuery111207497989586833758_{}&type=2&page_size=10&page={}&_={}'.format(
ts, rs, signature, ts, page_num, ts)
self.get_data(ts, url)
def get_data(self, ts, url): # 请求数据
print(url)
response = requests.get(url=url, headers=self.headers)
response = response.text.replace('\/', '')
content = response.encode('utf-8').decode('unicode_escape')# 乱码解码
jQuery = 'try{jQuery111207497989586833758_' + ts + '('
catch = ');}catch(e){};'
replace_data = content.replace(jQuery, '').replace(catch, '').replace('\n', '')
json_data = json.loads(replace_data)
# print(json_data)
#
if json_data['result']['status']['code'] == 0:
self.get_detail(json_data['result']['data'])
else:
print('接口请求失败!!!')
def get_detail(self, data): # 处理数据
for i in data['lists']:
title = i['main']['title']
url = i['main']['url']
timestamp = i['main']['timestamp']
author = i['author']['title']
avatar = i['author']['avatar']
summary = i['main']['summary']
url = 'https://' + url.replace('complaintview','/complaint/view/') + '/'
timeArray = time.localtime(int(timestamp))
time_str = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(title)
print(url)
print(time_str)
print(author)
print(avatar)
print(summary)
if __name__ == '__main__':
back = BlackCatComplaints()
back.get_url(page=1)