简体   繁体   English

在数组内生成动态嵌套 JSON 对象 - python

[英]Generating a dynamic nested JSON objects inside an array - python

I have a script through which I am scraping data from multiple pages.我有一个脚本,通过它我可以从多个页面抓取数据。 I am trying to generate dynamic nested JSON objects inside an array.我正在尝试在数组内生成动态嵌套的 JSON 对象。 But the result I am getting is that there are nested JSON objects but inside two lists .我得到的结果是有嵌套的 JSON 个对象,但在两个列表中 The output data starts like this ['"[{ and ends }]"'] like this. output 数据像这样开始['[{ and ends }]"']像这样。 I need someone who can explain to me where I am making a mistake?我需要有人可以向我解释我在哪里犯了错误?

**** I am pasting my code below please have a look **** **** 我在下面粘贴我的代码,请看一下 ****

I am pasting below my code.我粘贴在我的代码下面。

def geturl():
    urls = [
    
        # list of URLs
    ]
    
    with open('temp.json', 'w', encoding='utf-8') as file:
        for url in urls:
            r = requests.get(url)
            print(r.status_code)
            data = json.loads(r.content)
            items = data['items']
            baseurl =  # URL
            data = OrderedDict()
            main = []
            for item in items:
                data['Title'] = item['name']
                data["Price"] = item['price']
                data['Detai Page'] = baseurl + item['slug']
                data['Image'] = item['thumb_image']
    
                main.append(data)
                result = json.dumps(main)
        json.dump(result, file, indent=4, sort_keys=True)

geturl()

Someone, please help me how to solve this issue?有人,请帮我解决这个问题?

This should work file这应该工作文件

import pandas as pd
def geturl(filehandler):
  urls =[ # your urls here ] 

  main = []  
  baseurl = # your base url

  for url in urls:
  
    r = requests.get(url)
    data = json.loads(r.content)
    items = data['items']     
    
    for item in items:
      data = {}
      data['Title'] = item['name']
      data["Price"] = item['price']
      data['Detai Page'] = baseurl+item['slug']
      data['Image'] = item['thumb_image']
      main.append(data)
    
  # writing to json file
  json.dump(main, filehandler)

  # reading with pandas
  df= pd.DataFrame(main)
  df.to_json('data.json', orient='records')

  

with open('temp.json', 'w', encoding='utf-8') as filehandler:
  geturl(filehandler)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM