JohnPaul JohnPaul - 3 months ago 15
Python Question

Python BeautifulSoup extracting text from result

I am trying to get the text from contents but when i try beautiful soup functions on the result variable it results in errors.

from bs4 import BeautifulSoup as bs
import requests

webpage = 'http://www.dictionary.com/browse/coypu'
r = requests.get(webpage)
page_text = r.text

soup = bs(page_text, 'html.parser')

result = soup.find_all('meta', attrs={'name':'description'})

print (result.get['contents'])


I am trying to get the result to read;

"Coypu definition, a large, South American, aquatic rodent, Myocastor (or Myopotamus) coypus, yielding the fur nutria. See more."

Answer

soup.find_all() returns a list. Since in your case, it returns only one element in the list, you can do:

>>> type(result)
<class 'bs4.element.ResultSet'>
>>> type(result[0])
<class 'bs4.element.ResultSet'>
>>> result[0].get('content')
Coypu definition, a large, South American, aquatic rodent, Myocastor (or Myopotamus) coypus, yielding the fur nutria. See more.
Comments