Python 爬取必应翻译
程序员文章站
2022-05-04 12:07:12
...
封装爬取必应翻译功能
import requests
def transation(text):
tunsXml = requests.get('https://cn.bing.com/translator/')
if tunsXml.status_code != 200:
raise Exception('')
pos = tunsXml.text.find('IG:"')
IG = tunsXml.text[pos + 4:pos + 4 + 32]
cookie = tunsXml.cookies
Counts = 1
url = 'https://cn.bing.com/ttranslatev3?isVertical=1&&IG={}&IID=translator.5028.{}'.format(IG, Counts)
postdata = '&fromLang=en&text={}&to=zh-Hans'.format( requests.utils.quote(text))
headers = {
'accept': '*/*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
'content-type': 'application/x-www-form-urlencoded',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'origin': 'https://cn.bing.com',
'referer': 'https://cn.bing.com/translator/',
'content-length': str(len(postdata))
}
res = requests.post(url=url, headers=headers, data=postdata, cookies=cookie)
if res.status_code != 200:
raise Exception('')
return res.json()[0]['translations'][0]['text']
上一篇: 爬虫:爬取微软必应翻译做成API
下一篇: 电子英汉词典C语言课程设计