

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Welcome
incomplete
2: Golang Setup
incomplete
3: Normalize URLs
incomplete
4: Extract Page Content
incomplete
5: Extract Links and Images
incomplete
6: Structure Page Data
incomplete
This lesson's interactive features are locked, please to keep using them
So far we've built functions to help us normalize URLs and extract links/text from HTML. Now let's structure that data in a way that's much more usable.
func extractPageData(html, pageURL string) PageData {
html is an HTML stringpageURL is the absolute URL of the page (used for converting relative URLs)URL, Heading, FirstParagraph, OutgoingLinks, ImageURLsHere's one example test case to get you started:
func TestExtractPageData(t *testing.T) {
inputURL := "https://crawler-test.com"
inputBody := `<html><body>
<h1>Test Title</h1>
<p>This is the first paragraph.</p>
<a href="/link1">Link 1</a>
<img src="/image1.jpg" alt="Image 1">
</body></html>`
actual := extractPageData(inputBody, inputURL)
expected := PageData{
URL: "https://crawler-test.com",
Heading: "Test Title",
FirstParagraph: "This is the first paragraph.",
OutgoingLinks: []string{"https://crawler-test.com/link1"},
ImageURLs: []string{"https://crawler-test.com/image1.jpg"},
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("expected %+v, got %+v", expected, actual)
}
}
Run and submit the CLI tests to verify your extraction logic works correctly!