Web Scraping with Beam Functions
In this example, we’ll demonstrate how to build a Wikipedia web scraper using Beam functions. While you could run this on a local computer, Beam provides access to more powerful computational resources, allowing you to add advanced features to your webscraper using large language models or OCR models.
View the Code
See the code for this example on Github.
Defining our Scraping Function
We will start by defining our scraping function. This is the Beam function that will be invoked remotely. We use the Image
class from the beam
SDK to install these packages in the container running your code.
Our function takes in a URL, fetches the page’s HTML, and then uses BeautifulSoup to extract the page’s title, content, and links. It returns that content in a dictionary so that our crawler can invoke new functions with the links found on the page. If we wanted, we could add more functionality to this function to extract or process the content in a variety of ways. For example, we could add a language model to summarize the content or use an OCR model to extract text from an image.
Building a Batch Crawler with Beam’s Function Map
Next, we’ll build a crawler that will use Beam’s map
method to invoke our scrape_page
function on a list of URLs. Below, is our __init__
method for the crawler.
Our crawler takes in a starting URL, a maximum number of pages to scrape, and a batch size. The batch size determines how many remote function invocations we will make at a time.
Next, we’ll define the actual crawl
method along with a helper method to determine if a URL is a valid Wikipedia URL.
The crawl method runs continuously until we have scraped the maximum number of pages or there are no more pages to visit. It creates a batch of URLs to scrape and then passes them to the scrape_page
function’s map
method. This allows us to scrape multiple pages in parallel. After the pages are scraped, we collect any new links that we want to visit and add them to the pages_to_visit
list.
Running the Batch Crawler
Finally, we can run our crawler. Below is the code for our main
function which initializes the crawler and runs the crawl method.
This code initializes the crawler with a starting URL and a maximum number of pages to scrape. It then runs the crawl method and writes the scraped data to a file. You can run this code like any other Python script:
When you run this code, you should see output that looks like the following:
The output shows five function invocations in parallel. Once the scraping is complete, you can see the results in the scraped_data.json
file. It will look something like this:
Building a Continuous Crawler with Beam Functions and Threads
The batched web crawler is a good starting point, but it requires waiting for a full batch to finish before starting any new jobs. If we want to keep our crawler limit continuously saturated, we can use Beam functions in conjunction with Python threads.
To do this, we will use the same scrape_page
function, but instead of using the map
method, we will use a thread pool to invoke the function in parallel. Below is the code for our WikipediaCrawler
class with a continuous crawl method.
This code is more complex than the batch crawler, but it allows us to better utilize our compute resources. Instead of having containers sitting idle while other containers complete their work, we immediately send a new function invocation as soon as another one completes. To do this, we track the futures returned by the executor.submit
method and wait for any of them to complete using the concurrent.futures.wait
method. We specify that we only want to wait for one of the futures to complete using the concurrent.futures.FIRST_COMPLETED
constant. This means that as soon as any future completes, we will process the result and add new work to the pool.
Running the Continuous Crawler
To run the continuous crawler, you can use the same main
function as before. When you run this code, you should see output that looks like the following:
As you can see, as soon as one function invocation completes, we immediately start a new one.
Was this page helpful?