首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用python在csv文件头中附加多个值

如何用python在csv文件头中附加多个值
EN

Stack Overflow用户
提问于 2022-09-03 05:42:16
回答 1查看 38关注 0票数 -2

这是我的代码,我不能在“标题、成分、说明、营养、图像、链接”中添加值。

代码语言:javascript
复制
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)

我如何解决这段代码

EN

回答 1

Stack Overflow用户

发布于 2022-09-03 06:01:57

您的代码有许多问题。首先,你的压痕脱落了。您正在不同的代码块中创建thewriter变量,然后尝试在不同的代码块中访问它。要解决这个问题,您必须将with open语句下面的所有代码缩进相同的级别。

其次,根据食谱刮刀医生scraper是一个不能迭代的AllRecipesCurated对象,所以您的行:

代码语言:javascript
复制
for scrap in scraper:

这是没有意义的,因为您试图迭代一个不可迭代的对象,并将给您一个错误。

最后,这两行:

代码语言:javascript
复制
info = ['title, Ingredients, instructions, nutrients,Image,link']
thewriter.writerow(info)

这意味着您将始终将标题写入您的文件,而不是从调用URL中获得的数据。相反,应该让它指向从url中提取的数据:

代码语言:javascript
复制
thewriter.writerow([scraper.title(), scraper.ingredients(), scraper.instructions(), scraper.nutrients(), scraper.image(), scraper.links()])

这是固定的完整代码。您应该能够使用它获得正确的结果:

代码语言:javascript
复制
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()])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73589924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档