目录
百度翻译详细翻译接口
这个接口,是我上一次用的接口,MD。
关于复制的小技巧
这个接口的参数数据就比较多了,我们都需要构建到data对象当中。
这里可以第一步,先复制数据,然后粘贴到sublime当中。
第二步,在sublime当中使用正则表达式进行匹配替换,具体的匹配正则是(.*): (.*)
,替换正则是'\1': '\2',
,如下所示:
第三步,当我们点击replace all之后,就会变成下面的样子,我们可以直接复制到pycharm当中。
复制浏览器全部的request header
使用了全部的request header之后,还是会出现错误的情况。
这是因为,如上所示的Accept-Encoding
这一行一定一定要注释
掉。
然后,就成功了:
代码
import urllib.request
import urllib.parse
import json
headers = {
'Accept': '*/*',
# 'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
'Acs-Token': '1707739203275_1707745719253_quc6AWGAYRx43JBG38dEQrzI0/YYmBkJA3kipqbjj92wSkwBPjmJrtAqbjrvirmdWeWzqhxfdL2y+nrdlzRvy92Xgy+AETOkPIh3LJnNTNP39WYcuwxtoMhR4FQqP/B8kGheWQjriq+3mfZLrQiBihGV24YtFwzltmJBDr/pHbrXjpx9Zp3bUkUk2WRipXeor+EihqZy+iY23s6iHe33KD1mBR3PnX+vhWA8mWcU3RpzpmE3xgUf9a3u94Cc+4n1Ibn0J/Ai6JWg6Hmf07Olu3TO1mK3L0RF0Au5t/WgeE/8Hj1Md23SeoUHddk64dJoJGsukByPOk6m2g2KZdx1VcCI4xS9eoxeDVcs6lwdsrgpuVlMtrul+Rkb+WdhaanPyVpwP8qDaV4p2KhyZhR4R+s22uoPRzFFDA7FvJ2UUZeuUC/FMC8y8tAflF3EltzfdOnDLDC+xmqLkKnzIFyDo6VNnTFBSVh3dROohT2vN04=',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Content-Length': '152',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': 'H_WISE_SIDS=216851_213351_214796_110085_244726_261710_236312_256419_265881_266361_267288_267074_268478_268592_268030_259642_268670_269731_269832_269904_270083_267066_256739_270460_270548_270442_271173_271177_271226_256957_267659_269296_271319_265032_271271_271578_270102_271726_271882_271671_269853_269771_269875_269564_271943_256154_234295_234208_272223_272279_266566_267596_272365_272012_272504_253022_272077_272611_272764_272815_272801_260335_272998_271284_273060_273094_273154_273118_273141_273242_273301_273399_273396_271157_270055_273520_273462_273198_272562_271562_271147_273671_273703_264170_270185_273735_273741_273924_274081_273931_273965_274176_269610_274206_273918_273351_273045_273595_272858_272855_274328_272618_274356_272319_188331; H_WISE_SIDS_BFESS=216851_213351_214796_110085_244726_261710_236312_256419_265881_266361_267288_267074_268478_268592_268030_259642_268670_269731_269832_269904_270083_267066_256739_270460_270548_270442_271173_271177_271226_256957_267659_269296_271319_265032_271271_271578_270102_271726_271882_271671_269853_269771_269875_269564_271943_256154_234295_234208_272223_272279_266566_267596_272365_272012_272504_253022_272077_272611_272764_272815_272801_260335_272998_271284_273060_273094_273154_273118_273141_273242_273301_273399_273396_271157_270055_273520_273462_273198_272562_271562_271147_273671_273703_264170_270185_273735_273741_273924_274081_273931_273965_274176_269610_274206_273918_273351_273045_273595_272858_272855_274328_272618_274356_272319_188331; BDUSS=GtDYVJOQXhhOGFndk9XOFhKOUY3ZGd0OHNUTHBEOWlvam9SbXFYYXJIZWdTbHRsRVFBQUFBJCQAAAAAAAAAAAEAAABWdpLrvNPK2c2kwt4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKC9M2WgvTNlM; BDUSS_BFESS=GtDYVJOQXhhOGFndk9XOFhKOUY3ZGd0OHNUTHBEOWlvam9SbXFYYXJIZWdTbHRsRVFBQUFBJCQAAAAAAAAAAAEAAABWdpLrvNPK2c2kwt4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKC9M2WgvTNlM; BAIDUID=1051D247173D4AE24940CEE00AD208CF:FG=1; BIDUPSID=1051D247173D4AE24940CEE00AD208CF; PSTM=1702218668; H_PS_PSSID=40155_40203_40206_40211_40215_40222_40062; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDUID_BFESS=1051D247173D4AE24940CEE00AD208CF:FG=1; ZFY=sxANs46b2LscIvHG:AKuHlaU8yGhEVmSIK164G5Y8zvs:C; __bid_n=18d886405c14368b133294; BA_HECTOR=85al24248k0g24040la00l0lftfp3h1isjudt1t; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; PSINO=1; delPer=0; APPGUIDE_10_6_9=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1707741883; RT="z=1&dm=baidu.com&si=36f056a9-56e3-45a3-a296-bb466d58cafa&ss=lsiz2zmw&sl=9&tt=3y0&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=cxqk"; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1707745337; ab_sr=1.0.1_NmJiYTBiMjJmN2MyNmE3MDQ5YTYwZTZmYTc5YWY5ODc2NGJiYTg5ODU5NDcxNTNmY2ZjYTY2ZmI1ZmQ3OTIxYmMyYzA3Yjc5NTBmMTE1MDg3ZTgwYjcyMDI0MzRiMjRjOGQ3OGE1ZjUxOGRkYTNjMjYzZjJkNzBlZDJlNjYzZjExYzFhYzkwZjhjOTc4YzhmMDYxYmI0MDVkYzI2YTEzNDc1NjI3ZjQ5MDY4MGM0YjM2Njg3YTYyODVkOGNiMDVi',
'Host': 'fanyi.baidu.com',
'Origin': 'https://fanyi.baidu.com',
'Pragma': 'no-cache',
'Referer': 'https://fanyi.baidu.com/?aldtype=16047&ext_channel=Aldtype',
'Sec-Ch-Ua': '"Not A(Brand";v="99", "Google Chrome";v="121", "Chromium";v="121"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
}
url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'
# post请求的参数必须要进行编码
data = {
'from': 'en',
'to': 'zh',
'query': 'love',
'transtype': 'realtime',
'simple_means_flag': '3',
'sign': '198772.518981',
'token': '6dab4cd9524d6653409c8da4b382cf38',
'domain': 'common',
'ts': '1707745719210',
}
data = urllib.parse.urlencode(data).encode('utf-8')
request = urllib.request.Request(url=url, data=data, headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
obj = json.loads(content)
print(obj)
标签:baidu,060,翻译,request,urllib,Sec,data From: https://www.cnblogs.com/gnuzsx/p/18014185这里面起决定性作用的是Cookie。