メニューをスクラップする美味しいスープスクリプトを実行しようとしています。最初に食べ物のリストを取得してから、forループでツリーの上に移動して、食べ物が提供されている食事と食べ物が提供されている食堂を見つけます。次に、情報が辞書に追加され、食べ物が鍵となり、食事と食事会場が価値になります。ここでは、コードがあります:Beautiful Soup AttributeError: 'NoneType'オブジェクトに 'find'属性がありません。
foodDict = {}
foodList = bsObj.findAll("td")
for foodItem in foodList:
print("foodItems: " +foodItem.getText())
meal = foodItem.parent.parent.parent.find("h4").getText().lower()
print("Meal: " +meal)
diningHall = foodItem.parent.parent.parent.parent.parent.parent.find("h2").getText().lower()
s = "-"
seq = (meal, diningHall)
mealAndHall = s.join(seq)
foodDict[foodItem.getText().lower().strip()] = mealAndHall
print(foodDict)
これは、ループの最初の実行を通過するが、それは第二のために行くとき、それはエラーを返します:私は取得していますなぜ
foodItems: Bacon
Meal: breakfast
{'bacon': 'breakfast-chase/duckett'}
foodItems: Hard & Soft Cooked Eggs
Traceback (most recent call last):
File "menuscrape.py", line 24, in <module>
meal = foodItem.parent.parent.parent.find("h4").getText().lower()
AttributeError: 'NoneType' object has no attribute 'find'
誰かがちょうど説明することができますエラー? NoneはfoodItemをタイプしているオブジェクトですか?なぜ私のコードは、最初の実行で必要な情報を取得しますが、それ以降の実行では必要としません。私は完全に理解していない。また、誰かが繰り返しparents.parents.parents.parentsを変更する方法についてのヒントを持っているなら、いいですね...私はまだ学んでいるので、あなたはむしろその情報を保留したいと思っています。前もって感謝します。
編集:ここでは
のソースです:
url = "https://www.smith.edu/diningservices/menu_poc/cbord_menus.php"
response = requests.get(url)
bsObj = BeautifulSoup(response.content, "html.parser")
出力が欲しかっ:
{'bacon': 'breakfast-chase/duckett', 'hard & soft cooked eggs': 'breakfast-chase/duckett', 'fried eggs': 'breakfast-chase/duckett', 'morning glory muffins': 'breakfast-chase/duckett', 'rolled oats': 'breakfast-chase/duckett', 'red grapes': 'breakfast-chase/duckett', 'red grapes': 'breakfast-chase/duckett', 'fresh pineapple': 'breakfast-chase/duckett', 'crudites & dip': 'lunch-chase/duckett', 'vegan pesto pizza': 'lunch-chase-duckett', 'pepperoni pizza': 'lunch-chase/duckett', 'extra cheese pizza': 'lunch-chase/duckett', 'caesar salad': 'lunch-chase/duckett', 'chocolate chip bars': 'lunch-chase/duckett', 'assorted fruit': 'dinner-chase/duckett', 'london broil': 'dinner-chase/duckett', 'vegan mushroom tofu': 'dinner-chase/duckett', 'oven-browned red potatoes': 'dinner-chase/duckett', 'baby carrots w/ parsley': 'dinner-chase/duckett', 'hummingbird cake w/ frosting': 'dinner-chase/duckett'}
POSあなたが望む出力を –
私はコメントを更新しました。 –