我是Python的新手,而且我已经编写了一个刮刀,可以打印出我需要它的确切方式的数据,但是我在将数据写入文件时遇到了麻烦。我需要它看起来完全相同的方式,并且与在IDLE中打印时的顺序相同
import requests
import re
from bs4 import BeautifulSoup
year_entry = raw_input("Enter year: ")
week_entry = raw_input("Enter week number: ")
week_link = requests.get("http://sports.yahoo.com/nfl/scoreboard/?week=" + week_entry + "&phase=2&season=" + year_entry)
page_content = BeautifulSoup(week_link.content)
a_links = page_content.find_all('tr', {'class': 'game link'})
for link in a_links:
r = 'http://www.sports.yahoo.com' + str(link.attrs['data-url'])
r_get = requests.get(r)
soup = BeautifulSoup(r_get.content)
stats = soup.find_all("td", {'class':'stat-value'})
teams = soup.find_all("th", {'class':'stat-value'})
scores = soup.find_all('dd', {"class": 'score'})
try:
game_score = scores[-1]
game_score = game_score.text
x = game_score.split(" ")
away_score = x[1]
home_score = x[4]
home_team = teams[1]
away_team = teams[0]
away_team_stats = stats[0::2]
home_team_stats = stats[1::2]
print away_team.text + ',' + away_score + ',',
for stats in away_team_stats:
print stats.text + ',',
print '\n'
print home_team.text + ',' + home_score +',',
for stats in home_team_stats:
print stats.text + ',',
print '\n'
except:
pass
我完全不知道如何将它打印到txt文件,就像它在IDLE中打印一样。该代码仅适用于NFL赛季的完整周。因此,如果您测试代码,我建议年= 2014年和周= 12(或之前)
谢谢,
JT
答案 0 :(得分:1)
要写入文件,您需要将该行构建为字符串,然后将该行写入文件。
您可以使用以下内容:
# Open/create a file for your output
with open('my_output_file.csv', 'wb') as csv_out:
...
# Your BeautifulSoup code and parsing goes here
...
# Then build up your output strings
for link in a_links:
away_line = ",".join([away_team.text, away_score])
for stats in away_team_stats:
away_line += [stats.text]
home_line = ",".join(home_team.text, home_score])
for stats in home_team_stats:
home_line += [stats.text]
# Write your output strings to the file
csv_out.write(away_line + '\n')
csv_out.write(home_line + '\n')
这是一个快速而肮脏的修复方法。要正确执行此操作,您可能需要查看csv
模块(docs)
答案 1 :(得分:0)
从输出的结构我同意Jamie的看法,使用CSV是一个合理的选择。
但是,由于您使用的是Python 2,因此可能使用替代形式的print语句打印到文件。
来自https://docs.python.org/2/reference/simple_stmts.html#the-print-statement
print也有一个扩展形式,由第二部分定义 语法如上所述。这种形式有时被称为“印刷品” chevron。“在这种形式下,>>之后的第一个表达式。必须 评估为“类文件”对象,特别是具有 write()方法如上所述。有了这种扩展形式, 后续表达式将打印到此文件对象。如果是第一个 表达式求值为None,然后sys.stdout用作文件 输出
例如,
outfile = open("myfile.txt", "w")
print >>outfile, "Hello, world"
outfile.close()
但是,Python 3不支持这种语法,所以我认为使用它可能不是一个好主意。 :) FWIW,我在编写文件时通常在代码中使用文件write()方法,但我倾向于使用print >>sys.stderr
来获取错误消息。