我正在试图弄清楚如何从failes requests.get
获取正确的english / ascii错误消息。由于某种原因(可能正在使用希伯来语语言环境),我得到一个jibrish unicode错误消息。我可以取消消息并在希伯来语中正确显示它,但我真的更喜欢(出于各种原因)异常错误文本将是英文。
检查Requests Exception source code甚至没有把我引向正确的方向,因为我似乎得到了一个不同的例外:
(10060, some encoded error text,')
这个班级是:<class 'socket.error'>
。
所以我想我们可能会从socket lib查看socket.error
。至少就我解析Exception args而言(我不能直接引发异常,只有在运行应用程序时,我在那里做了一个臭except Exception as e
并解析e.args
和{{1 }})。
编辑:这是我从解析e.message
,e.args
,e.message
e.__class__
字符串编码为'windows-1255'并解码为(希伯来语):
ניסיוןחיבורנכשלמאחרשהצדהמחוברלאהגיבכראוילאחרתקופתזמן,
转换为:“连接尝试失败,因为连接端在一段时间后没有正确响应”
引发异常的代码:
(10060, '\xfe\xfe\xf0\xe9\xf1\xe9\xe5\xef \xe7\xe9\xe1\xe5\xf8 \xf0\xeb\xf9\xec \xee\xe0\xe7\xf8 \xf9\xe4\xf6\xe3 \xe4\xee\xe7\xe5\xe1\xf8 \xec\xe0 \xe4\xe2\xe9\xe1 \xeb\xf8\xe0\xe5\xe9 \xec\xe0\xe7\xf8 \xfa\xf7\xe5\xf4\xfa \xe6\xee\xef,'), message: , class: <class 'socket.error'>
所以:
如何设置语言环境以便请求(如果我的猜测正确,则为底层套接字lib)会返回英语/ Ascii错误消息?
感谢您的帮助!
答案 0 :(得分:0)
您可以尝试os.strerror
,但它可能不会返回任何内容或相同的非英语字符串。
这个硬编码的英语从这里被删除:http://support.microsoft.com/kb/819124
ENGLISH_WINDOWS_SOCKET_MESSAGES = {
10004: "Interrupted function call.",
10013: "Permission denied.",
10014: "Bad address.",
10022: "Invalid argument.",
10024: "Too many open files.",
10035: "Resource temporarily unavailable.",
10036: "Operation now in progress.",
10037: "Operation already in progress.",
10038: "Socket operation on nonsocket.",
10039: "Destination address required.",
10040: "Message too long.",
10041: "Protocol wrong type for socket.",
10042: "Bad protocol option.",
10043: "Protocol not supported.",
10044: "Socket type not supported.",
10045: "Operation not supported.",
10046: "Protocol family not supported.",
10047: "Address family not supported by protocol family.",
10048: "Address already in use.",
10049: "Cannot assign requested address.",
10050: "Network is down.",
10051: "Network is unreachable.",
10052: "Network dropped connection on reset.",
10053: "Software caused connection abort.",
10054: "Connection reset by peer.",
10055: "No buffer space available.",
10056: "Socket is already connected.",
10057: "Socket is not connected.",
10058: "Cannot send after socket shutdown.",
10060: "Connection timed out.",
10061: "Connection refused.",
10064: "Host is down.",
10065: "No route to host.",
10067: "Too many processes.",
10091: "Network subsystem is unavailable.",
10092: "Winsock.dll version out of range.",
10093: "Successful WSAStartup not yet performed.",
10101: "Graceful shutdown in progress.",
10109: "Class type not found.",
11001: "Host not found. No such host is known.",
11002: "Nonauthoritative host not found.",
11003: "This is a nonrecoverable error.",
11004: "Valid name, no data record of requested type.",
}
(对于未来的抓取工具:使用了以下xpath://div[@id="MT1"]//h3
和following-sibling::ul/li
)