Local Link Building with Python and the Places API

Posted in: SEO

Join thousands of marketers to get the best search news in under 5 minutes. Get resources, tips and more with The Splash newsletter:

The Value of Local Links

Among SEO strategies, link building is arguably the one that has seen the most swings in popularity over the years. As soon as a link building tactic proves both successful and scalable, it is deployed to the point of overuse. Often, the value of the tactic’s resulting links falls due to oversupply.

Related Content:

Tactics that add value are the exception. Fixing broken links and linking brand mentions, for example, make the internet a more connected, informative, and useful tool.

One such tactic is local link building – accruing backlinks from local interest websites and websites associated with local businesses. This practice makes the internet a clearer reflection of the physical world, ensuring that real-life neighbors are connected by hyperlinks in the same way they are connected by streets and sidewalks.

Google has shown a preference to deliver hyperlocal results for some time now. The Pigeon update back in 2014 indicated that they were connecting their local algorithm more closely to their traditional web algorithm. That means that gaining links from local websites matters for improving local rankings. But where can you find websites that are associated with places nearby?

Finding Nearby Places

The biggest challenge in building links from local websites is finding a significant number of websites efficiently. One could spend hours typing in local business names into Google, but that is not a scalable process.

In our case, we were link building for a chain of restaurants with hundreds of locations across North America. For each restaurant, I wanted a list of websites for the hotels and local attractions within a ten-mile radius. Often, those types of websites are keen to list nearby restaurants, as they want to provide their out-of-town guests with local information. Scalability was essential.

Google’s Places API can return this information, but not in the same request. There are a couple of different endpoints you must loop through in order to get websites from local establishments.

The Python function below takes the coordinates of a location and makes a request to the Places API to pull nearby places of a certain type. A place type might be a restaurant, a hotel, a city park, or any other non-residential entity that may pop up on Google Maps. This function uses a few modules: requests, json, and time. Additionally, you will need an API key (it’s free).

There are some limitations to this API. Each request, for example, returns a maximum of 60 results. Those results are split into 20 results per page. If a request has more than 20 results, you’ll need to use the “next_page_token” value, which will be returned if there is a next page, to make a new request for the next page of results. Furthermore, if you need to request the next page of results, there is a short delay (less than a second) before the token becomes valid.

Requesting nearby places by type is the best way I’ve found to maximize the number of places returned. Using this method will return up to 60 results for each type. Here is the full list of place types listed in the API.

Getting Local Websites via Place Details

The code above calls a second function to get each place’s website. The “nearby places” endpoint does not return much info about the places themselves. To get the website, as well as any other details about the company like a phone number or address, the API requires a separate request — this time to the Place Details endpoint. This request uses the “place_id” value returned in the previous request. The code for the Place Details request is below:

Run the code by calling the first function, specifying your business’ coordinates, and the type of places for which you’d like to get URLs. For example:

That returns a list of 60 hotels, and their websites, within a ten-mile radius of Times Square, NYC. The first hotels to come through the API are shown in the screenshot below.

Final Thoughts

This is a nice example of how a relatively simple Python script can expand the capabilities of an SEO campaign. In this example, I was getting one type of place, but you can easily adapt this code to loop through an array of each place type. You can find the full code below, with a list of the place types covered by the API.

How are you using APIs to power your marketing efforts? Let us know in the comments!

Search News Straight To Your Inbox

This field is for validation purposes and should be left unchanged.


Join thousands of marketers to get the best search news in under 5 minutes. Get resources, tips and more with The Splash newsletter: