Calculating degrees of separation

Now let's calculate the degrees of separation between any two pages.  This answers the question of how many pages we need to go through from a source page to find another page.  This could be a non-trivial graph traversal problem as there can be multiple paths between the two pages.  Fortunately for us, NetworkX, using the exact same graph model, has built in function to solve this with the exact same model from the previous recipe.

Get Python Web Scraping Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.