Python writes to file unicodeencodeerror: ' GBK ' codec can ' t encode character ' \xa0 ' in position 23869:

Source: Internet
Author: User

Correct code:

Import Urllib.requesturl = "71025372?utm_source=itdadao&utm_medium=referral"; response = Urllib.request.Request ( Url=url,method= "GET"), result = Urllib.request.urlopen (response), HTML = Result.read (). Decode ("UTF8"); f = open ("x.html "," W ", encoding=" UTF8 "); F.write (HTML); F.close ();

  

1:urllib.request.urlopen (URL). read (); Read content defaults to bytes format

2:open (Filename,open,encode); Open File

Error message:

Unicodeencodeerror: ' GBK ' codec can ' t encode character ' \xa0 ' in position 23869:illegal multibyte sequence

  

How to resolve:

f = open ("X.html", "W", encoding= "UTF8"); Specifies the encoding of the open file or F = open ("x.html", "WB")  opening the file for writing to the binary

  

Try to maintain all encoding consistency when working with files

Python writes to file unicodeencodeerror: ' GBK ' codec can ' t encode character ' \xa0 ' in position 23869:

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.