Home > Back-end >  How to search Dictionary.com for a word and ONLY extract the first definition?
How to search Dictionary.com for a word and ONLY extract the first definition?

Time:01-12

This is my code:

import colored
import requests
from bs4 import BeautifulSoup

inputcolor = colored.fg(2)

headers = {
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'GET',
    'Access-Control-Allow-Headers': 'Content-Type',
    'Access-Control-Max-Age': '3600',
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'
    }

url = "https://www.dictionary.com/browse/balance-of-power"
req = requests.get(url, headers)
soup = BeautifulSoup(req.content, 'html.parser')
print(soup.find_all("div", {"value": "1"}))

The reason why I used soup.find_all("div", {"value": "1"}) is because this is where the first result will be in the site code:

<div value="1" ><span  data-term="distribution" data-linkid="nn1ov4">a distribution and opposition of forces among nations such that no single nation is strong enough to assert its will or dominate all the others.</span></div>

The code that I have returns this:

<div value="1" ><span  data-term="distribution" data-linkid="nn1ov4">a distribution and opposition of forces among nations such that no single nation is strong enough to assert its will or dominate all the others.</span></div>

It's close, but it still doesn't print only the definition and nothing else, how could I get it to do that?

CodePudding user response:

find_all returns a list. So you'll have to get the first list item by index 0. Then you can extract the text with get_text():

soup.find_all("div", {"value": "1"})[0].get_text()
  •  Tags:  
  • Related