Make a list of random names

Sometimes you just want to build a list of random names. Here's a quick and easy way.


Sometimes you just want to build a list of random names. Here's a quick and easy way.

Code Breakdown

First we import all the libraries we will need.

import lxml.html
import requests
import yaml
import random

This function performs a critical role. First it gets a page from the internet, parses it using the LXML library, and then gets a list of names based on a supplied XPath.

def get_names(url,xpath):
    page = requests.get(url)
    tree = lxml.html.fromstring(page.content)
    names = [item.text for item in tree.xpath(xpath)]
    return names

To get a list of first names, we first visit a random web page that has a list of names and obtain the xpath for the element that we care about. By supplying the URL to the page and the XPath string, we can get back the list of first names.

xpath='/html/body/div/div/div[1]/div/div/div[4]/table/tbody/tr[*]/td[2]/a'
url = 'https://www.thenamegeek.com/most-common-first-names'
first_names = get_names(url,xpath)

We reuse this function for finding a list of last names, and then extend the initial list with the second through fifth page results as well

xpath= '/html/body/div/div/main/section[2]/div[2]/table/tbody/tr[*]/td[2]/a'
url = 'https://namecensus.com/last-names/'
last_names = get_names(url,xpath)
last_names.extend(get_names('https://namecensus.com/last-names/?page=2',xpath))
last_names.extend(get_names('https://namecensus.com/last-names/?page=3',xpath))
last_names.extend(get_names('https://namecensus.com/last-names/?page=4',xpath))
last_names.extend(get_names('https://namecensus.com/last-names/?page=5',xpath))

We create a dictionary and put the list of first names and last names in the dictionary.

my_dict = {}
my_dict['first_names'] = first_names
my_dict['last_names'] = last_names

To create arbitrary email addresses we supply a list of fun old school provider names.

providers = []
providers.append('gmail.com')
providers.append('yahoo.com')
providers.append('aol.com')
providers.append('altavista.net')
providers.append('comcast.net')
providers.append('geocities.com')
providers.append('friendster.com')
providers.append('myspace.com')

to create a full user profile, We combine a randomized first name and last name With a randomized provider to create a random full name and email. These profiles are added to a list of random profiles, Which are also added to our dictionary.

random_names = []
for ii in range(1000):
    first_name = random.choice(first_names)
    last_name = random.choice(last_names)
    provider = random.choice(providers)
    full_name = f'{first_name} {last_name}'
    email = f'{first_name}_{last_name}@{provider}'
    user = {}
    user['first_name'] = first_name
    user['last_name'] = last_name
    user['full_name'] = full_name
    user['email'] = email
    random_names.append(user)
my_dict['random_names'] = random_names

With our dictionary completed, we save it to a file on our PC.

with open('names.yaml','w') as f:
    yaml.dump(my_dict,f)

Full Code

import lxml.html
import requests
import yaml
import random


def get_names(url,xpath):
    page = requests.get(url)
    tree = lxml.html.fromstring(page.content)
    first_names = [item.text for item in tree.xpath(xpath)]
    return first_names

xpath='/html/body/div/div/div[1]/div/div/div[4]/table/tbody/tr[*]/td[2]/a'
url = 'https://www.thenamegeek.com/most-common-first-names'
first_names = get_names(url,xpath)

xpath= '/html/body/div/div/main/section[2]/div[2]/table/tbody/tr[*]/td[2]/a'
url = 'https://namecensus.com/last-names/'
last_names = get_names(url,xpath)
last_names.extend(get_names('https://namecensus.com/last-names/?page=2',xpath))
last_names.extend(get_names('https://namecensus.com/last-names/?page=3',xpath))
last_names.extend(get_names('https://namecensus.com/last-names/?page=4',xpath))
last_names.extend(get_names('https://namecensus.com/last-names/?page=5',xpath))

my_dict = {}
my_dict['first_names'] = first_names
my_dict['last_names'] = last_names


providers = []
providers.append('gmail.com')
providers.append('yahoo.com')
providers.append('aol.com')
providers.append('altavista.net')
providers.append('comcast.net')
providers.append('geocities.com')
providers.append('friendster.com')
providers.append('myspace.com')


random_names = []
for ii in range(1000):
    first_name = random.choice(first_names)
    last_name = random.choice(last_names)
    provider = random.choice(providers)
    full_name = f'{first_name} {last_name}'
    email = f'{first_name}_{last_name}@{provider}'
    user = {}
    user['first_name'] = first_name
    user['last_name'] = last_name
    user['full_name'] = full_name
    user['email'] = email
    random_names.append(user)


my_dict['random_names'] = random_names


with open('names.yaml','w') as f:
    yaml.dump(my_dict,f)

About

I am an engineer and educator, having spent ten years as a professor. My goal is to help you build your knowledge of design and technology, get your hardware working, and propel your startup or small business. Get in touch!

Read More