学知识拯救世界 发表于 2020-12-22 18:30:22

翻译爬虫

import requests
import json

if __name__ == '__main__':
    url = 'https://cn.bing.com/ttranslatev3'
    kw = input('enter a word:')
    data = {
      'fromLang': 'auto - detect',
      'text': '你好',
      'to': 'zh - Hant',
      'isVertical': '1',
      'IG': 'C5705396C9EE4E68894807E27BF573BD',
      'IID': 'translator.5024.5',
    }
    headers = {
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    }
    response = requests.post(url=url,headers=headers,data=data)
    print(response)
    result_list = response.json()
    print(result_list)


为什么输出的结果为
enter a word:你好
<Response >
{'statusCode': 400}

YunGuo 发表于 2020-12-22 23:31:09

参数传入错误。isVertical、IG、IID是写到url后面(你直接复制浏览器url就行),不是写data里面。
import requests


url = 'https://cn.bing.com/ttranslatev3?isVertical=1&&IG=52F0F718127D4C35862CC2EBE7A4626F&IID=translator.5024.1'
header = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
                  '(KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
}
data = {
    'fromLang': 'auto-detect',
    'text': '你好',
    'to': 'en'
}
res = requests.post(url, headers=header, data=data)
print(res.text)
页: [1]
查看完整版本: 翻译爬虫