这是我的代码,我不能在“标题、成分、说明、营养、图像、链接”中添加值。
from recipe_scrapers import scrape_me
import requests
from recipe_scrapers import scrape_html
from csv import writer
with open('recipe.csv', 'w', encoding='utf8', newline='') as file:
#create new CSV file and write header that name Title ,Ingredients,instructions,nutrients,Image,link.
thewriter = writer(file)
header = ['Title', 'Ingredients', 'Instructions', 'Nutrition_Facts','image','links']
thewriter.writerow(header)
url = "https://www.allrecipes.com/recipe/220751/quick-chicken-piccata/"
html = requests.get(url).content
scraper = scrape_html(html=html, org_url=url)
for scrap in scraper:
#this loop add Title ,Ingredients,instructions,nutrients,Image,link value .
info = ['title, Ingredients, instructions, nutrients,Image,link']
thewriter.writerow(info)
Title = scraper.title()
Ingredients = scraper.ingredients()
instructions = scraper.instructions()
nutrients = scraper.nutrients()
Image = scraper.image()
link = scraper.links()
print(scrap)我如何解决这段代码
发布于 2022-09-03 06:01:57
您的代码有许多问题。首先,你的压痕脱落了。您正在不同的代码块中创建thewriter变量,然后尝试在不同的代码块中访问它。要解决这个问题,您必须将with open语句下面的所有代码缩进相同的级别。
其次,根据食谱刮刀医生,scraper是一个不能迭代的AllRecipesCurated对象,所以您的行:
for scrap in scraper:这是没有意义的,因为您试图迭代一个不可迭代的对象,并将给您一个错误。
最后,这两行:
info = ['title, Ingredients, instructions, nutrients,Image,link']
thewriter.writerow(info)这意味着您将始终将标题写入您的文件,而不是从调用URL中获得的数据。相反,应该让它指向从url中提取的数据:
thewriter.writerow([scraper.title(), scraper.ingredients(), scraper.instructions(), scraper.nutrients(), scraper.image(), scraper.links()])这是固定的完整代码。您应该能够使用它获得正确的结果:
import requests
from recipe_scrapers import scrape_html
from csv import writer
with open('recipe.csv', 'w', encoding='utf8', newline='') as file:
# create new CSV file and write header that name Title ,Ingredients,instructions,nutrients,Image,link.
thewriter = writer(file)
header = ['Title', 'Ingredients', 'Instructions', 'Nutrition_Facts', 'image', 'links']
thewriter.writerow(header)
url = "https://www.allrecipes.com/recipe/220751/quick-chicken-piccata/"
html = requests.get(url).content
scraper = scrape_html(html=html, org_url=url)
thewriter.writerow([scraper.title(), scraper.ingredients(), scraper.instructions(), scraper.nutrients(), scraper.image(), scraper.links()])https://stackoverflow.com/questions/73589924
复制相似问题