Unicodeencodeerror: ' GBK ' codec can ' t encode character ' \xbb ' in position

Source: Internet
Author: User

Python implementation crawler encounters coding problems:

Error:unicodeencodeerror: ' GBK ' codec can ' t encode character ' \xxx ' in position XX

WORKAROUND: Change the standard output

 fromUrllibImportRequestimport Ioimport syssys.stdout = io. Textiowrapper (sys.stdout.buffer,encoding= ' gb18030 ') #改变标准输出的默认编码Req=request. Request ('http://www.baidu.com') Req.add_header ('user-agent','mozilla/5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/61.0.3163.100 safari/537.36') Resp=Request.urlopen (req)Print(Resp.read (). Decode ('UTF-8'))

Add the code in the page labeled red to

Ps:

1.str Turn bytes called encode,bytes turn str called decode

2. Commonly used Chinese code name

Reference article: http://blog.csdn.net/jim7424994/article/details/22675759




Unicodeencodeerror: ' GBK ' codec can ' t encode character ' \xbb ' in position

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.