为了账号安全,请及时绑定邮箱和手机立即绑定

使用模板和 JSON 输出 HTML 数据

使用模板和 JSON 输出 HTML 数据

慕娘9325324 2021-07-20 13:01:36
使用模板和 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 的事情。
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 188 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号