Goal
Write a Python script that chooses a random movie from a user’s Letterboxd watchlist.
Getting Started
Start by importing the necessary Python libraries for scraping a webpage and then choosing a random selection from an array.
import requestsfrom bs4 import BeautifulSoupimport randomConfiguring BeaufifulSoup
Next we will define our variables and tell the BeautifulSoup library how to find the film title when scraping the web page.
# Set the URL of the user's Letterboxd watchlisturl = "https://letterboxd.com/mackiser/watchlist/"
# Make a GET request to the URLresponse = requests.get(url)
# Parse the HTML content of the pagesoup = BeautifulSoup(response.content, "html.parser")
# Find all the movie elements on the pagemovies = soup.find_all("div", class_="film-poster")Selecting a Random Film
Now we will use the random library to choose a random film from the array of movies we have now.
# Pick a random movie from the listrandom_movie = random.choice(movies)Print the Selection
print(title)Output
$ python3 watchlistPicker.pyCitizen Kane