Home > Blockchain >  Web scraping specific td tag from a table with python
Web scraping specific td tag from a table with python

Time:11-27

I am trying to extract the text from the first <td> tag but there are multiple identical class tags in a row which I am having trouble extracting a single one (the final golf score from the golfer, -19 in the example below). I cannot get python to pick it up at all. I have it picking up the golfers name, but that's it. Only want their final score, and bypass the rest, then on to the next golfer.

I am new to python and I understand my variables in code below are not the best practice. Trying to scrape:

<tr >
  <td >
    <svg aria-hidden="true"  viewBox="0 0 24 24"><use xlink:href="#icon__caret__down"></use></svg></td>
  <td >1</td>
  <td >
    <img src="https://a.espncdn.com/combiner/i?img=/i/teamlogos/countries/500/can.png&amp;w=40&amp;h=40&amp;scale=crop" alt="Canada" >
    <a  tabindex="0" href="http://www.espn.com/golf/player/_/id/9127/adam-svensson">A.  Svensson</a>
  </td>
  <td >-19</td>
  <td >73</td>
  <td >64</td>
  <td >62</td>
  <td >64</td>
  <td >263</td>
  <td >$1,458,000</td>
  <td >500</td></tr>

Tried:

from bs4 import BeautifulSoup
import requests

html_text = requests.get('https://www.espn.com/golf/leaderboard/_/tournamentId/401465506').text

soup = BeautifulSoup(html_text, 'lxml')
golfers = soup.find_all('tr', class_ = 'PlayerRow__Overview PlayerRow__Overview--expandable Table__TR Table__even')
for golfer in golfers:
    golfer_name = golfer.a.text
    golfer_score = golfer.td.text
    print(f'{golfer_name} final score {golfer_score}')

Result:

Traceback (most recent call last):
  File "C:\Users\mikef\AppData\Local\Temp\tempCodeRunnerFile.python", line 10, in <module>
    golfer_score = golfer.td = 'Table__TD'.text
AttributeError: 'str' object has no attribute 'text'

[Done] exited with code=1 in 0.559 seconds

If I comment out the golfer_score then I get a list of all the golfers like I'm intending.

Source code can be found at https://www.espn.com/golf/leaderboard/_/tournamentId/401465506

CodePudding user response:

You can try the next example using CSS selectors correctly

from bs4 import BeautifulSoup
import requests

html_text = requests.get('https://www.espn.com/golf/leaderboard/_/tournamentId/401465506').text

soup = BeautifulSoup(html_text, 'lxml')
golfers = soup.find_all('tr', class_ = 'PlayerRow__Overview PlayerRow__Overview--expandable Table__TR Table__even')
for golfer in golfers:
    golfer_name = golfer.select_one('a[]').get_text(strip=True)
    golfer_score = golfer.select_one('td.Table__TD:nth-child(4)').get_text(strip=True)
    print(f'{golfer_name} final score {golfer_score}')

Output:

Adam Svensson final score -19
Callum Tarren final score -17
Brian Harman final score -17
Sahith Theegala final score -17
Joel Dahmen final score -15
Cole Hammer final score -15
Chris Stroud final score -15
Seamus Power final score -15
Alex Smalley final score -15
Robby Shelton final score -14
David Lingmerth final score -14
Erik Barnes final score -14
Wyndham Clark final score -14
Patrick Rodgers final score -14
J.J. Spaun final score -13
Greyson Sigg final score -13
Seung-Yul Noh final score -13
Will Gordon final score -13
Taylor Pendrith final score -13
Taylor Montgomery final score -13
J.T. Poston final score -12
Russell Knox final score -12
Danny Lee final score -12
Ben Taylor final score -12
Beau Hossler final score -12
Harry Higgs final score -12
Andrew Putnam final score -12
Ben Martin final score -12
Kevin Kisner final score -11
Zac Blair final score -11
Ben Griffin final score -11
Harris English final score -11
Justin Rose final score -11
Paul Haley II final score -11
Chris Gotterup final score -10
Michael Kim final score -10
Patton Kizzire final score -10
Kevin Streelman final score -10
Hayden Buckley final score -9
Keith Mitchell final score -9
Aaron Baddeley final score -9
Henrik Norlander final score -9
Eric Cole final score -9
Carl Yuan final score -9
Akshay Bhatia final score -8
Denny McCarthy final score -7
Kevin Roy final score -7
Brice Garnett final score -7
Davis Riley final score -7
Jim Herman final score -7
Stephan Jaeger final score -7
Dylan Wu final score -7
Ryan Armour final score -7
Martin Trainer final score -6
Trevor Cone final score -6
Scott Stallings final score -6
Dean Burmester final score -6
Brandon Wu final score -6
Kevin Yu final score -6
Brent Grant final score -6
Jacob Bridgeman final score -6
Matthias Schwab final score -5
Tyson Alexander final score -5
Joseph Bramlett final score -4
Doc Redman final score -4
Justin Suh final score -3
Zecheng Dou final score -1
Andrew Landry final score -1
MJ Daffue final score  1
Kevin Tway final score CUT
Sam Ryder final score CUT
Stewart Cink final score CUT
Brendon Todd final score CUT
Vincent Norrman final score CUT
Dylan Frittelli final score CUT
Adam Long final score CUT
Jason Dufner final score CUT
Chesson Hadley final score CUT
Nick Hardy final score CUT
Rory Sabbatini final score CUT
Seonghyeon Kim final score CUT
Sam Stevens final score CUT
Matt Kuchar final score CUT
Robert Streb final score CUT
Mackenzie Hughes final score CUT
Nicolas Echavarria final score CUT
Spencer Ralston final score CUT
Nate Lashley final score CUT
Tyler Duncan final score CUT
Peter Malnati final score CUT
Kyle Westmoreland final score CUT
Adam Schenk final score CUT
Brian Gay final score CUT
Chris Kirk final score CUT
Sung Kang final score CUT
Matthew NeSmith final score CUT
Zach Johnson final score CUT
Webb Simpson final score CUT
Cameron Percy final score CUT
Conner Godsey final score CUT
Chad Ramey final score CUT
Scott Piercy final score CUT
Sean O'Hair final score CUT
John Huh final score CUT
Francesco Molinari final score CUT
Lee Hodges final score CUT
Jason Day final score CUT
Brandon Matthews final score CUT
Harry Hall final score CUT
Kelly Kraft final score CUT
Charley Hoffman final score CUT
Nick Watney final score CUT
Brett Drewitt final score CUT
Byeong Hun An final score CUT
Camilo Villegas final score CUT
Justin Lower final score CUT
Harrison Endycott final score CUT
Carson Young final score CUT
Troy Merritt final score CUT
Garrick Higgo final score CUT
Aaron Rai final score CUT
Tom Hoge final score CUT
Luke List final score CUT
Andrew Novak final score CUT
Matt Wallace final score CUT
Ryan Brehm final score CUT
Sepp Straka final score CUT
Michael Thompson final score CUT
Austin Smotherman final score CUT
Bill Haas final score CUT
Austin Cook final score CUT
Jonathan Byrd final score CUT
Michael Gligic final score CUT
Cameron Champ final score CUT
Matti Schmid final score CUT
Hank Lebioda final score CUT
Davis Thompson final score CUT
Taylor Moore final score CUT
Tim Weinhart final score CUT
Augusto Núñez final score CUT
Kevin Chappell final score CUT
Palmer Jackson (a) final score CUT
Vaughn Taylor final score CUT
Scott Harrington final score CUT
Trevor Werbylo final score CUT
Max McGreevy final score CUT
Scott Brown final score CUT
Philip Knowles final score CUT
Brian Stuard final score CUT
Tano Goya final score CUT
Richy Werenski final score CUT
Bryson Nimmer final score CUT
Danny Willett final score WD
Trey Mullinax final score WD
David Lipsky final score WD
  • Related