Reputation: 81
How do i access the <tr> tags inside <tbody>
using find_all
in for
loop because each <tr>
seems independent from each other and has alternative class 'even'
and 'odd'
. I can only pass two args in find_all
. i.e find_all('tr', class_='odd')
or (even)
Also how do I access only the 1st, 3rd, 4th and 6th in each . The tags do not have id or class.
[from bs4 import BeautifulSoup
import requests
src_code = requests.get('https://bschool.careers360.com/colleges/ranking/2018').text
soup = BeautifulSoup(src_code, features="html.parser")
i = 1
for trr in soup.find_all('tr', class_='odd'):
i+=1
college = trr.td.a.text
print(college)
if i%2==0:
class_='even'
else:
class_='odd'][1]
Upvotes: 0
Views: 359
Reputation: 1210
find_all("tr",class_=['odd','even'])
This gets all the tr tags then the td tag with a tag and the text of a tag
from bs4 import BeautifulSoup
import requests
src_code = requests.get('https://bschool.careers360.com/colleges/ranking/2018').text
soup = BeautifulSoup(src_code, features="html.parser")
alltr=soup.find_all("tr",class_=['odd','even'])
for x in alltr:
print(x.td.a.text)
Upvotes: 1
Reputation: 183
You could find the parent tag first.
from bs4 import BeautifulSoup
import requests
src_code = requests.get('https://bschool.careers360.com/colleges/ranking/2018').content
soup = BeautifulSoup(src_code, features="html5lib")
trs=soup.find(name = "div",id="related-results").find_all(name = "tr")
trs
the trs is what you want:
[<tr><th>College Name</th><th>Rank</th><th>Overall Score</th><th>Rating</th><th>Ownership</th><th>Intake Exams</th><th></th></tr>,
<tr class="odd"><td><a href="https://www.careers360.com/university/indian-institute-of-management-ahmedabad">Indian Institute of Management Ahmedabad</a><br/></td><td><span class="serialNum circlerate Government"></span><span class="rankStyle">1</span></td><td><span class="overall_scoredata">427.92</span></td><td>AAAAA<div class="rankInfo"> <strong>2017 Rating: </strong> AAAAA</div></td><td><div class="ownership_name">Government</div><div class="rating_review rankInfo"><strong>User Rating: </strong>4.7 / 5</div></td><td><div class="showMoreCheck"> <input type="checkbox"/><div class="ranked_best_branch intakeExam"><div class="intakeExam ng-binding"><span class="best_branch plusMinus">CAT</span><ul><li>GMAT</li></ul></div></div></div></td><td><div class="rank-apply-button btnBlockInfo"><div class="flagging" id="divid-7057"><div class="flag-link flag-default-link"><a class="buttonDefault follow iframe-popup-button" href="/user/register?destination=colleges/ranking/2018&nid=7057&flag=bookmarks&click_location=follow_button&popup=iframe">Follow</a></div></div><div class="client_url"></div></div><div class="college-compare-checkbox combine-rating-block smallclListing"> <label> <input class="tmCheckbox" name="college_ranking" type="checkbox" value="7057"/><span></span> <i>Compare</i> </label></div></td></tr>,
<tr class="even"><td><a href="https://www.careers360.com/university/indian-institute-of-management-bangalore">Indian Institute of Management Bangalore</a><br/></td><td><span class="serialNum circlerate Government"></span><span class="rankStyle">2</span></td><td><span class="overall_scoredata">408.32</span></td><td>AAAAA<div class="rankInfo"> <strong>2017 Rating: </strong> AAAAA</div></td><td><div class="ownership_name">Government</div><div class="rating_review rankInfo"><strong>User Rating: </strong>4.1 / 5</div></td><td><div class="showMoreCheck"> <input type="checkbox"/><div class="ranked_best_branch intakeExam"><div class="intakeExam ng-binding"><span class="best_branch plusMinus">CAT</span><ul><li>GMAT</li></ul></div></div></div></td><td><div class="rank-apply-button btnBlockInfo"><div class="flagging" id="divid-6872"><div class="flag-link flag-default-link"><a class="buttonDefault follow iframe-popup-button" href="/user/register?destination=colleges/ranking/2018&nid=6872&flag=bookmarks&click_location=follow_button&popup=iframe">Follow</a></div></div><div class="client_url"></div></div><div class="college-compare-checkbox combine-rating-block smallclListing"> <label> <input class="tmCheckbox" name="college_ranking" type="checkbox" value="6872"/><span></span> <i>Compare</i> </label></div></td></tr>,
<tr class="odd"><td><a href="https://www.careers360.com/university/indian-institute-of-management-calcutta">Indian Institute of Management Calcutta</a><br/></td><td><span class="serialNum circlerate Government"></span><span class="rankStyle">3</span></td><td><span class="overall_scoredata">375.18</span></td><td>AAAAA<div class="rankInfo"> <strong>2017 Rating: </strong> AAAAA</div></td><td><div class="ownership_name">Government</div><div class="rating_review rankInfo"><strong>User Rating: </strong>4.9 / 5</div></td><td><div class="showMoreCheck"> <input type="checkbox"/><div class="ranked_best_branch intakeExam"><div class="intakeExam ng-binding"><span class="best_branch plusMinus">GMAT</span><ul><li>CAT</li></ul></div></div></div></td><td><div class="rank-apply-button btnBlockInfo"><div class="flagging" id="divid-6933"><div class="flag-link flag-default-link"><a class="buttonDefault follow iframe-popup-button" href="/user/register?destination=colleges/ranking/2018&nid=6933&flag=bookmarks&click_location=follow_button&popup=iframe">Follow</a></div></div><div class="client_url"></div></div><div class="college-compare-checkbox combine-rating-block smallclListing"> <label> <input class="tmCheckbox" name="college_ranking" type="checkbox" value="6933"/><span></span> <i>Compare</i> </label></div></td></tr>,
......
Upvotes: 4