How to do it

We could scrape this page to get this information but fortunately, freegeoip.net gives us a convenient REST API to use. Scrolling further down the page, we can see the API documentation:

The freegeoio.net API documentation

We can simply use the requests library to make a GET request using the properly formatted URL. As an example, just entering the following URL in the browser returns a JSON representation of the geocoded data for the given IP address:

Sample JSON for an IP address

A Python script to demonstrate this is available ...

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.