Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeautifulSoup scrape itemprop="name" in Python

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

like image 405
Harry Avatar asked Dec 23 '22 20:12

Harry


1 Answers

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.

like image 140
Zroq Avatar answered Dec 31 '22 13:12

Zroq