Home > database >  Data are overwritten and then print same name
Data are overwritten and then print same name

Time:07-04

import requests
import json
import pandas as pd
headers = {
    'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8,pt;q=0.7',
    'Connection': 'keep-alive',
    'Origin': 'https://www.nationalhardwareshow.com',
    'Referer': 'https://www.nationalhardwareshow.com/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'cross-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
    'accept': 'application/json',
    'content-type': 'application/x-www-form-urlencoded',
    'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

params = {
    'x-algolia-agent': 'Algolia for vanilla JavaScript 3.27.1',
    'x-algolia-application-id': 'XD0U5M6Y4R',
    'x-algolia-api-key': 'd5cd7d4ec26134ff4a34d736a7f9ad47',
}

data = '{"params":"query=&page=0&facetFilters=&optionalFilters=[]"}'

resp = requests.post('https://xd0u5m6y4r-dsn.algolia.net/1/indexes/event-edition-eve-e6b1ae25-5b9f-457b-83b3-335667332366_en-us/query', params=params, headers=headers, data=data).json()


req_json=resp

data=[]
wev={}

item=req_json['hits']

for title in item:
  
    t=title['name']
    wev['title']=t
               
for spec  in item:
    r=spec['representedBrands']
    wev['Brand']=r
      
for des in item:
    q=des['description']
    wev['des']=q
    
    data.append(wev)
    
    
df=pd.DataFrame(data)
print(df)

The data are overwritten and they print same name how I solve these issue I know these is beacuse of for loop they overwrite the data but How to get out of the for loop to solve these problem is any solution then share with me if any feasible solution provide me this is the output:

1   Changzhou Feiwang Tool Co.,Ltd.   
2   Changzhou Feiwang Tool Co.,Ltd. 

CodePudding user response:

Why not simply convert req_json['hits'] directly into a dataFrame?

...
req_json=resp
df = pd.DataFrame(req_json['hits'])

You could filter the information to display:

df[['name','representedBrands','description']]

Output

    name    representedBrands   description
0   21st Century Inc    [V&O, Globrite & 21st Century]  We specialize in the following:\nBBQ Grill ac...
1   3M  [3M, Bondo] The 3M Company is an American multinational co...
2   3V Snap Ring LLC.   [BagSnap-n-Go]  3V Snapring manufactures BagSnap-N-Go, an inno...
3   48 Tools - Woodpecker Imports LLC   [48Tools]   The seed of 48 tools was planted in 2016 when ...
4   A-ipower Corp   [A-iPower]  A-iPower Corp Manufactures and Distributes Gen...

...

  • Related