import requestsimport json# 1. urlurl = ‘http://fanyi.baidu.com/sug‘# 2. form/ data/ bodydef translate(kw): ???form = { ???????‘kw‘: kw ???} ???????# request 的post方法色使用 ???response = requests.post(url, data=form) ???# str = response.content.decode(‘utf-8/gbk/gb2312/gb18080‘) = ???response.encoding = ‘utf-8/gbk/gb2312/gb18080‘ ???str = response.text ???# 4. 解析json ???res_dict = json.loads(response.text) ???# 5. 打印翻译结果 ???#print(res_dict) ???result = res_dict[‘data‘][0][‘v‘] ???return resultif __name__ == ‘__main__‘: ???res = translate(‘哈哈‘) ???print(res) ???res = translate(‘绿色‘) ???print(res)
baidu_urllib_requests
原文地址:https://www.cnblogs.com/gxsmm/p/9484520.html