Reputation: 779
import enum
import requests
from bs4 import BeautifulSoup
import json
import pandas as pd
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.3"
}
r = requests.get("https://www.fleetpride.com/parts/otr-brake-drum-otr1601b")
soup = BeautifulSoup(r.content, "html5lib")
raw_json = ""
for table_index,table in enumerate( soup.find_all("script")):
if('CCRZ.detailData.jsonProductData = {"' in str(table)):
x=str(table).split('CCRZ.detailData.jsonProductData = {"')
raw_json = "{\""+str(x[-1]).split('};')[0]+"}"
break
req_json = json.loads(raw_json)
# uri = req_json.keys()
print(req_json)
this is my ouput show when I run code :
I want like theses in json format
can it will be possible to convert in this format if possible can you tell how we convert in json form like these as you shown below I am grateful to you:
{
"mediaWrappers": {
"Alternate Images": [{
"uri": "https://www.fleetpride.com/imagesns/PDPF/OTR1601B-Webb copy.jpg",
"mediaName": "https://www.fleetpride.com/imagesns/PDPF/OTR1601B-Webb copy.jpg",
"sourceType": "URI",
"URI": "https://www.fleetpride.com/imagesns/PDPF/OTR1601B-Webb copy.jpg",
"startDate": "2018-05-23",
"sequence": 2,
"productMediaId": "OTR-OTR1601B-Alternate Images-2",
"mediaType": "Alternate Images",
"locale": "en_US",
"endDate": "2099-12-31",
"enabled": true,
"altMessage": "OTR OTR OTR1601B",
"sfdcName": "406524",
"sfid": "a8B1W000000c4a1UAA",
"product": "a8G1W000000Y7DfUAK"
},
Upvotes: 0
Views: 104
Reputation: 2022
Both outputs are JSON formats. What you are looking for is called pretty printing. Read about json.dumps()
and its options. You want to use the indent
option.
...
pretty = json.dumps(req_json, indent=4)
print(pretty)
Upvotes: 1
Reputation: 136
You could just add the following code after json.loads()
.
with open("req_json.json", "w") as f:
json.dump(req_json, f, indent=4)
Upvotes: 1