0

I was recently trying out the following Python code using BeautifulSoup from this question, which seems to have worked for the question-asker.

import urllib2
import bs4
import string
from bs4 import BeautifulSoup

badwords = set([
    'cup','cups',
    'clove','cloves',
    'tsp','teaspoon','teaspoons',
    'tbsp','tablespoon','tablespoons',
    'minced'
])

def cleanIngred(s):

    s=s.strip()

    s=s.strip(string.digits + string.punctuation)

    return ' '.join(word for word in s.split() if not word in badwords)

def cleanIngred(s):
    # remove leading and trailing whitespace
    s = s.strip()
    # remove numbers and punctuation in the string
    s = s.strip(string.digits + string.punctuation)
    # remove unwanted words
    return ' '.join(word for word in s.split() if not word in badwords)

def main():
    url = "http://allrecipes.com/Recipe/Slow-Cooker-Pork-Chops-II/Detail.aspx"
    data = urllib2.urlopen(url).read()
    bs = BeautifulSoup.BeautifulSoup(data)

    ingreds = bs.find('div', {'class': 'ingredients'})
    ingreds = [cleanIngred(s.getText()) for s in ingreds.findAll('li')]

    fname = 'PorkRecipe.txt'
    with open(fname, 'w') as outf:
        outf.write('\n'.join(ingreds))

if __name__=="__main__":
    main()

I can't get it to work in my case though for some reason. I receive the error:

AttributeError                            Traceback (most recent call last)
<ipython-input-4-55411b0c5016> in <module>()
     41 
     42 if __name__=="__main__":
---> 43     main()

<ipython-input-4-55411b0c5016> in main()
     31     url = "http://allrecipes.com/Recipe/Slow-Cooker-Pork-Chops-II/Detail.aspx"
     32     data = urllib2.urlopen(url).read()
---> 33     bs = BeautifulSoup.BeautifulSoup(data)
     34 
     35     ingreds = bs.find('div', {'class': 'ingredients'})

AttributeError: type object 'BeautifulSoup' has no attribute 'BeautifulSoup'

I suspect this is because I'm using bs4 and not BeautifulSoup. I tried replacing the line bs = BeautifulSoup.BeautifulSoup(data) with bs = bs4.BeautifulSoup(data) and no longer receive an error, but get no output. Are there too many possible causes for this to guess?

1
  • 2
    They import BeautifulSoup, you from bs4 import BeautifulSoup. You should use bs = BeautifulSoup(data), or import bs4 then bs = bs4.BeautifulSoup(data). Commented Sep 23, 2014 at 15:34

1 Answer 1

1

The original code used BeautifulSoup version 3:

import BeautifulSoup

You switched to BeautifulSoup version 4, but also switched the style of the import:

from bs4 import BeautifulSoup

Either remove that line; you already have the correct import earlier in your file:

import bs4

and then use:

bs = bs4.BeautifulSoup(data)

or change that latter line to:

bs = BeautifulSoup(data)

(and remove the import bs4 line).

You may also want to review the Porting code to BS4 section of the BeautifulSoup documentation, so you can make any other necessary changes upgrading the code you found to get the best out of BeautifulSoup version 4.

The script otherwise works just fine and produces a new file, PorkRecipe.txt, it doesn't produce output on stdout.

The contents of the file after fixing the bs4.BeautifulSoup reference:

READY IN 4+ hrs

Slow Cooker Pork Chops II

Amazing Pork Tenderloin in the Slow Cooker

Jerre's Black Bean and Pork Slow Cooker Chili

Slow Cooker Pulled Pork

Slow Cooker Sauerkraut Pork Loin

Slow Cooker Texas Pulled Pork

Oven-Fried Pork Chops

Pork Chops for the Slow Cooker

Tangy Slow Cooker Pork Roast

Types of Cooking Oil

Garlic: Fresh Vs. Powdered

All about Paprika

Types of Salt
olive oil
chicken broth
garlic,
paprika
garlic powder
poultry seasoning
dried oregano
dried basil
thick cut boneless pork chops
salt and pepper to taste
PREP 10 mins
COOK 4 hrs
READY IN 4 hrs 10 mins
In a large bowl, whisk together the olive oil, chicken broth, garlic, paprika, garlic powder, poultry seasoning, oregano, and basil. Pour into the slow cooker. Cut small slits in each pork chop with the tip of a knife, and season lightly with salt and pepper. Place pork chops into the slow cooker, cover, and cook on High for 4 hours. Baste periodically with the sauce
Sign up to request clarification or add additional context in comments.

2 Comments

@MaxPower: the script works otherwise; you need to check that the file is produced, not if there is output on the console.
@Martjin Thanks so much, I should be more careful when using different versions!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.