We're almost done! Our web crawler now extracts rich data from every page and stores it efficiently in a dictionary. But printing this data to the console isn't very useful for analysis. Let's export it to CSV format so its easier to read and parse.
For example, from this page data dictionary:
pages := map[string]PageData{
"blog.boot.dev": {
URL: "https://blog.boot.dev",
H1: "Learn Backend Development",
FirstParagraph: "Boot.dev teaches backend development...",
OutgoingLinks: []string{"https://boot.dev/courses", "https://boot.dev/about"},
ImageURLs: []string{"https://blog.boot.dev/logo.png"},
},
}
We want to create a CSV file with columns: page_url
, h1
, first_paragraph
, outgoing_link_urls
, image_urls
Import Go's built-in encoding/csv
package:
import "encoding/csv"
func writeCSVRport(pages maps[string]PageData, filename string) error:
pages
is the map returned by your crawler (keys are normalized URLs, values are PageData
structs)filename
is the CSV file to create (defaults to "report.csv" in main.go
)page_url
, h1
, first_paragraph
, outgoing_link_urls
, image_urls
;
) for the link and image URL columnsHere are some tips to get you started:
os.Create(filename)
csv.NewWriter(file)
to write the datawriter.Write([]string{...})
PageData
in the map, write a row with its fieldsstrings.Join(page.OutgoingLinks, ";")
writeCSVReport
functionwriteCSVReport(cfg.pages, "report.csv")
report.csv
is created after running the crawlerRun and submit the CLI tests.
The Boot.dev CLI requires you to be signed in to submit your solution!
Copy/paste one of the following commands into your terminal:
Run
bootdev run 5cb7abfb-61d9-46f7-8243-aab57991e569
Submit
bootdev run 5cb7abfb-61d9-46f7-8243-aab57991e569 -s
To run and submit the tests for this lesson, you must have an active Boot.dev membership
Become a member to view solution
Using the Bootdev CLI
The Bootdev CLI is the only way to submit your solution for this type of lesson. We need to be able to run commands in your environment to verify your solution.
You can install it here. It's a Go program hosted on GitHub, so you'll need Go installed as well. Instructions are on the GitHub page.