亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用模板和 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 關注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號