alexanderholt
10/24/2017 - 5:07 PM

Scraping for loop to pd dataframe

#Joe says to try and except every time

import pandas as pd
dc_eats = pd.DataFrame(columns=["name","location","price","bookings"])

# loop through each entry
for entry in html.find_all('div', {'class':'result content-section-list-row cf with-times'}):
    # grab the name
    name = entry.find('span', {'class': 'rest-row-name-text'}).text
    # grab the location
    location = entry.find('span', {'class': 'rest-row-meta--location rest-row-meta-text'}).text
    # grab the price
    price = entry.find('div', {'class': 'rest-row-pricing'}).find('i').text.count('$') #can chain
    # try to find the number of bookings
    try:
        temp = entry.find('div', {'class':'booking'}).text
        match = re.search(r'\d+', temp)
        if match:
            bookings = match.group()
    except:
        bookings = 0
    # add to df
    dc_eats.loc[len(dc_eats)]=[name, location, price, bookings]