如果值为' 0'
,有人可以告诉我在哪里申请IF语句例如:
24 May 2015
Premier League
Aston Villa 0 - Burnley 1
6H,6A,
输出不正确,因为没有6H只有6A - 所以对于家庭和离开我需要应用IF语句不发布任何东西如果homeScore = 0
我的完整代码在这里
import requests
from bs4 import BeautifulSoup
import csv
import re
from collections import OrderedDict
import time
def parse_page(data):
#Sleep to avoid excessive requests
time.sleep(5)
subsoup = BeautifulSoup(data)
rs = requests.get("http://www.bbc.co.uk/sport/0/football/32770767")
ssubsoup = BeautifulSoup(rs.content)
matchoverview = subsoup.find('div', attrs={'id':'match-overview'})
print '--------------'
date = subsoup.find('div', attrs={'id':'article-sidebar'}).findNext('span').text
league = subsoup.find('a', attrs={'class':'secondary-nav__link'}).findNext('span').findNext('span').text
#HomeTeam info printing
homeTeam = matchoverview.find('div', attrs={'class':'team-match-details'}).findNext('span').findNext('a').text
homeScore = matchoverview.find('div', attrs={'class':'team-match-details'}).findNext('span').findNext('span').text
homeGoalScorers = []
for goals in matchoverview.find('div', attrs={'class':'team-match-details'}).findNext('p').find_all('span'):
homeGoalScorers.append(goals.text.replace(u'\u2032', "'"))
homeGoals = homeGoalScorers
homeGoals2 = ''.join(homeGoals)
homeGoals3 = re.sub("[^0-9']","",homeGoals2)
homeGoals4 = homeGoals3.replace("'","',")
homeGoals5 = homeGoals4.replace("'","H")
#AwayTeam info printing
awayTeam = matchoverview.find('div', attrs={'id': 'away-team'}).find('div', attrs={'class':'team-match-details'}).findNext('span').findNext('a').text
awayScore = matchoverview.find('div', attrs={'id': 'away-team'}).find('div', attrs={'class':'team-match-details'}).findNext('span').findNext('span').text
awayGoalScorers = []
for goals in matchoverview.find('div', attrs={'id': 'away-team'}).find('div', attrs={'class':'team-match-details'}).findNext('p').find_all('span'):
awayGoalScorers.append(goals.text.replace(u'\u2032', "'"))
awayGoals = awayGoalScorers
awayGoals2 = ''.join(awayGoals)
awayGoals3 = re.sub("[^0-9']","",awayGoals2)
awayGoals4 = awayGoals3.replace("'","',")
awayGoals5 = awayGoals4.replace("'","A")
scores = homeGoals5+awayGoals5
#Printouts
print date
print league
print '{0} {1} - {2} {3}'.format(homeTeam, homeScore, awayTeam, awayScore)
print scores
if len(homeTeam) >1:
with open('score.txt', 'a') as f:
writer = csv.writer(f)
writer.writerow([league,date,homeTeam,awayTeam,scores])
def all_league_results():
r = requests.get("http://www.bbc.co.uk/sport/football/premier-league/results")
soup = BeautifulSoup(r.content)
# Save Teams
for link in soup.find_all("a", attrs={'class': 'report'}):
fullLink = 'http://www.bbc.com' + link['href']
time.sleep(5)
subr = requests.get(fullLink)
parse_page(subr.text)
def specific_game_results(url):
subr = requests.get(url)
parse_page(subr.text)
#get specific games results
specific_game_results('http://www.bbc.co.uk/sport/0/football/32770767')
答案 0 :(得分:0)
如果我理解你想要正确做什么,你可以简单地在if语句中移动打印分数,检查homeGoals值是否大于0.
如果是家庭目标: 打印分数
如果主场和客场得分都需要大于0,则只需添加另一个if语句。
如果是家庭目标: 如果离开目标: 打印分数