import requests
from bs4 import BeautifulSoup
import json
from util import parse_money


SANDWICHES_URL = "https://www.ugent.be/student/nl/meer-dan-studeren/resto/broodjes/overzicht.htm"
OUTFILE = "resto/2.0/sandwiches.json"
HTML_PARSER = 'lxml'


def parse_ingredients(ingredients):
    return [ingredient.strip().lower()
            for ingredient
            in columns[1].string.replace(' en ', ',').split(',')]


if __name__ == "__main__":
    r = requests.get(SANDWICHES_URL)
    soup = BeautifulSoup(r.text, HTML_PARSER)
    sandwiches = []

    for row in soup.table.find_all("tr", class_=lambda x: x != 'tabelheader'):
        columns = row.find_all("td")
        sandwiches.append({
            "name": columns[0].find(text=True),
            "ingredients": parse_ingredients(columns[1].string),
            "price_small": parse_money(columns[2].string),
            "price_medium": parse_money(''.join(columns[3].findAll(text=True)))  # workaround
        })
    with open(OUTFILE, 'w') as outfile:
        json.dump(sandwiches, outfile, sort_keys=True, indent=4, separators=(',', ': '))
@@ -1,12 +1,38 @@
+import argparse%0Aimport os%0A
 import reque
@@ -69,19 +69,8 @@
 oup%0A
-import json
 %0Afro
@@ -94,17 +94,36 @@
 se_money
-%0A
+, write_json_to_file
 %0A%0ASANDWI
@@ -218,46 +218,8 @@
 tm%22%0A
-OUTFILE = %22resto/2.0/sandwiches.json%22%0A
 HTML
@@ -259,16 +259,25 @@
 edients(
+columns, 
 ingredie
@@ -420,34 +420,120 @@
 %5D%0A%0A%0A
-if __name__ == %22__main__%22:
+def main(output):%0A    %22%22%22%0A    Parse sandwiches from the menu.%0A    :param output: The root output folder.%0A    %22%22%22
 %0A   
@@ -866,24 +866,33 @@
 ingredients(
+columns, 
 columns%5B1%5D.s
@@ -1067,132 +1067,542 @@
  %7D)%0A
+%0A
     
-with open(OUTFILE, 'w') as outfile:%0A        json.dump(sandwiches, outfile, sort_keys=True, indent=4, separators=(',', ': '))
+output_file = os.path.join(output, %22sandwiches.json%22)%0A    write_json_to_file(sandwiches, output_file)%0A%0A%0Aif __name__ == %22__main__%22:%0A    parser = argparse.ArgumentParser(description='Run sandwich scraper')%0A    parser.add_argument('output',%0A                        help='Path of the folder in which the output must be written. Will be created if needed.')%0A    args = parser.parse_args()%0A%0A    output_path = os.path.abspath(args.output)  # Like realpath%0A    os.makedirs(output_path, exist_ok=True)  # Like mkdir -p%0A%0A    main(output_path)%0A

Modify sandwich scraper