运行BeautifulSoup Python代码时没有输出

运行BeautifulSoup Python代码时没有输出,python,beautifulsoup,Python,Beautifulsoup,最近,我使用BeautifulSoup from尝试了以下Python代码,这似乎对提问者有效 import urllib2 import bs4 import string from bs4 import BeautifulSoup badwords = set([ 'cup','cups', 'clove','cloves', 'tsp','teaspoon','teaspoons', 'tbsp','tablespoon','tablespoons',

最近,我使用BeautifulSoup from尝试了以下Python代码,这似乎对提问者有效

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()
但我不能让它在我的情况下工作,虽然出于某种原因。我收到错误消息:

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'
AttributeError回溯(最近一次调用)
在()
41
42如果名称=“\uuuuu main\uuuuuuuu”:
--->43主要内容()
大体上
31 url=”http://allrecipes.com/Recipe/Slow-Cooker-Pork-Chops-II/Detail.aspx"
32 data=urllib2.urlopen(url.read())
--->33 bs=美化组。美化组(数据)
34
35 ingreds=bs.find('div',{'class':'components'})
AttributeError:类型对象“BeautifulSoup”没有属性“BeautifulSoup”

我怀疑这是因为我使用的是bs4而不是BeautifulSoup。我尝试用
bs=bs4.BeautifulSoup(数据)
替换行
bs=BeautifulSoup.BeautifulSoup(数据)
,不再收到错误,但没有得到输出。是否有太多可能的原因让人无法猜测

使用的原始代码为ULSOUP第3版:

import BeautifulSoup
您切换到BeautifulSoup版本4,但也切换了导入的样式:

from bs4 import BeautifulSoup
要么去掉那条线;您的文件前面已经有正确的导入:

import bs4
然后使用:

bs = bs4.BeautifulSoup(data)
或者将后一行更改为:

bs = BeautifulSoup(data)
(并删除导入bs4的
行)

您可能还需要查看BeautifulSoup文档的详细信息,以便对找到的代码进行任何其他必要的更改,以充分利用BeautifulSoup版本4

否则,脚本工作正常,并生成一个新文件,
PorkRecipe.txt
,它不会在stdout上生成输出

修复
bs4.BeautifulSoup
参考后的文件内容:

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

他们导入美化组
,您从bs4导入美化组。您应该使用
bs=BeautifulSoup(data)
,或者
import bs4
然后
bs=bs4.BeautifulSoup(data)
@MaxPower:否则脚本会起作用;您需要检查文件是否生成,而不是控制台上是否有输出。@Martjin非常感谢,在使用不同版本时,我应该更加小心!