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 6252757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:51:52+00:00 2026-05-24T13:51:52+00:00

I am running the following code trying to find particular information in some HTML.

  • 0

I am running the following code trying to find particular information in some HTML. I am having an encoding/decoding problem, however, that I cannot resolve.

import urllib
req = urllib.urlopen('http://securities.stanford.edu/1046/AAI00_01/')
html = req.read()
type(html)
#   <type 'str'>
html.upper().find('HTML')
#   -1
print html[0:20]
#   ??<HTML><HE
html[0:10]
#   '\xff\xfe<\x00H\x00T\x00M\x00'
req.headers['content-type']
#   'text/html'
html = html.encode('utf-8')
#   Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#   UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

What is the solution to this problem? All I need to do is scrape some information from the page using .find and regular expressions.

I am using Mac OSX and running Python 2.6.1 from within Terminal.

  • 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-24T13:51:53+00:00Added an answer on May 24, 2026 at 1:51 pm

    If you’re trying to convert from the str you have to a unicode, you want to use html.decode, not encode.

    Older, bad advice: Also, since you seem to have a BOM at the beginning there, you probably
    want to use 'utf_8_sig' as the encoding, which will strip the BOM on decode.

    New, better advice: Actually, from seeing all those \x00‘s in the output along with the BOM, it looks more like the encoding is actually UTF-16, not UTF-8. So, html.decode('utf-16') should be the way to go.

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

Sidebar

Related Questions

I had no problem at all running the following code on a local server,
I'm running the following code to trace the tagName of every control that gets
I'm trying to figure out a Java regex problem that I'm having. This is
Running the following code, I get a StackOverflowError at the getPackage() line. How can
When running the following code it leaves out one row. When I do a
When running the following code I get no output but I cannot work out
I am running following PHP code to interact with a MS Access database. $odbc_con
I am currently running the following code based on Chapter 12.5 of the Python
I'm running the following code to get a random entry from a dictionary: SELECT
I'm running the following code to update the database according to the data I

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.