python连续写入文件_Python:将多个变量写入文件

我对Python相当陌生,而且我写了一个刮板来打印数据,我放弃了我需要的确切方式,但是我无法将数据写入文件。我需要它看起来完全相同的方式,并以相同的顺序,因为它确实当它在空闲Python:将多个变量写入文件

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

2014-12-02

J.T.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值