考虑以下简单的循环,它产生我想要的输出:
import itertools as it
for e in range (0, 16):
for i in it.chain(range(30, 53), range(1, 19)):
print(e, i)
我构建的这段代码的扩展版本没有产生预期的结果:
import json
import requests
import time
import datetime
import dateutil
import traceback
import os.path
import itertools as it
year_map = [2,89,213,359,542,803,1208,937,2025,2539,3115,4345,5476,6531,7794,9155,5826]
tournament_map = [26,114,243,421,579,903,1291,1645,2175,2689,3419,4940,5861,6978,8273]
years = [1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015]
for i in range (0, 16):
try:
url = ('http://www.whoscored.com/tournamentsfeed/',str(year_map[i]),'/Fixtures/')
url = ''.join(url)
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
'Host': 'www.whoscored.com',
'Referer': 'http://www.whoscored.com/'}
for f in it.chain(range(30, 53), range(1, 19)):
game_week = (str(years[i]), 'W', str(f))
game_week = ''.join(game_week)
params = {'d': game_week,
'isAggregate': 'false'}
response = requests.get(url, params=params, headers=headers)
response2 = (url, str(params))
response2 = ','.join(response2)
json_data = response.content.replace("'", '"').replace(',,', ',null,')
fixtures = json.loads(json_data)
if len(fixtures) > 1:
myfile = ",\n".join([", ".join(str(x) for x in fixture) for fixture in fixtures]) # `fixtures` is a nested list
print myfile
myfile2 = myfile + '\n'
time.sleep(20)
#create text file here
except Exception as exc:
print response2
print exc
print response.status_code
print response.text
pass
time.sleep(20)
print "-" *120
当我运行此版本的代码时,它只迭代两个链式范围中的第一个。在这种情况下,生成一个季节的八月到十二月期间浏览的足球数据部分的数据。然后代码停止执行而不会出错。
任何人都可以在代码中看到导致这种情况发生的任何事情吗?我很难过。
由于