<jupyter_start><jupyter_text>Scrape Texas death row inmatesIn this exercise, we're going to scrape [a table of Texas death row inmates](http://www.tdcj.state.tx.us/death_row/dr_offenders_on_dr.html) into a csv file.First, import the libraries we'll need:<jupyter_code>import requests
from bs4 import BeautifulSoup
import csv<jupyter_output><empty_output><jupyter_text>Use `requests` to fetch the page and feed its contents to `BeautifulSoup`:<jupyter_code>url = 'http://www.tdcj.state.tx.us/death_row/dr_offenders_on_dr.html'

r = requests.get(url)

soup = BeautifulSoup(r.text, 'html.parser')<jupyter_output><empty_output><jupyter_text>Now _view source_ on the page and figure out how we're going to isolate that table. There are a couple ways to do this -- I'm going to target its class.<jupyter_code>dr_table = soup.find('table', {'class': 'os'})<jupyter_output><empty_output><jupyter_text>- Find all of the rows in the table.- In a for loop, iterate over the rows (skip the first one) and print each one<jupyter_code>dr_rows = dr_table.find_all('tr')[1:]

for row in dr_rows:
    print(row)<jupyter_output><empty_output><jupyter_text>Let's look at each cell in each row. Using `find_all`, get a list of cells in each row and print the list:<jupyter_code>for row in dr_rows:
    cols = row.find_all('td')
    print(cols)<jupyter_output><empty_output><jupyter_text>Let's assign descriptive names to each cell based on its position in the list.You will use bracket notation to access items in a list -- keep in mind that counting starts at zero in Python. For instance: `your_list[0]` returns the first item in `your_list`.The first item in our list of cells (`[0]`) is the inmate's ID number. Next is a link to his/her detail page (which we'll extract by accessing the nested a tag's `href` property; we'll also prepend the URL base to make it an absolute link), then his/her last name, etc.<jupyter_code>for row in dr_rows:
    cols = row.find_all('td')

    id_number = cols[0].text
    detail_link = 'http://www.tdcj.state.tx.us/death_row/' + cols[1].a['href']
    last_name = cols[2].text
    first_name = cols[3].text
    dob = cols[4].text
    sex = cols[5].text
    race = cols[6].text
    date_received = cols[7].text
    county = cols[8].text
    date_offense = cols[9].text
    print(id_number, detail_link, last_name, first_name, dob, sex, race, date_received, county, date_offense)<jupyter_output><empty_output><jupyter_text>Finally, let's write the data to a local csv file. Same setup as when we opened the practice table, except this time we're going to open a file in `w` mode -- let's call it _tx-death-row.csv_ -- and we're going to use a `csv.writer` object to take lists of values and write them to the file, one row at a time.The loop we just made will go inside the `with` block.<jupyter_code>with open('tx-death-row.csv', 'w') as outfile:
    writer = csv.writer(outfile)
    headers = ['id', 'link', 'last', 'first', 'dob', 'sex', 'race',
               'admission_date', 'county', 'offense_date']
    
    writer.writerow(headers)
    
    for row in dr_rows:
        cols = row.find_all('td')

        id_number = cols[0].text
        detail_link = 'http://www.tdcj.state.tx.us/death_row/' + cols[1].a['href']
        last_name = cols[2].text
        first_name = cols[3].text
        dob = cols[4].text
        sex = cols[5].text
        race = cols[6].text
        date_received = cols[7].text
        county = cols[8].text
        date_offense = cols[9].text
        writer.writerow([id_number, detail_link, last_name, first_name, dob, sex,
                         race, date_received, county, date_offense])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11444141689373297, 'fraction_numerical': 0.006539509536784741, 'mean_word_length': 3.8946666666666667, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '519755', 'license': 'MIT', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1226, 'n_words': 390, 'path': 'completed/5. Scrape Texas death row inmates-completed.ipynb', 'repo_name': 'cjwinchester/ire-2017-python-scraping', 'size': '3670'}