xyy1239012884 发表于 2018-3-7 12:09:48

好帖子!!感谢楼主!!

noda 发表于 2018-3-24 16:13:15

请问一下,我看了零基础入门学习python的“一只爬虫的自我修养2”,讲有道翻译的,我之后想再练习一下就用了百度翻译,出来的结果是{"error":997,"from":"en","to":"zh","query":"this"},请问是怎么回事呀,求解答

Lilyse 发表于 2018-4-6 21:39:09

研究中

haha_1 发表于 2018-8-2 10:03:10

好。

汝鱼得水 发表于 2018-8-16 11:08:59

感谢分享的好东西。

伊瑟拉觉醒 发表于 2018-9-9 01:24:26

非常有用!

nbPlus 发表于 2018-9-10 17:03:07

nice!!!!!!

学程序的LILI 发表于 2018-9-26 11:16:20

============ RESTART: C:\Users\zhangyuanli\Desktop\translation.py ============
{"errorCode":50}
>>> import urllib.request
import urllib.parse


url="http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
data={}
data['i']='i love fishc.com'
data['from']='AUTO'
data['to']='AUTO'
data['smartresult']='dict'
data['client']='fanyideskweb'
data['salt']='1537863642103'
data['sign']='b9e371ea87a15b977e2202c10f6cc272'
data['doctype']='json'
data['version']='2.1'
data['keyfrom']='fanyi.web'
data['action']='FY_BY_CLICKBUTTION'
data['typoResult']='false'

data=urllib.parse.urlencode(data).encode('utf-8')
response=urllib.request.urlopen(url,data)
html=response.read().decode('utf-8')
print(html)





============ RESTART: C:\Users\zhangyuanli\Desktop\translation.py ============
{"errorCode":50}
>>> import json
>>> json.loads(html)
{'errorCode': 50}
>>>

学程序的LILI 发表于 2018-9-26 11:23:10

import urllib.request
import urllib.parse
import json

content=input('请输入待翻译的内容:')
url="http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
data={}
data['i']=content
data['from']='AUTO'
data['to']='AUTO'
data['smartresult']='dict'
data['client']='fanyideskweb'
data['salt']='1537863642103'
data['sign']='b9e371ea87a15b977e2202c10f6cc272'
data['doctype']='json'
data['version']='2.1'
data['keyfrom']='fanyi.web'
data['action']='FY_BY_CLICKBUTTION'
data['typoResult']='false'

data=urllib.parse.urlencode(data).encode('utf-8')
response=urllib.request.urlopen(url,data)
html=response.read().decode('utf-8')

target=json.loads(html)
target=target['translationResult']['tgt']
print(target)

请输入待翻译的内容:小甲鱼
Traceback (most recent call last):
File "C:\Users\zhangyuanli\Desktop\translation.py", line 26, in <module>
    target=target['translationResult']['tgt']
KeyError: 'translationResult'
>>>

学程序的LILI 发表于 2018-9-26 11:35:31

import urllib.request
import urllib.parse
import json

content=input('请输入待翻译的内容:')
url="http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
head={}
head['User-Agent']='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4882.400 QQBrowser/9.7.13036.400'
data={}
data['i']=content
data['from']='AUTO'
data['to']='AUTO'
data['smartresult']='dict'
data['client']='fanyideskweb'
data['salt']='1537863642103'
data['sign']='b9e371ea87a15b977e2202c10f6cc272'
data['doctype']='json'
data['version']='2.1'
data['keyfrom']='fanyi.web'
data['action']='FY_BY_CLICKBUTTION'
data['typoResult']='false'

data=urllib.parse.urlencode(data).encode('utf-8')
req=urllib.request.Request(url,data,head)
response=urllib.request.urlopen(req)
html=response.read().decode('utf-8')

target=json.loads(html)
target=target['translateResult']['tgt']
print(target)



============ RESTART: C:\Users\zhangyuanli\Desktop\translation.py ============
请输入待翻译的内容:love
Traceback (most recent call last):
File "C:\Users\zhangyuanli\Desktop\translation.py", line 29, in <module>
    target=target['translateResult']['tgt']
KeyError: 'translateResult'
>>>


学程序的LILI 发表于 2018-9-26 16:00:34

>>>
============= RESTART: C:\Users\zhangyuanli\Desktop\proxy_eg.py =============
Traceback (most recent call last):
File "C:\Users\zhangyuanli\Anaconda3\lib\urllib\request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
File "C:\Users\zhangyuanli\Anaconda3\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\zhangyuanli\Anaconda3\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\zhangyuanli\Anaconda3\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\zhangyuanli\Anaconda3\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
File "C:\Users\zhangyuanli\Anaconda3\lib\http\client.py", line 964, in send
    self.connect()
File "C:\Users\zhangyuanli\Anaconda3\lib\http\client.py", line 936, in connect
    (self.host,self.port), self.timeout, self.source_address)
File "C:\Users\zhangyuanli\Anaconda3\lib\socket.py", line 704, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\zhangyuanli\Anaconda3\lib\socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\zhangyuanli\Desktop\proxy_eg.py", line 15, in <module>
    response=urllib.request.urlopen(url)
File "C:\Users\zhangyuanli\Anaconda3\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
File "C:\Users\zhangyuanli\Anaconda3\lib\urllib\request.py", line 526, in open
    response = self._open(req, data)
File "C:\Users\zhangyuanli\Anaconda3\lib\urllib\request.py", line 544, in _open
    '_open', req)
File "C:\Users\zhangyuanli\Anaconda3\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
File "C:\Users\zhangyuanli\Anaconda3\lib\urllib\request.py", line 1346, in http_open
    return self.do_open(http.client.HTTPConnection, req)
File "C:\Users\zhangyuanli\Anaconda3\lib\urllib\request.py", line 1320, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error getaddrinfo failed>

caigen001 发表于 2018-9-27 20:56:26

又学到了 支持

leyanqz 发表于 2018-11-6 11:04:16

谢谢楼主的分享!

leyanqz 发表于 2018-11-6 11:05:21

旋转风 发表于 2015-12-10 14:34
看一些其他资料介绍,现在的Python3.X开始用字符串类型(str)来代替Unicode类型了,省去了不少编码问题:lo ...

字符串在python的内部表示就是Unicode编码

东子子 发表于 2018-12-19 09:00:11

http://wx1.sinaimg.cn/mw690/9e407d97gy1fybrx2oxmbj20fw0ek41e.jpg    请问大神们,哪里出错了,我打印出来总是提示translateresult error

Lotus无忧 发表于 2018-12-30 23:06:41

收下了

loveky 发表于 2018-12-31 10:38:00

非常总要的知识

黑色四叶草 发表于 2019-2-2 07:41:37

解决了我的困惑

denny1984 发表于 2019-6-11 16:47:02

学习收藏了

cydd1412 发表于 2019-7-7 17:39:08

Python3都不用这些转换,直接可以显示!!
页: 1 2 3 [4] 5 6
查看完整版本: Python编码问题的解决方案总结