Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 904955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:08:10+00:00 2026-05-15T16:08:10+00:00

I want to get html content from a url and parse the html content

  • 0

I want to get html content from a url and parse the html content with regular expression.
But the html content has some multibyte characters. So I met the error described in title.

Could somebody tell me how to resolve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:08:10+00:00Added an answer on May 15, 2026 at 4:08 pm

    You need to edit your question to show (1) the code that you used (2) the full error and traceback (3) the url that is involved (4) what is the unicode character that you are trying to encode as gbk

    You seem to have somehow obtained unicode characters from the raw bytes in the the html content — how? what encoding is specified in the html content?

    Then (I guess) you are trying to write the unicode characters to a file, endcoding the unicode as gbk. During this process, you got an error something like this:

    >>> u'\uffff'.encode('gbk')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    UnicodeEncodeError: 'gbk' codec can't encode character u'\uffff' in position 0: illegal multibyte sequence
    >>>
    

    If the raw bytes in the html content were not encoded in gbk, then it is quite possible that you have some unicode characters that can’t be represented in gbk. In that case you may like to encode your results using the original encoding, or encode them in gb18030 which can take any unicode character.

    Another possibility is that you have mangled the raw bytes or the unicode somehow. I certainly hope that your regex machinations have been done on the unicode and not on some variable-length-character encoding like gb2312, gbk, etc.

    Update:

    Here is your code snippet:

    import sys, urllib.request
    url = "http://www.meilishuo.com"
    wp = urllib.request.urlopen(url)
    content = wp.read()
    str_content = content.decode('utf-8')
    fp = open("web.txt","w")
    fp.write(str_content)
    fp.close() 
    

    From that I’ve had to deduce:
    (1) You are running Python 3.x
    (2) sys.defaultencoding == “gbk” — otherwise you wouldn’t have got the error message some part of which you reported earlier.

    As my sys.defaultencoding is NOT ‘gbk’, I replaced your last 3 lines withgbk_content = str_content.encode('gbk') and ran the amended snippet with Python 3.1.2.

    Observations:

    (1) website has charset=utf-8, decodes OK with utf-8
    (2) Error message: UnicodeEncodeError: 'gbk' codec can't encode character '\u2764' in position 35070: illegal multibyte sequence

    \u2664 is a dingbat (HEAVY BLACK HEART). The website is dynamic; in another attempt, the first offending character was \xa9 (COPYRIGHT SIGN).

    So the web page contains Unicode characters which are not mapped in gbk. Options are

    (1) encode with ‘gbk’ but use the ‘replace’ option
    (2) encode with ‘gbk’ but use the ‘ignore’ option
    (3) encode with an encoding that supports ALL Unicode characters (utf-8, gb18030) and for which you have a display mechanism that renders all those characters that aren’t in gbk

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to fetch some content from a webserver using Net:HTTP, like this: url
My UIWebView loads a contenteditable html content. I want to let the UIWebView get
I want to get images from a flickr rss but this code won't get
I want to insert some value from first and second foreach into database, but
I am using Mechanize gem to parse html content. Firing this on terminal, agent.get(http://www.example.com/).search(.sidebar-deal-excerpt).first
I want to get html page and parse it. I send to http://gdata.youtube.com/feeds/api/videos?q=u_dAYLIG984 NSURL*
I want to get html tags.For ex www.google.com. When I enter this site with
I want to get HTML source of select text in div and alert that.
I want to get the html including the selector that I am using to
i want to get the html Source code of website, it something like: wb.Document...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.