2 回答

TA貢獻1806條經驗 獲得超8個贊
嘗試這個,
import requests
from bs4 import BeautifulSoup
resp = requests.get("https://www.simplyrecipes.com/recipes/egg_salad_sandwich/")
soup = BeautifulSoup(resp.text, "html.parser")
div_ = soup.find("div", attrs={"class": "recipe-callout"})
recipes = {"_".join(div_.find("h2").text.split()):
[x.text for x in div_.findAll("li", attrs={"class": "ingredient"})]}

TA貢獻1966條經驗 獲得超4個贊
聽起來你的代碼應該在下面,在我刪除了不必要的h3檢索之后
executable_path = {'executable_path': ChromeDriverManager().install()}
browser = Browser('chrome', **executable_path)
webpage_url = 'https://www.simplyrecipes.com/recipes/egg_salad_sandwich/'
browser.visit(webpage_url)
time.sleep(1)
website_html = browser.html
website_soup = BeautifulSoup(website_html, 'html.parser')
ingredientsList = website_soup.find('li', class_ = "ingredient")
print({ingredients})
您正在嘗試查找具有不存在的類名的h3元素Ingredients
添加回答
舉報