Search code examples
pythonweb-scrapingbeautifulsoup

How do I stop each letter from printing on different line?


When I try to scrape some text with beautifulsoup

class scrape(object):
    
        def dirae(self, word):
            url = 'http://dirae.es/palabras/' + word
            site = urllib2.urlopen(url)
            soup = BeautifulSoup(site.read())
            for result in soup.select('div.definitionContent')[0].get_text():
                print(result.encode('utf-8'))
    
    search = scrape()
    search.dirae('bellota')

Example of the html code:

 <div class="definitionContent">
    <li><p>Text</p></li>
    <li><p>Text</p></li>
 </div>

I get:

T
e
x
t

T
e
x
t

I want to get the output on the same line.


Solution

  • soup.select('div.definitionContent')[0].get_text() is returning a string. So doing a for on it means you are iterating in the characters.

    You can try do like this:

    class scrape(object):
    
        def dirae(self, word):
            url = 'http://dirae.es/palabras/' + word
            site = urllib2.urlopen(url)
            soup = BeautifulSoup(site.read())
            print soup.select('div.definitionContent')[0].get_text().enconde('utf-8')