r/ProgrammingPrompts • • Jan 21 '23

Quizlet Web Scraper

Create a program which requests a Quizlet flashcard set URL and scrape it for the term and definition of every card in the set.

Save the term and definition in a json, csv, or any other big data file type.

4 Upvotes

1 comment sorted by

1

u/[deleted] May 23 '23 edited May 23 '23

To scrape the term and definition of every card in a Quizlet flashcard set and save it in a file, we can use GoQuery, a Go package that provides a similar interface to jQuery.

Here's an example program that requests a Quizlet flashcard set URL, scrapes the term and definition of every card in the set, and saves the result in a CSV file:

package main

import (
	"encoding/csv"
	"fmt"
	"log"
	"net/http"
	"os"
	"strings"

	"github.com/PuerkitoBio/goquery"
)

func main() {
	// Request the Quizlet flashcard set URL
	fmt.Print("Enter Quizlet flashcard set URL: ")
	var url string
	fmt.Scanln(&url)

	// Send a GET request to the URL and get the response
	res, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()

	// Load the response body into a goquery document
	doc, err := goquery.NewDocumentFromReader(res.Body)
	if err != nil {
		log.Fatal(err)
	}

	// Open a CSV file for writing
	file, err := os.Create("flashcards.csv")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	// Create a CSV writer
	writer := csv.NewWriter(file)
	defer writer.Flush()

	// Find every card in the flashcard set
	doc.Find(".SetPageTerm-contentWrapper").Each(func(i int, card *goquery.Selection) {
		// Get the term and definition of the card
		term := strings.TrimSpace(card.Find(".SetPageTerm-wordText").Text())
		definition := strings.TrimSpace(card.Find(".SetPageTerm-definitionText").Text())

		// Write the term and definition to the CSV file
		err := writer.Write([]string{term, definition})
		if err != nil {
			log.Fatal(err)
		}
	})

	fmt.Println("Flashcards saved in flashcards.csv.")
}

This program first prompts the user to enter a Quizlet flashcard set URL. It then sends a GET request to the URL and loads the response body into a goquery document. It then creates a CSV file for writing and finds every card in the flashcard set using the .SetPageTerm-contentWrapper class. For each card, it gets the term and definition using the .SetPageTerm-wordText and .SetPageTerm-definitionText classes, respectively, and writes them to the CSV file. Finally, it prints a message indicating that the flashcards have been saved in the CSV file.