BeautifulSoup scrape itemprop="name" in Python

14,228

Change instructions = recipeSoup.find("div", itemprop="name") to instructions = recipeSoup.find("span", itemprop="name") to get the recipe title.

For the instructions you'll have to search for li tags with itemprop=ingredients.

Share:
14,228
Harry
Author by

Harry

Updated on June 15, 2022

Comments

  • Harry
    Harry almost 2 years

    I have some python 3.5 code that I want to scrape part of a web page with but instead of printing "Thick and Chewy Peanut Butter Chocolate Chip Bars" it prints "None". Do you know why? Thanks.

    import requests, bs4
    import tkinter as tk
    from tkinter import *
    import pymysql
    import pymysql.cursors
    
    res = requests.get("http://www.foodnetwork.co.uk/article/traybake-recipes/thick-and-chewy-peanut-butter-chocolate-chip-bars/list-page-2.html")
    res.raise_for_status()
    recipeSoup = bs4.BeautifulSoup(res.text, "html.parser")
    type(recipeSoup)
    instructions = recipeSoup.find("div", itemprop="name")
    try:
        method = str.replace(instructions.get_text(strip=True),". ",".")
        method = str.replace(method, ". ", ".")
        method = (str.replace(method, ".",".\n"))
    except AttributeError:
        print(instructions)
    

    Link to scraped page