码迷,mamicode.com
首页 > 编程语言 > 详细

python爬取百度翻译返回:{'error': 997, 'from': 'zh', 'to': 'en', 'query 问题

时间:2018-04-18 23:46:08      阅读:5522      评论:0      收藏:0      [点我收藏+]

标签:lin   escape   href   mobile   爬取   utf-8   返回   res   agent   

解决办法:

修改url为手机版的地址:http://fanyi.baidu.com/basetrans
User-Agent也用手机版的

测试代码:

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:     requestsGet
   Description :    爬取在线翻译数据s
   Author :       神秘藏宝室
   date:          2018-04-17
-------------------------------------------------
   Change Activity:
                   2018-04-17:
-------------------------------------------------
"""
import requests
url = "http://fanyi.baidu.com/basetrans"

data = {
    "query":"你好世界",
    "from":"zh",
    "to":"en",

}

headers = {
    "User-Agent":"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36",
        }

response = requests.post(url,data = data,headers = headers)

# print response.request.url     #请求地址
# print response.url              #响应地址
# print response.request.headers  #请求头
# print response.headers          #响应头
print response.content.decode(‘unicode-escape‘)     #显示出来unicode的中文
# print response.text

返回结果:

{"errno":0,"from":"zh","to":"en","trans":[{"dst":"Hello world","prefixWrap":0,"src":"你好世界","relation":[],"result":[[0,"Hello world",["0|12"],[],["0|12"],["0|11"]]]}],"dict":[],"keywords":[{"means":["hello","hi","How do you do!"],"word":"你好"}]}

python爬取百度翻译返回:{'error': 997, 'from': 'zh', 'to': 'en', 'query 问题

标签:lin   escape   href   mobile   爬取   utf-8   返回   res   agent   

原文地址:https://www.cnblogs.com/Mysterious/p/8877970.html

(9)
(2)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!