单帆蔡 发表于 2020-8-24 11:36:24

尝试爬百度翻译 一直提示未知错误 求助大佬指点

请输入要翻译的内容:love
{'errno': 997, 'errmsg': '未知错误', 'query': 'love', 'from': 'en', 'to': 'zh', 'error': 997}


代码
import urllib.request
import urllib.parse
import json

while True:
        content = input('请输入要翻译的内容:')


        url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'
        date = {}
        date['query']=content
        date['domain']='common'
        date['from']='en'
        date['sign']='682950.952567'
        date['simple_means_flag']= 3
        date['to']='zh'
        date['token']='c9c1a05ce811848d5ef65ec86bd7a822'
        date['transtype']='realtime'
       


        date = urllib.parse.urlencode(date).encode('utf-8')


        response = urllib.request.urlopen(url,date)
        html = response.read().decode('utf-8')
       

        target = json.loads(html)
        print(target)
        target = target["trans_result"]["data"]["dst"]
        print(target)

单帆蔡 发表于 2020-8-24 21:08:19

import urllib.request
import urllib.parse
import json

while True:
        content = input('请输入要翻译的内容:')
       
        url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'
        head = {}
        head['user-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763'
        date = {}
        date['query']=content
        date['domain']='common'
        date['from']='en'
        date['sign']='682950.952567'
        date['simple_means_flag']='3'
        date['to']='zh'
        date['token']='c9c1a05'
        date['lts']='c9c1a05ce811848d5ef65ec86bd7a822'
        date['bv']='cc652a2ad669c22da983a705e3bca726'
        date['transtype']='realtime'
       


        date = urllib.parse.urlencode(date).encode('utf-8')
        req = urllib.request.Request(url,date,head)
       


        response = urllib.request.urlopen(req)
        html = response.read().decode('utf-8')


        target = json.loads(html)
        print(target)
        target = target["trans_result"]["data"]["dst"]
        print(target)

加了user-Agent 还是 提示未知错误

伏惜寒 发表于 2020-8-24 23:47:56

因为你被反爬了,headers(重点是useragent)和代理ip和动态验证码,这三个加上基本就没问题了
页: [1]
查看完整版本: 尝试爬百度翻译 一直提示未知错误 求助大佬指点