使用模板和 JSON 作为数据输出(符合 AMP 标准)HTML 的好方法是什么?我有一个不错的小python脚本:import requestsimport jsonfrom bs4 import BeautifulSoupurl = requests.get('https://www.perfectimprints.com/custom-promos/20492/Beach-Balls.html')source = BeautifulSoup(url.text, 'html.parser')products = source.find_all('div', class_="product_wrapper")infos = source.find_all('div', class_="categories_wrapper")def get_category_information(category): category_name = category.find('h1', class_="category_head_name").text return { "category_name": category_name.strip() }category_information = [get_category_information(info) for info in infos]with open("category_info.json", "w") as write_file: json.dump(category_information, write_file)def get_product_details(product): product_name = product.find('div', class_="product_name").a.text sku = product.find('div', class_="product_sku").text product_link = product.find('div', class_="product_image_wrapper").find("a")["href"] src = product.find('div', class_="product_image_wrapper").find('a').find("img")["src"] return { "title": product_name, "link": product_link.strip(), "sku": sku.strip(), "src": src.strip() }all_products = [get_product_details(product) for product in products]with open("products.json", "w") as write_file: json.dump({'items': all_products}, write_file)print("Success")这会生成我需要的 JSON 文件。但是,我现在需要使用这些 JSON 文件并将其输入到我的模板 ( gist ) 中的任何地方{{ JSON DATA HERE }}。我什至不知道从哪里开始。我最熟悉 JavaScript,所以如果可能的话,我想使用它。我想出了一些涉及 Node.js 的事情。
添加回答
举报
0/150
提交
取消