Lawase
Lawase

Reputation: 23

Python Web Scraping - Trying To Extract Text

from bs4 import BeautifulSoup
import requests

a = 0
while a == 0:
   word = input("What word do you want to know? ")

    url = "http://dictionary.cambridge.org/dictionary/british/" + word.lower()
    r = requests.get(url)

    soup = BeautifulSoup(r.content)

    word = soup.find("span", {"class": "pos"})
    definition = soup.find("span", {"class": "def"})

    for a in word:
            print (a)
    for b in definition:
            print (b)

I'm trying to make a basic dictionary program just as a beginner to web scraping in python. The problem is I'm trying to extract the definition of a word but I cant figure out how to remove the tags and have the definition in a readable state.

Above is the code I've wrote so far which when printing b, just print a mess of tags which does have the text I'm looking for, but just not correctly displayed. Could someone give me some tips, would be much appreciated.

p.s. I'm new to this site and programming, so be nice please

Upvotes: 2

Views: 2000

Answers (1)

alecxe
alecxe

Reputation: 474041

You've correctly found the appropriate tags. Now, just get the .text:

word = soup.find("span", {"class": "pos"}).text
definition = soup.find("span", {"class": "def"}).text

print(word)
print(definition)

For a python input, it prints:

noun
a very large snake that kills animals for food by wrapping itself around them and crushing them

Upvotes: 2

Related Questions