

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 7
click for more info
Not enough gems
Cost: 6 gems
1: JSON Report
incomplete
2: Submit the Link to Your Repository!
incomplete
This lesson's interactive features are locked, please to keep using them
We're almost done! Our web crawler now extracts rich data from every page and stores it efficiently in a map. Let's export it to JSON so it's easy to read and share.
For example, one page record in that map might look like this:
pages := map[string]PageData{
"learnwebscraping.dev/practice/ecommerce/products/ashenfang-longsword-fan-1001": {
URL: "https://learnwebscraping.dev/practice/ecommerce/products/ashenfang-longsword-fan-1001/",
Heading: "Ashenfang Longsword",
FirstParagraph: "A balanced battlefield blade with a smoldering fuller and leather-wrapped grip.",
OutgoingLinks: []string{
"https://learnwebscraping.dev/practice/ecommerce/",
"https://learnwebscraping.dev/practice/ecommerce/categories/",
"https://learnwebscraping.dev/practice/ecommerce/categories/longswords/",
},
ImageURLs: []string{"https://learnwebscraping.dev/images/catalog/longswords.svg"},
},
}
We want to create a report.json file containing a sorted array of all page records.
type PageData struct {
URL string `json:"url"`
Heading string `json:"heading"`
FirstParagraph string `json:"first_paragraph"`
OutgoingLinks []string `json:"outgoing_links"`
ImageURLs []string `json:"image_urls"`
}
import "encoding/json"
func writeJSONReport(pages map[string]PageData, filename string) error
[]PageData slice in sorted orderjson.MarshalIndent using indent=2os.WriteFileHere are some tips:
sort.Strings(keys)data, err := json.MarshalIndent(sorted, "", " ")os.WriteFile(filename, data, 0644)writeJSONReport(cfg.pages, "report.json") after crawling completesreport.json is created after running the crawlerRun and submit the CLI tests.