2015-11-02 43 views
0

拉我的头发了一下,试图找出如何让我可以使用我的刮取数据。基本上我想刮的数据是这样的:美丽的汤,订购我提取的数据

DateTime_match,DateTime_odds,sport,team,team_type,odds 

05/11/2015 16:00:00,01/11/2015 10:37:58,NFL,New York Giants,home,1.21 

05/11/2015 16:00:00,01/11/2015 10:37:58,NFL,New Orleans,away,3.5 

我已经放在一起的Python代码为我想要的元素完成了艰难的部分已经,只是需要一些帮助组织它。

我的Python代码:

这是输出:

NFL Matches 
Indianapolis ColtsCarolina Panthers3.331.35 
Cleveland BrownsCincinnati Bengals5.651.16 
Miami DolphinsBuffalo Bills2.301.65 
Washington RedskinsNew England Patriots10.501.06 
Oakland RaidersPittsburgh Steelers2.941.43 
St. Louis RamsMinnesota Vikings2.251.68 
Atlanta FalconsSan Francisco 49ers1.502.69 
New York GiantsTampa Bay Buccaneers1.802.06 
Philadelphia EaglesDallas Cowboys1.782.09 
Chicago BearsSan Diego Chargers2.711.49 
Indianapolis Colts At Carolina Panthers12:30 
Cleveland Browns At Cincinnati Bengals12:25 
Miami Dolphins At Buffalo Bills05:00 
Washington Redskins At New England Patriots05:00 
Oakland Raiders At Pittsburgh Steelers05:00 
St. Louis Rams At Minnesota Vikings05:00 
Atlanta Falcons At San Francisco 49ers08:05 
New York Giants At Tampa Bay Buccaneers08:05 
Philadelphia Eagles At Dallas Cowboys12:30 
Chicago Bears At San Diego Chargers12:30 
Tuesday 03/11/2015 
Friday 06/11/2015 
Monday 09/11/2015 
Tuesday 10/11/2015 
+1

什么是输出,为什么不是你想要的? –

+1

嗨@PeterWood我更新了问题以包括输出。困难的部分是试图将日期嵌套到所有类中。 – Jimmyn

+0

输出看起来不像所需的输出。我看不出你怎么能把一个转换成另一个。 –

回答

1

您需要存储在变量中的数据,然后组织他们和打印正确的输出。使用print将在调用数据时打印数据。使用变量,列表,字典等,然后建立你想要的输出。

。例如,而不是打印的团队和价格像你这样的一行在这里做:在变量

print teama.text + teamb.text + pricea.text.strip() + priceb.text.strip() 

存储数据:

team_a = teama.text 
team_b = teamb.text 
price_a = pricea.text.strip() 
price_b = priceb.text.strip() 

甚至被比赛(使用字典):

match = { 
    'team_a' : teama.text 
    'team_b' : teamb.text 
    'price_a' : pricea.text.strip() 
    'price_b' : priceb.text.strip() 
} 

然后打印它为哟ü希望:

# With variables 
print '%s,%s,%s,%s' % (team_a, team_b, price_a, price_b) 

# With dictionary 
print '%s,%s,%s,%s' % (match['team_a'], match['team_b'], match['price_a'], match['price_b']) 

通过这样做,你会得到:

Indianapolis Colts,Carolina Panthers,3.33,1.35 

有关如何格式化Python字符串检查this更多的信息。