我有一些有用的东西,但我知道有更好的方法可以做到这一点,我很想听听你的意见。
以下代码每5秒检查一次远程XML文件x次尝试,并查找特定游戏中得分的更改,并在分数发生变化时输出消息。
有什么更好的方法可以做到这一点?
from xml.dom import minidom
import time
import urllib
import urllib2
import requests
url = 'http://www.*******.com/soccer/livescore/livescore.xml'
r = requests.get(url)
with open("test.xml", "wb") as code:
code.write(r.content)
#Config
curr_score = 0
playing = 'home'
team = 'Everton'
for x in xrange(1320):
xmldoc = minidom.parse('test.xml')
gameplayedat = xmldoc.getElementsByTagName(playing)
for s in gameplayedat :
if s.attributes['name'].value == team :
if s.attributes['goals'].value > curr_score :
print "Score increased " + " - " + s.attributes['goals'].value
curr_score = s.attributes['goals'].value
else:
print "Score the same " + " - " + s.attributes['goals'].value
curr_score = s.attributes['goals'].value
time.sleep(5)
XML的布局如下:
<livescore updated="19.01.2015 17:20:21" sport="soccer">
<league country="africa" name="Africa: Africa Cup Of Nations" cup="True" id="2892" sub_id="28920">
<match alternate_id="4085749" alternate_id_2="4295052" commentary="True" date="19.01.2015" id="4203316" static_id="19011523425322346965" status="62" time="16:00">
<home goals="1" id="2342532" name="Ghana"/>
<away goals="1" id="2346965" name="Senegal"/>
<events>
<event assist="" assistid="" extra_min="" id="21964024" minute="13" player="B. Coundoul" playerid="2338774" result="" team="away" type="yellowcard"/>
<event assist="" assistid="" extra_min="" id="21964025" minute="14" player="A. Ayew (pen.)" playerid="2352405" result="[1 - 0]" team="home" type="goal"/>
<event assist="" assistid="" extra_min="" id="21964026" minute="25" player="D. N'Doye" playerid="2350975" result="" team="away" type="yellowcard"/>
<event assist="D. N'Doye" assistid="19142" extra_min="" id="21964027" minute="58" player="M. Diouf" playerid="2360781" result="[1 - 1]" team="away" type="goal"/>
</events>
<ht score="[1-0]"/>
</match>
<match alternate_id="4085750" alternate_id_2="4295053" commentary="True" date="19.01.2015" id="4203317" static_id="19011523375672347374" status="19:00" time="19:00">
<home goals="?" id="2337567" name="Algeria"/>
<away goals="?" id="2347374" name="South Africa"/>
<events/>
<ht score=""/>
</match>
</league>
</livescore