Getting names from HTML file via python -


def get_players(team_id, year):     """     list of players in team in given year.     inputs:         team_id: string, three-letter id of team         year: integer, year     returns:         list of strings players' names     """     link = "http://espn.go.com/nba/team/stats/_/name/{}/year/{}/".format(team_id, year)     soup = soup_link(link)      game_stat_row = soup.find("tr", {"class": "colhead"})      player_names = []     name in game_stat_row:         player_names.append(name)      return player_names 

hi want player names url: http://espn.go.com/nba/team/stats/_/name/por/year/2015/ giving html text not list of players. need find first occurence of each name , once. can me. thanks. using beautifulsoup way.

i use different way html code here

source = urllib2.urlopen(link) html = source.read() source.close(); soup = beautifulsoup(html, "html.parser")   

since noticed rows end number used regex extract rows

players_table = soup.find_all("tr",{"class" : re.compile(r"\d+$")}) 

here code

from bs4 import beautifulsoup  import urllib2 import re  def get_players(team_id, year):     link = "http://espn.go.com/nba/team/stats/_/name/{}/year/{}/".format(team_id, year)     source = urllib2.urlopen(link)     html = source.read()     source.close();     soup = beautifulsoup(html, "html.parser")       players_table = soup.find_all("tr",{"class" : re.compile(r"\d+$")})     player_names = []     table in players_table:         in table.find_all('a'):             player_names.append(a.get_text())      return player_names 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -