How To Web Scrape

Step 1

Import BeautifulSoup

import requests
from requests import get
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
import pandas as pd

Step 2

In this specific instance we’ll set the url to the Wikipedia page of the most recent bachelorette season (Season 16 — Clare and Tayshia season).

url = ''
results = requests.get(url)

Step 3

“Beautiful Soup is a Python library for pulling data out of HTML files. It works with your parser to provide ways of navigating and parsing.”

soup = BeautifulSoup(results.text, "html.parser")

Step 4

On the bachelorette Wikipedia page, right click and hit “Inspect.”

Step 5

Click the top left image (arrow into the box)

Step 6

Look on the console for the line that highlights the bachelorette table

Step 7

soup.findAll('table', class_='wikitable')

Step 8

I have set the above equal to soup.

soup = soup.findAll('table', class_='wikitable')
soup = soup[0].find('tbody')

Step 9

Continuation of parsing through html for the name Dale Moss.

x = soup.find_all('tr')[1]

Step 10

The name, Dale Moss, has been isolated from the table! (Clare’s winner)


Step 11

The name, Zac Clark has been isolated! (Tayshia’s winner)


Step 12

Zac and Dale involved a different process, while isolating their names. But as you can see below, the rest of the contestants will all follow the same pattern.

Step 13

Now let’s isolate the rest of the names.


Step 14

names = []
for n in range(3,36):
for i in range(len(names)):
num = names[i].index('[')
names[i] = names[i][:num]

Step 15

hometown = []
for x in range(3,36):
jobs = []
for n in range(3,36):
ages = []
for age in range(3,36):
list(map(lambda x: int(x), ages))

Step 16

Create a DataFrame

import pandas as pd
from pandas import DataFrame
df = pd.DataFrame(ages, columns=['ages'])
df['names'] = names
df['hometowns'] = hometown
df['jobs'] = jobs

Step 17

Dale and Zac have to be added in separately because the web scraping for them was a little different.

new_row1 = {'ages': [31, 36], 'names':['Dale Moss', 'Zac Clark'], 'hometowns': ['Brandon, South Dakota', 'Haddonfield, New Jersey'], 'jobs': ['Former Pro Football Wide Receiver', 'Addiction Specialist']}top_row = pd.DataFrame(new_row1)df = pd.concat([top_row, df]).reset_index(drop = True)
All names included



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store