2

I have a html file with russian text. How i can get all words in text without html tags, special symbols, etc ?

Example:

<html>...<body>...<div id='text'>Foo bar! Foo, bar.</div></body></html>

I need:

['foo','bar','Foo','bar']

I tried nltk, but it does not support russian words.

rushter
  • 21
  • 3

4 Answers4

4

Definitely try BeautifulSoup, it supports Unicode.

UncleZeiv
  • 18,272
  • 7
  • 49
  • 77
  • 1
    While I support this answer (+1), I feel that I should warn that BeautifulSoup is basically deprecated. I've used it before and love it, but there isn't as much official support for it anymore – inspectorG4dget Feb 10 '12 at 16:09
  • Not true -- as of yesterday, BeautifulSoup 4 is out in beta. – grifaton Feb 10 '12 at 16:26
4

I'm using lxml library to parse xml/html. lxml works good with any unicode data.

0

Use lxml. It can strip tags, elements, and more:

import urllib2

from lxml import etree


URL = 'http://stackoverflow.com/questions/9230675/python-html-processing'

html = urllib2.urlopen(URL).read()
tree = etree.fromstring(html, parser=etree.HTMLParser())

tree.xpath('//script')
# [<Element script at 102f831b0>,
#  ...
#  <Element script at 102f83ba8>]

tree.xpath('//style')
# [<Element style at 102f83c58>]

tags_to_strip = ['script', 'style']
etree.strip_elements(tree, *tags_to_strip)

tree.xpath('//style')
# []

tree.xpath('//script')
# []

body = tree.xpath('//body')
body = body[0]

text = ' '.join(body.itertext())
tokens = text.split()
# [u'Stack',
#  u'Exchange',
#  u'log',
#  u'in',
#  ...
#  u'Stack',
#  u'Overflow',
#  u'works',
#  u'best',
#  u'with',
#  u'JavaScript',
#  u'enabled']

In case of text in russian you get tokens looking likes this:

# [u'\xd1\x8d\xd1\x84\xd1\x84\xd0\xb5\xd0\xba\xd1\x82\xd1\x8b\xe2\x80\xa6',
#  u'\xd0\x9c\xd0\xb0\xd1\x80\xd0\xba',
#  ...
#  u'\xd0\x9c\xd0\xb0\xd0\xb9\xd0\xb5\xd1\x80']

Errors handling is your home assignment.

Misha Akovantsev
  • 1,817
  • 13
  • 14
0

Use regex to remove the tags. Nltk is all about language analysis (nouns vs verbs) and word meaning (semantics) not string removal and pattern matching although I can see how someoneaybe confused.

Here is a removal function using regex

import re
def remove_html_tags(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)
Matt Alcock
  • 12,399
  • 14
  • 45
  • 61