Skip to content
Advertisement

BeautifulSoup Web Scraping to find values of a specific key within the result set

I am scraping a webpage using beautiful soup:

JavaScript

<class ‘bs4.element.Tag’> , 1

JavaScript

0

Here is what ‘result’ looks like:

>>>print(result)

I am unable to access recipeIngredient (highlighted in the image) as a dictionary keys. It gives me a keyerror.

JavaScript

KeyError: ‘recipeIngredient’

How can I do this? I want to extract this from ‘result’:

“recipeIngredient”:[“1 pound fresh, best-quality mozzarella (preferably buffalo milk)”,”4 medium heirloom tomatoes”,”1 bunch fresh basil, leaves only, some reserved for garnish”,”Flaky sea salt, such as Maldon”,”Coarsely ground black pepper”,”High-quality extra-virgin olive oil”]

Advertisement

Answer

You would need to convert the data inside the script tag to json using json.loads. In order to get the data inside the script tag use .get_text method

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement