BeautifulSoup findall with class attribute- unicode encode error?

Because BeautifulSoup works internally with unicode strings. Printing unicode strings to the console will cause Python to try the conversion of unicode to the default encoding of Python which is usually ascii. This will in general fail for non-ascii web-site.

You may learn the basics about Python and Unicode by googling for "python + unicode". Meanwhile convert your unicode strings to utf-8 using print some_unicode_string. Decode('utf-8').

1 You want . Encode('utf-8') to convert from a Unicode string to a UTF-8 encoded string. – Alex Apr 21 at 16:27.

It works fine, what's broken is the output. Either explicitly encode to your console's charset, or find a different way to run your code (e.g. , from within IDLE).

One thing to note about your code is that findAll returns a list (in this case a list of BeautifulSoup objects) and you just want the titles. You might want to use find instead. And rather than printing out a list of the BeautifulSoup objects, you say that you just want the titles.

The following works fine, for example: import urllib2 from BeautifulSoup import BeautifulSoup HN_url = "news.ycombinator.com" def get_page(): page_html = urllib2. Urlopen(HN_url) return page_html def get_stories(content): soup = BeautifulSoup(content) titles = for td in soup. FindAll("td", { "class":"title" }): a_element = td.

Find("a") if a_element: titles. Append(a_element. String) return titles print get_stories(get_page()) So now get_stories() returns a list of unicode objects, which prints out as you'd expect.

Because BeautifulSoup works internally with unicode strings. Printing unicode strings to the console will cause Python to try the conversion of unicode to the default encoding of Python which is usually ascii. This will in general fail for non-ascii web-site.

You may learn the basics about Python and Unicode by googling for "python + unicode".

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions