1

This link contains the table I'm trying to parse. I'm trying to use BeautifulSoup in Python. I'm very new to BeautifulSoup and HTML. This is my attempt to solve my problem.

soup = BeautifulSoup(open('BBS_student_grads.php'))

data = []
table = soup.find('table')
rows = table.find_all('tr') #array of rows in table 

for x,row in enumerate(rows[1:]):# skips first row 
    cols = row.find_all('td')    # finds all cols in rows
    for y,col in enumerate(cols): # iterates through col
        data.append([])
        data[x].append(col)       # puts table into a 2d array called data

print(data[0][0])                 #prints top left corner

Sample Output

I'm trying to extract all the names in the table, then update the names in the list and then update the table. I'm also using a local copy of this HTML. Temporary fix till I learn how to do more web programming.

help is much appreciated

1 Answer 1

1

I think you need just the td elements in the tr element with class="searchbox_black".

You can use CSS Selectors to get to the desired td elements:

for cell in soup.select('tr.searchbox_black td'):
    print cell.text

It prints:

BB Salsa

 Adams State University Alamosa, CO               
              Sensei: Oneyda Maestas               
              Raymond Breitstein               

...
Sign up to request clarification or add additional context in comments.

5 Comments

for some reason when you go further down when it starts printing spaces between every character. Actual output: "Rebecca Ha l l b r > "
@thor sorry, cannot reproduce it, what is your current code? Thanks.
soup = BeautifulSoup(open('BBS_student_grads.php')) data = [] for cell in soup.select('tr.searchbox_black td'): data.append(cell) print(data[0]) @alecxe
@thor did you mean data.append(cell.text) instead?
cell.text does output just the text but it still does some odd formatting around "Rebecca Hall". python puts 3 space around every character

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.