CsdnKiramario.py 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author: Roc-xb
"""
import hashlib
import hmac
from base64 import b64encode
import random
import requests

# 获取x-ca-nonce
def get_nonce():
    text = ""
    char_list = []
    for c in range(97, 97 + 6):
        char_list.append(chr(c))
    for c in range(49, 58):
        char_list.append(chr(c))
    for i in "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx":
        if i == "4":
            text += "4"
        elif i == "-":
            text += "-"
        else:
            text += random.choice(char_list)
    return text


# 获取x-ca-signature
def get_signature(nonce):
    appSecret = "OTGHZy1hLh1HFWbLnpG68OwZGc2TQwld".encode()
    to_enc = f"GET\napplication/json, text/plain, */*\n\n\n\nx-ca-key:203871397\nx-ca-nonce:{nonce}\n/mp/ask/v1/questions/list?communityInfo=1&pageNo=1&pageSize=30&quick=3&rewardType=2&sortBy=1&type=5".encode()
    signature = b64encode(hmac.new(appSecret, to_enc, digestmod=hashlib.sha256).digest()).decode()
    return signature


# 获取接口数据
def get_list(url):
    nonce = get_nonce()
    signature = get_signature(nonce)
    print(nonce)
    print(signature)
    payload = {}
    headers = {
        'authority': 'bizapi.csdn.net',
        'pragma': 'no-cache',
        'cache-control': 'no-cache',
        'sec-ch-ua': '"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"',
        'dnt': '1',
        'x-ca-signature-headers': 'x-ca-key,x-ca-nonce',
        'x-ca-signature': signature,
        'x-ca-nonce': nonce,
        'sec-ch-ua-mobile': '?0',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
        'uber-trace-id': '50d7304716310079669041001c6a0ab7:50d7304716310089:0:0',
        'accept': 'application/json, text/plain, */*',
        'x-ca-key': '203871397',
        'origin': 'https://ask.csdn.net',
        'sec-fetch-site': 'same-site',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://ask.csdn.net/?spm=1005.2025.3001.4492&rewardType=2&stateType=5&sortBy=1&quick=3',
        'accept-language': 'zh-CN,zh;q=0.9',
    }
    response = requests.request("GET", url, headers=headers, data=payload).json()
    for item in response['data']['list']:
        print(item['title'])


if __name__ == '__main__':
    get_list(f"https://bizapi.csdn.net/mp/ask/v1/questions/list?pageNo=1&pageSize=30&communityInfo=1&rewardType=2&type=5&sortBy=1&quick=3")