How do i save bs4 values in xls or csv?

185 Views Asked by At

So I have extracted data from a website and I need to save it in excel. I'm new to python and can't figure out how to go about it.

here is the data that I've extracted and it's type is bs4.beautifulsoup

{"lookbook":{"img":[],"count":0},"sizeInfoDes":{"sizeInfo":[{"size":"XS","Shoulder ":" 53 cm","Bust ":" 95 cm","Length ":" 51.5 cm","Sleeve Length ":" 49.5 cm"},{"size":"S","Shoulder ":" 55 cm","Bust ":" 99 cm","Length ":" 52.5 cm","Sleeve Length ":" 50 cm"},{"size":"M","Shoulder ":" 57 cm","Bust ":" 103 cm","Length ":" 53.5 cm","Sleeve Length ":" 50.5 cm"},{"size":"L","Shoulder ":" 60 cm","Bust ":" 109 cm","Length ":" 55 cm","Sleeve Length ":" 51 cm"},{"size":"XL","Shoulder ":" 63 cm","Bust ":" 115 cm","Length ":" 56.5 cm","Sleeve Length ":" 51.5 cm"}],"sizeInfoInch":[{"size":"XS","Shoulder ":" 20.9 inch","Bust ":" 37.4 inch","Length ":" 20.3 inch","Sleeve Length ":" 19.5 inch"},{"size":"S","Shoulder ":" 21.7 inch","Bust ":" 39 inch","Length ":" 20.7 inch","Sleeve Length ":" 19.7 inch"},{"size":"M","Shoulder ":" 22.4 inch","Bust ":" 40.6 inch","Length ":" 21.1 inch","Sleeve Length ":" 19.9 inch"},{"size":"L","Shoulder ":" 23.6 inch","Bust ":" 42.9 inch","Length ":" 21.7 inch","Sleeve Length ":" 20.1 inch"},{"size":"XL","Shoulder ":" 24.8 inch","Bust ":" 45.3 inch","Length ":" 22.2 inch","Sleeve Length ":" 20.3 inch"}],"sizeUnit":0,"allcmFlag":1,"sizeInfoAttribute":[],"basicAttribute":{"image_url":"","attribute_info":[],"base_code_info":[],"base_code_info_inch":[]}},"model":{"attr":{"Height":"175 cm","Bust":"85 cm","Waist":"61 cm","Hip":"93 cm"},"size":"S","name":"Andy","attrcm":{"Height":"175 cm","Bust":"85 cm","Waist":"61 cm","Hip":"93 cm"},"attrinch":{"Height":"68.9 inch","Bust":"33.5 inch","Waist":"24 inch","Hip":"36.6 inch"},"sizeUnit":0},"getTheLookInfo":[]}

I need shouder, bust, lenght and sleeve length values extracted.

1

There are 1 best solutions below

0
Sushil On

Convert the data that you have into a dictionary (it mostly looks like a json file, so u can easily convert it into a dictionary using the json module). Then, just use this code to output the data to an excel file:

from bs4 import BeautifulSoup
import pandas as pd

dictionary = {"lookbook":{"img":[],"count":0},"sizeInfoDes":{"sizeInfo":[{"size":"XS","Shoulder ":" 53 cm","Bust ":" 95 cm","Length ":" 51.5 cm","Sleeve Length ":" 49.5 cm"},{"size":"S","Shoulder ":" 55 cm","Bust ":" 99 cm","Length ":" 52.5 cm","Sleeve Length ":" 50 cm"},{"size":"M","Shoulder ":" 57 cm","Bust ":" 103 cm","Length ":" 53.5 cm","Sleeve Length ":" 50.5 cm"},{"size":"L","Shoulder ":" 60 cm","Bust ":" 109 cm","Length ":" 55 cm","Sleeve Length ":" 51 cm"},{"size":"XL","Shoulder ":" 63 cm","Bust ":" 115 cm","Length ":" 56.5 cm","Sleeve Length ":" 51.5 cm"}],"sizeInfoInch":[{"size":"XS","Shoulder ":" 20.9 inch","Bust ":" 37.4 inch","Length ":" 20.3 inch","Sleeve Length ":" 19.5 inch"},{"size":"S","Shoulder ":" 21.7 inch","Bust ":" 39 inch","Length ":" 20.7 inch","Sleeve Length ":" 19.7 inch"},{"size":"M","Shoulder ":" 22.4 inch","Bust ":" 40.6 inch","Length ":" 21.1 inch","Sleeve Length ":" 19.9 inch"},{"size":"L","Shoulder ":" 23.6 inch","Bust ":" 42.9 inch","Length ":" 21.7 inch","Sleeve Length ":" 20.1 inch"},{"size":"XL","Shoulder ":" 24.8 inch","Bust ":" 45.3 inch","Length ":" 22.2 inch","Sleeve Length ":" 20.3 inch"}],"sizeUnit":0,"allcmFlag":1,"sizeInfoAttribute":[],"basicAttribute":{"image_url":"","attribute_info":[],"base_code_info":[],"base_code_info_inch":[]}},"model":{"attr":{"Height":"175 cm","Bust":"85 cm","Waist":"61 cm","Hip":"93 cm"},"size":"S","name":"Andy","attrcm":{"Height":"175 cm","Bust":"85 cm","Waist":"61 cm","Hip":"93 cm"},"attrinch":{"Height":"68.9 inch","Bust":"33.5 inch","Waist":"24 inch","Hip":"36.6 inch"},"sizeUnit":0},"getTheLookInfo":[]}

size_info_dict = dictionary['sizeInfoDes']['sizeInfo']

final_dict = {'Shoulder ':[],
              'Bust ':[],
              'Length ':[],
              'Sleeve Length ':[]}

for curr_dict in size_info_dict:
    for key in curr_dict.keys():
        try:
            final_dict[key].append(curr_dict[key])
        except KeyError:
            pass

df = pd.DataFrame(final_dict)

df.to_excel("D:\\Output.xlsx",index = False)

Output Screenshot:

enter image description here

Hope that this helps!