environment
OS:w7
PY:python3.5
IDE:Pycharm
moudle:requestsProblem
Error content:
When crawling the content of the web page and storing it in the txt document, I encountered a problem, which has not been solved after google for a long time. I have to say that the coding problem is really a headache (especially using py2, it is even more painful)
analyze
Common coding problems
①: For example, A is converted into B, if A is not unicode type, first decode to unicode, and then convert to B type, unicode is like a bridge.
②: But there is another situation, although A is of unicode type, but because A data contains some special symbols, such as →, when this symbol is encoded as GBK, if it is not recognized, the above error will be reported.
When saving to the local txt, in the w7 system, the default encoding of the newly created file is: gbk. The second situation occurs.
Solution
with open(filepath, 'a+',encoding='utf-8') as fp #因为utf-8编码是可变长编码,可以识别任何的字符,所以就不会出现以上错误了。