(self, query_text:str, from_language:str='auto', to_language:str='en', **kwargs)
return data if is_detail_result else '\n'.join([x['dst'] for x in data['trans_result']['data']])
https://fanyi.baidu.com
        :param query_text: str, must.
        :param from_language: str, default 'auto'.
        :param to_language: str, default 'en'.
        :param **kwargs:
                :param professional_field: str, default 'common'. Choose from ('common', 'medicine', 'electronics', 'mechanics')
                :param if_ignore_limit_of_length: boolean, default False.
                :param is_detail_result: boolean, default False.
                :param timeout: float, default None.
                :param proxies: dict, default None.
                :param sleep_seconds: float, default `random.random()`.
        :return: str or dict
https://fanyi.baidu.com
        :param query_text: str, must.
        :param from_language: str, default 'auto'.
        :param to_language: str, default 'en'.
        :param **kwargs:
                :param professional_field: str, default 'common'. Choose from ('common', 'medicine', 'electronics', 'mechanics')
                :param if_ignore_limit_of_length: boolean, default False.
                :param is_detail_result: boolean, default False.
                :param timeout: float, default None.
                :param proxies: dict, default None.
                :param sleep_seconds: float, default `random.random()`.
        :return: str or dict
https : // fanyi . baidu . com : param query_text : str must . : param from_language : str default auto . : param to_language : str default en . : param ** kwargs : : param professional_field : str default common . Choose from ( common medicine electronics mechanics ) : param if_ignore_limit_of_length : boolean default False . : param is_detail_result : boolean default False . : param timeout : float default None . : param proxies : dict default None . : param sleep_seconds : float default random . random () . : return : str or dict
def baidu_api(self, query_text:str, from_language:str='auto', to_language:str='en', **kwargs) -> Union[str,dict]:
        """
        https://fanyi.baidu.com
        :param query_text: str, must.
        :param from_language: str, default 'auto'.
        :param to_language: str, default 'en'.
        :param **kwargs:
                :param professional_field: str, default 'common'. Choose from ('common', 'medicine', 'electronics', 'mechanics')
                :param if_ignore_limit_of_length: boolean, default False.
                :param is_detail_result: boolean, default False.
                :param timeout: float, default None.
                :param proxies: dict, default None.
                :param sleep_seconds: float, default `random.random()`.
        :return: str or dict
        """

        use_domain = kwargs.get('professional_field', 'common')
        assert use_domain in ('common', 'medicine', 'electronics', 'mechanics')
        is_detail_result = kwargs.get('is_detail_result', False)
        timeout = kwargs.get('timeout', None)
        proxies = kwargs.get('proxies', None)
        sleep_seconds = kwargs.get('sleep_seconds', random.random())
        if_ignore_limit_of_length = kwargs.get('if_ignore_limit_of_length', False)
        query_text = self.check_query_text(query_text, if_ignore_limit_of_length)
    
        with requests.Session() as ss:
            host_html = ss.get(self.host_url, headers=self.host_headers, timeout=timeout, proxies=proxies).text
            sign_html = self.get_sign_html(ss, host_html, timeout, proxies)

            self.host_info = self.get_host_info(host_html, sign_html, query_text)
            self.new_bdtk = {"baidu_id": ss.cookies.get("BAIDUID"), "token": self.host_info.get("token")}
            self.language_map = self.host_info['langMap']
            from_language,to_language = self.check_language(from_language,to_language,self.language_map,output_zh=self.output_zh)
            self.api_headers.update({"cookie": "BAIDUID={};".format(self.bdtk['baidu_id'])})

            if from_language == 'auto':
                res = ss.post(self.langdetect_url, headers=self.api_headers, data={"query": query_text}, timeout=timeout, proxies=proxies)
                from_language = res.json()['lan']
            
            # param_data = {"from": from_language, "to": to_language}
            form_data = {
                "from": from_language,
                "to": to_language,
                "query": query_text,  # from urllib.parse import quote_plus
                "transtype": "translang",  # ["translang","realtime"]
                "simple_means_flag": "3",
                "sign": self.host_info.get('sign'),
                "token": self.bdtk['token'],  # self.host_info.get('token'),
                "domain": use_domain,
            }
            form_data = urllib.parse.urlencode(form_data).encode('utf-8')
            r = ss.post(self.api_url, headers=self.api_headers, data=form_data, timeout=timeout, proxies=proxies)
            r.raise_for_status()
            data = r.json()
        time.sleep(sleep_seconds)
        self.query_count += 1
        return data if is_detail_result else '\n'.join([x['dst'] for x in data['trans_result']['data']])
def baidu_api ( self , query_text : str , from_language : str = 'auto' , to_language : str = 'en' , * * kwargs ) -> Union [ str , dict ] : use_domain = kwargs . get ( 'professional_field' , 'common' ) assert use_domain in ( 'common' , 'medicine' , 'electronics' , 'mechanics' ) is_detail_result = kwargs . get ( 'is_detail_result' , False ) timeout = kwargs . get ( 'timeout' , None ) proxies = kwargs . get ( 'proxies' , None ) sleep_seconds = kwargs . get ( 'sleep_seconds' , random . random ( ) ) if_ignore_limit_of_length = kwargs . get ( 'if_ignore_limit_of_length' , False ) query_text = self . check_query_text ( query_text , if_ignore_limit_of_length ) with requests . Session ( ) as ss : host_html = ss . get ( self . host_url , headers = self . host_headers , timeout = timeout , proxies = proxies ) . text sign_html = self . get_sign_html ( ss , host_html , timeout , proxies ) self . host_info = self . get_host_info ( host_html , sign_html , query_text ) self . new_bdtk = { "baidu_id" : ss . cookies . get ( "BAIDUID" ) , "token" : self . host_info . get ( "token" ) } self . language_map = self . host_info [ 'langMap' ] from_language , to_language = self . check_language ( from_language , to_language , self . language_map , output_zh = self . output_zh ) self . api_headers . update ( { "cookie" : "BAIDUID={};" . format ( self . bdtk [ 'baidu_id' ] ) } ) if from_language == 'auto' : res = ss . post ( self . langdetect_url , headers = self . api_headers , data = { "query" : query_text } , timeout = timeout , proxies = proxies ) from_language = res . json ( ) [ 'lan' ] # param_data = {"from": from_language, "to": to_language} form_data = { "from" : from_language , "to" : to_language , "query" : query_text , # from urllib.parse import quote_plus "transtype" : "translang" , # ["translang","realtime"] "simple_means_flag" : "3" , "sign" : self . host_info . get ( 'sign' ) , "token" : self . bdtk [ 'token' ] , # self.host_info.get('token'), "domain" : use_domain , } form_data = urllib . parse . urlencode ( form_data ) . encode ( 'utf-8' ) r = ss . post ( self . api_url , headers = self . api_headers , data = form_data , timeout = timeout , proxies = proxies ) r . raise_for_status ( ) data = r . json ( ) time . sleep ( sleep_seconds ) self . query_count += 1 return data if is_detail_result else '\n' . join ( [ x [ 'dst' ] for x in data [ 'trans_result' ] [ 'data' ] ] )