Hawk81
Hawk81

Reputation: 137

BeautifulSoup - how to arrange data and write to txt?

New to Python, have a simple problem. I am pulling some data from Yahoo Fantasy Baseball to text file, but my code didn't work properly:

from bs4 import BeautifulSoup
import urllib2

teams = ("http://baseball.fantasysports.yahoo.com/b1/2282/players?status=A&pos=B&cut_type=33&stat1=S_S_2015&myteam=0&sort=AR&sdir=1")
page = urllib2.urlopen(teams)
soup = BeautifulSoup(page, "html.parser")

players = soup.findAll('div', {'class':'ysf-player-name Nowrap Grid-u Relative Lh-xs Ta-start'})

playersLines = [span.get_text('\t',strip=True) for span in players]

with open('output.txt', 'w') as f:
    for line in playersLines:
        line = playersLines[0]
        output = line.encode('utf-8')
        f.write(output)

In output file is only one player for 25 times. Any ideas to get result like this?

Pedro Álvarez   Pit - 1B,3B
Kevin Pillar    Tor - OF
Melky Cabrera   CWS - OF
etc

Upvotes: 1

Views: 62

Answers (1)

Joe Young
Joe Young

Reputation: 5875

Try removing: line = playersLines[0]

Also, append a newline character to the end of your output to get them to write to separate lines in the output.txt file:

from bs4 import BeautifulSoup
import urllib2

teams = ("http://baseball.fantasysports.yahoo.com/b1/2282/players?status=A&pos=B&cut_type=33&stat1=S_S_2015&myteam=0&sort=AR&sdir=1")
page = urllib2.urlopen(teams)
soup = BeautifulSoup(page, "html.parser")

players = soup.findAll('div', {'class':'ysf-player-name Nowrap Grid-u Relative Lh-xs Ta-start'})

playersLines = [span.get_text('\t',strip=True) for span in players]

with open('output.txt', 'w') as f:
    for line in playersLines:
        output = line.encode('utf-8')
        f.write(output+'\n')

Results:

Pedro Álvarez   Pit - 1B,3B
Kevin Pillar    Tor - OF
Melky Cabrera   CWS - OF
Ryan Howard     Phi - 1B
Michael A. Taylor       Was - OF
Joe Mauer       Min - 1B
Maikel Franco   Phi - 3B
Joc Pederson    LAD - OF
Yangervis Solarte       SD - 1B,2B,3B
César Hernández Phi - 2B,3B,SS
Eddie Rosario   Min - 2B,OF
Austin Jackson  Sea - OF
Danny Espinosa  Was - 1B,2B,3B,SS
Danny Valencia  Oak - 1B,3B,OF
Freddy Galvis   Phi - 3B,SS
Jimmy Paredes   Bal - 2B,3B
Colby Rasmus    Hou - OF
Luis Valbuena   Hou - 1B,2B,3B
Chris Young     NYY - OF
Kevin Kiermaier TB - OF
Steven Souza    TB - OF
Jace Peterson   Atl - 2B,3B
Juan Lagares    NYM - OF
A.J. Pierzynski Atl - C
Khris Davis     Mil - OF

Upvotes: 1

Related Questions