algorhythm
algorhythm

Reputation: 3446

BeautifulSoup getText throwing an error

I'm trying to read the text from a web URL using the following code to store all valid strings to a variable which I can manipulate later. I am getting an error at run time though

from bs4 import BeautifulSoup
import urllib.request
from django.template.defaultfilters import title

response = urllib.request.urlopen('http://www.scotland.org/about-scotland/facts-about-scotland/')
data = response.read()
soup = BeautifulSoup(data)

textString = soup.findAll('p').getText()
print(textString)

error:

textString = soup.findAll('p').getText()
AttributeError: 'ResultSet' object has no attribute 'getText'

Upvotes: 0

Views: 918

Answers (2)

algorhythm
algorhythm

Reputation: 3446

I got a work around. I guess the same idea as your edit, thanks!

textString = ""
for i in soup.find_all('p'):
    textString += i.getText()

print(textString)

Upvotes: 0

Hasan Ramezani
Hasan Ramezani

Reputation: 5194

Try this:

textString = soup.findAll('p')[0].getText()

And if you want to get all paragraph data try this:

elements = soup.findAll('p')
for paragraph in elements:
    print paragraph.getText()

Upvotes: 2

Related Questions