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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:59:41+00:00 2026-06-16T05:59:41+00:00

I’m extremely confused over unicode in Python 2.x. I’m using BeautifulSoup to scrape a

  • 0

I’m extremely confused over unicode in Python 2.x.

I’m using BeautifulSoup to scrape a webpage, and I’m trying to insert the things I find into a dictionary with the name as the key, and the url as the value.

I’m using BeautifulSoup’s find function to get the info I need. My code started out as follows:

name = i.find('a').string
url = i.find('a').get('href')

This works, with the exception of the thign returned from find is an Object, and not a string.

Here’s were things start confusing me

If I try to convert it to type str before I assign it to the variable, it sometimes throws an UnicodeEncodeError.

'ascii' codec can't encode character u'\xa0' in position 5: ordinal not in range(128)

I Google around and find that I should be encoding to ascii

I try adding:

print str(i.find('a').string).encode('ascii', 'ignore')

No luck, still gives an, Unicode Error.

From there, I tried using repr.

print repr(i.find('a').string)

And that works… almost!

I ran into a new problem here.

Once everything is said and done, and the dictionary is built, I can’t bloody access anything! It keeps giving me a KeyError.

I can loop over the dict:

for i in sorted(data.iterkeys()):
    print i


>>> u'Key1'
>>> u'Key2'
>>> u'Key3'
>>> u'Key4'

but if I try to access an item of the dict like this:

print data['key1']

OR

print data[u'key1']

OR

test = unicode('key1')
print data[test]

They all return KeyErrors, which is 100% confusing to me. I assume it’s got something to do with them being Unicode objects.

I’ve tried just about everything I can come up with, but I can’t figure out what’s going on.

Oh! Adding to the oddity, is that this code:

name = repr(i.find('a').string)
print type(name)

returns

>>> type(str)

but if I just print the thing

print name

it shows it as a unicode string

>>>> u'string name' 
  • 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-06-16T05:59:43+00:00Added an answer on June 16, 2026 at 5:59 am

    The .string value is indeed not a string. You need to cast it to unicode():

    name = unicode(i.find('a').string)
    

    It’s a unicode-like object called NavigableString. If you really need it to be a str instead, you can encode it from there:

    name = unicode(i.find('a').string).encode('utf8')
    

    or similar. For use in a dict I’d use unicode() objects and not encode.

    To understand the difference between unicode() and str() and what encoding to use, I recommend you read the Python Unicode HOWTO.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:

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.