1

Everything works when running this code, but the output did not write on the CSV file. My data.txt files contain some links.

import csv
import requests
from recipe_scrapers import scrape_html
from csv import writer

with open('recipe.csv', 'w', encoding='utf8', newline='') as file:
    thewriter = writer(file)
    header = ['Title', 'Ingredients', 'Instructions', 'Nutrition_Facts', 'image', 'links']
    thewriter.writerow(header)

    with open('data.txt', 'r') as inf:
        for line in inf:
            html = requests.get(line).content
            scraper = scrape_html(html=html, org_url=line)
            thewriter.writerow([scraper.title(), scraper.ingredients(), scraper.instructions(), scraper.nutrients(), scraper.image(), scraper.links()])
3
  • 1
    to get the root cause , add try exception to your code Commented Sep 17, 2022 at 15:22
  • The contents of recipe.csv will largely depend on the contents of data.txt. If nothing else, I expect you will see the header line. Can you update your question with a sample of what is in data.txt? Commented Sep 17, 2022 at 15:25
  • 1
    The code, as shown, will induce a ValueError due to the attempt to write to a closed file Commented Sep 17, 2022 at 15:50

2 Answers 2

2

If the indentation of your code is as you show it, you only write the header to the output file, loop through the input, and then only try to write one more line from the last scraper, but even that is after the output file has been closed when you exited the with block.

Indent the last line to the same level as scraper = ... or there's no chance of this working properly. Like this:

with open('recipe.csv', 'w', encoding='utf8', newline='') as file:
    thewriter = writer(file)
    ...

    with open('data.txt', 'r') as inf:
        for line in inf:
            html = requests.get(line).content
            scraper = scrape_html(html=html, org_url=line)
            thewriter.writerow([.....])     # <---- proper indent
Sign up to request clarification or add additional context in comments.

2 Comments

when I run that code with proper indent then it throws an error like that "TypeError: argument of type 'NoneType' is not iterable".please tell me how I can solve this problem.
I see you edited your question to show the corrected code. Well, to fix the problem you should first find out which line in your code is causing it. Best to include the full stack trace -- edit your question and add it there.
0

I would try rearranging the logic so it is more orderly. This could help when debugging your code in the future.

As alexis pointed out your problem was improper indentation causing a scope issue. I would revisit some training material of your choice that speaks on scope in Python.

from csv import writer

import requests
from recipe_scrapers import scrape_html

with open('recipe.csv', 'w', encoding='utf8', newline='') as file:
    thewriter = writer(file)
    thewriter.writerow(['Title', 'Ingredients', 'Instructions', 'Nutrition_Facts', 'image', 'links'])  # Header
    
    with open('data.txt', 'r') as inf:
        for line in inf:
            html = requests.get(line).content
            scraper = scrape_html(html=html, org_url=line)
            
            thewriter.writerow([scraper.title(), scraper.ingredients(), scraper.instructions(), scraper.nutrients(), scraper.image(), scraper.links()])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.