1. requests模块发送post请求
# -*- coding: utf-8 -*- """ Created on Sun Jan 19 01:26:05 2020 @author: douzi """ # -*- coding: utf-8 -*- import requests import json import sys query_str = sys.argv[1] headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"} data = { "query": query_str, "from": "zh", "to": "en" } post_url = "https://fanyi.baidu.com/basetrans" r = requests.post(post_url, data=data, headers=headers) print(r.content.decode()) dict_ret = json.loads(r.content.decode()) ret = dict_ret["trans"][0]["dst"] print("result is : ", ret)
(ps:好像现在百度翻译手机版,sign也是js生成的,这里就当复习一下post了)