亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Web 抓取 - 挑戰在我的代碼中闡明層次結構

Web 抓取 - 挑戰在我的代碼中闡明層次結構

達令說 2023-05-09 11:00:46
目標:我正在嘗試抓取 100 多個網頁,特別是每個網頁的配方成分。如果我們舉一個例子——其中包含雞蛋三明治 ( url )的食譜,我為此使用了許多 Python 依賴項,包括BeautifulSoup, splinter.Browser, ChromeDrivermanager.預期輸出:一旦我收集了成分,我想將它們保存在字典中。下面的例子 -recipes = {"quick_and_easy_egg_salad_sandwich_recipe":['1-2 tablespoons mayonnaise (to taste)', '2 tablespoons chopped celery', '2 slices white, wheat, multigrain, or rye bread, toasted or plain']我取得的成就:1. 我已經能夠“粗略地”確定(通過 Web Inspector)我需要關注什么—— 看起來每種成分都有它自己的,但看起來我要么誤解了層次結構,要么誤解了我的代碼是不正確的。<li class='ingredient'>2.我的代碼如下-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.htmlwebsite_soup = BeautifulSoup(website_html, 'html.parser')ingredients = website_soup.find('h3', class_="Ingredients")ingredientsList = ingredients.find('li', class_ = "ingredient")print({ingredients})當我嘗試打印時,{ingredients}我得到一個AttributeError: 'NoneType' object has no attribute 'find'我知道我的代碼有缺陷的消息,但是我只是不知道如何解決這個問題,想知道是否有人有任何建議?
查看完整描述

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"})]}


查看完整回答
反對 回復 2023-05-09
?
慕標5832272

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


查看完整回答
反對 回復 2023-05-09
  • 2 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號