Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search 请求失败 #16

Open
zhiyiYo opened this issue Jul 14, 2023 · 0 comments
Open

search 请求失败 #16

zhiyiYo opened this issue Jul 14, 2023 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@zhiyiYo
Copy link

zhiyiYo commented Jul 14, 2023

试着把 KuWoApi 的代码剥离出来,然后直接调用 search() 方法,但是仍然无法得到数据:

from hashlib import md5, sha1, sha256
import requests


class KuwoApi:
    """ kuwo music API class """

    API_BASE: str = 'http://www.kuwo.cn/api/www'

    def __init__(self):
        """ class initializer """
        self.timeout = 30
        self.headers = {
            'Accept': '*/*',
            'Accept-Encoding': 'gzip,deflate',
            'Accept-Language': 'zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4',
            'Referer': 'http://kuwo.cn',
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/81.0.4044.138 Safari/537.36',
            'Host': 'kuwo.cn',
        }
        self.mobi_headers = {'User-Agent': 'okhttp/3.10.0'}

        # 网络请求中的值,发现 Cross 和 md5 算出来的不一样
        # random_str = "BH4h5GXbY45H266eeACjWdNAzmE48iwy"
        # self.headers['Cross'] = "2eddd1c15545e43176876e4f142d4448"

        random_str = "123456789"
        self.headers['csrf'] = random_str
        self.cookie = {'kw_token': random_str, 'Hm_token': random_str}
        self.headers['Cross'] = md5(random_str.encode('utf-8')).hexdigest()
        self._userid = ''
        self._sid = ''
        self._cookies = {}

    def search(self, keyword: str, limit=20, page=1) -> dict:
        uri = KuwoApi.API_BASE + f'/search/searchMusicBykeyWord?key={keyword}&pn={page}&rn={limit}'
        with requests.Session() as session:
            response = session.get(uri, cookies=self.cookie, headers=self.headers)
            data = response.json()
            return data


response = KuwoApi().search('Hello', 20, 1)
print(response) # {'success': False, 'message': 'CSRF token Invalid!', 'now': '2023-07-14T15:00:49.148Z'}
@BruceZhang1993 BruceZhang1993 added the bug Something isn't working label Jul 17, 2023
@BruceZhang1993 BruceZhang1993 self-assigned this Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants