将if/elif与来自网页的Python列表响应一起使用

将if/elif与来自网页的Python列表响应一起使用,python,scrapy,Python,Scrapy,我在Windows Vista 64位上使用Python.org 2.7 64位版本。我先前提出的一个与此相关的问题在一些评论中变得有些混乱,因此我认为最好重新开始。我有下面的代码,它模拟了一系列带日期戳的XHR请求,请求一个网页来获取其中包含的数据: from datetime import date, timedelta as td from ast import literal_eval from datetime import datetime import requests impor

我在Windows Vista 64位上使用Python.org 2.7 64位版本。我先前提出的一个与此相关的问题在一些评论中变得有些混乱,因此我认为最好重新开始。我有下面的代码,它模拟了一系列带日期戳的XHR请求,请求一个网页来获取其中包含的数据:

from datetime import date, timedelta as td
from ast import literal_eval
from datetime import datetime
import requests
import time
import re

d1 = date(2013,11,01)
d2 = date(2014,5,31)

delta = d2 - d1


for i in range(delta.days + 1):
    time1 =  str(d1 + td(days=i))
    time2 = time1.split("-", 1)[0]
    time3 = time1.split("-", -1)[1]
    time4 = time1.rsplit("-", 1)[-1]

    time2 = int(time2)
    time3 = int(time3)
    time4 = int(time4)

    date = datetime(year=time2, month=time3, day=time4)


    url = 'http://www.whoscored.com/tournamentsfeed/8273/Fixtures/'

    params = {'d': date.strftime('%Y%m%d'), 'isAggregate': 'false'}
    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'}

    response = requests.get(url, params=params, headers=headers)


    try:
        fixtures = literal_eval(response.content)
        fixtures = str(fixtures)

        if fixtures is not None:

            fixtures = re.sub("\[", '', fixtures)
            fixtures = re.sub("\], ", ',\n', fixtures)
            fixtures = re.sub("\'", '', fixtures)
            fixtures = re.sub("\]", '', fixtures)

            print fixtures 
            time.sleep(1)

        elif fixtures == []:

           print "No Fixtures Today: " + date
           time.sleep(1) 

    except SyntaxError:

        print "Error!!!"
        time.sleep(1)
并非该范围内的每个日期都有数据。如果请求的日期没有数据,则响应将按如下方式打印“[]”。我被告知返回的数据是一个Python列表,而不是json

鉴于我已将变量'fixtures'转换为字符串,我认为逻辑'elif fixtures==[]足以打印我所需的内容,但这不起作用

有谁能建议我应该做些什么改变,以便在我的elif声明中打印“今日无固定装置:”日期


谢谢

除非我有理由保持列表的原样,否则我不确定您为什么要将其作为字符串。 无论如何,这将起作用:

try:
    fixtures = literal_eval(response.content)
    if fixtures: # will be True if there is no falsey values like [] etc..
        fixtures = str(fixtures) # make string in here where needed

        fixtures = re.sub("\[", '', fixtures)
        fixtures = re.sub("\], ", ',\n', fixtures)
        fixtures = re.sub("\'", '', fixtures)
        fixtures = re.sub("\]", '', fixtures)

        print fixtures
        time.sleep(1)
    else:
       print "No Fixtures Today: " + date.isoformat() # cannot use `date` it is not a string
       time.sleep(1)
产出:

No Fixtures Today: 2013-11-01T00:00:00
No Fixtures Today: 2013-11-02T00:00:00
No Fixtures Today: 2013-11-03T00:00:00
No Fixtures Today: 2013-11-04T00:00:00
No Fixtures Today: 2013-11-05T00:00:00
No Fixtures Today: 2013-11-06T00:00:00
No Fixtures Today: 2013-11-07T00:00:00
785460, 1, Friday, Nov 8 2013, 19:45, 185, Bristol Rovers, 0, 100, York, 0, 3 : 3, 1 : 2, 1, 0, FT, 0, 0, 0, 0, 0,
785442, 1, Friday, Nov 8 2013, 20:00, 5955, AFC Wimbledon, 0, 17, Coventry, 0, 1 : 3, 0 : 0, 1, 0, FT, 2, 0, 0, 0, 0
785433, 1, Saturday, Nov 9 2013, 13:00, 164, Grimsby, 0, 186, Scunthorpe, 0, 0 : 0, 0 : 0, 0, 0, FT, 0, 0, 0, 0, 0,
785429, 1, Saturday, Nov 9 2013, 14:00, 1976, Boreham Wood, 0, 322, Carlisle, 0, 0 : 0, 0 : 0, 0, 0, FT, 0, 0, 0, 0, 0,
785430, 1, Saturday, Nov 9 2013, 15:00, 1978, St.Albans, 0, 99, Mansfield, 0, 1 : 8, 1 : 2, 1, 0, FT, 2, 0, 0, 0, 0,
785431, 1, Saturday, Nov 9 2013, 15:00, 28, Milton Keynes Dons, 0, 201, Halifax, 0, 4 : 1, 1 : 0, 1, 0, FT, 1, 0, 0, 0, 0,
785432, 1, Saturday, Nov 9 2013, 15:00, 1386, Tamworth, 0, 258, Cheltenham, 0, 1 : 0, 1 : 0, 1, 0, FT, 1, 0, 0, 0, 0,...
如果您希望日期如2013年11月1日星期五00:00:00

使用
date.ctime()

if fixtures:
如果您的数据返回一个
空列表
字符串
元组
无等,则将为False。
python中的
。因此,如果fixtures包含数据,它将打印它
,否则它将
打印“今天没有fixtures:”

In [1]: l =[]

In [2]: if l:
   ...:     print "Foo" # no output
   ...:     

In [3]: l =[1] # now l has an element

In [4]: if l:
    print "Foo" # this time `if l` is True so we get output
   .
  ..:     
  Foo

In [7]: l = []
In [8]: l is None
Out[8]: False

除非有原因,我会保持列表的原样,否则我不确定您为什么要将其作为字符串。 无论如何,这将起作用:

try:
    fixtures = literal_eval(response.content)
    if fixtures: # will be True if there is no falsey values like [] etc..
        fixtures = str(fixtures) # make string in here where needed

        fixtures = re.sub("\[", '', fixtures)
        fixtures = re.sub("\], ", ',\n', fixtures)
        fixtures = re.sub("\'", '', fixtures)
        fixtures = re.sub("\]", '', fixtures)

        print fixtures
        time.sleep(1)
    else:
       print "No Fixtures Today: " + date.isoformat() # cannot use `date` it is not a string
       time.sleep(1)
产出:

No Fixtures Today: 2013-11-01T00:00:00
No Fixtures Today: 2013-11-02T00:00:00
No Fixtures Today: 2013-11-03T00:00:00
No Fixtures Today: 2013-11-04T00:00:00
No Fixtures Today: 2013-11-05T00:00:00
No Fixtures Today: 2013-11-06T00:00:00
No Fixtures Today: 2013-11-07T00:00:00
785460, 1, Friday, Nov 8 2013, 19:45, 185, Bristol Rovers, 0, 100, York, 0, 3 : 3, 1 : 2, 1, 0, FT, 0, 0, 0, 0, 0,
785442, 1, Friday, Nov 8 2013, 20:00, 5955, AFC Wimbledon, 0, 17, Coventry, 0, 1 : 3, 0 : 0, 1, 0, FT, 2, 0, 0, 0, 0
785433, 1, Saturday, Nov 9 2013, 13:00, 164, Grimsby, 0, 186, Scunthorpe, 0, 0 : 0, 0 : 0, 0, 0, FT, 0, 0, 0, 0, 0,
785429, 1, Saturday, Nov 9 2013, 14:00, 1976, Boreham Wood, 0, 322, Carlisle, 0, 0 : 0, 0 : 0, 0, 0, FT, 0, 0, 0, 0, 0,
785430, 1, Saturday, Nov 9 2013, 15:00, 1978, St.Albans, 0, 99, Mansfield, 0, 1 : 8, 1 : 2, 1, 0, FT, 2, 0, 0, 0, 0,
785431, 1, Saturday, Nov 9 2013, 15:00, 28, Milton Keynes Dons, 0, 201, Halifax, 0, 4 : 1, 1 : 0, 1, 0, FT, 1, 0, 0, 0, 0,
785432, 1, Saturday, Nov 9 2013, 15:00, 1386, Tamworth, 0, 258, Cheltenham, 0, 1 : 0, 1 : 0, 1, 0, FT, 1, 0, 0, 0, 0,...
如果您希望日期如2013年11月1日星期五00:00:00

使用
date.ctime()

if fixtures:
如果您的数据返回一个
空列表
字符串
元组
等,则将为False。
python中的
。因此,如果fixtures包含数据,它将打印它
,否则它将
打印“今天没有fixtures:”

In [1]: l =[]

In [2]: if l:
   ...:     print "Foo" # no output
   ...:     

In [3]: l =[1] # now l has an element

In [4]: if l:
    print "Foo" # this time `if l` is True so we get output
   .
  ..:     
  Foo

In [7]: l = []
In [8]: l is None
Out[8]: False

除非有原因,我会保持列表的原样,否则我不确定您为什么要将其作为字符串。 无论如何,这将起作用:

try:
    fixtures = literal_eval(response.content)
    if fixtures: # will be True if there is no falsey values like [] etc..
        fixtures = str(fixtures) # make string in here where needed

        fixtures = re.sub("\[", '', fixtures)
        fixtures = re.sub("\], ", ',\n', fixtures)
        fixtures = re.sub("\'", '', fixtures)
        fixtures = re.sub("\]", '', fixtures)

        print fixtures
        time.sleep(1)
    else:
       print "No Fixtures Today: " + date.isoformat() # cannot use `date` it is not a string
       time.sleep(1)
产出:

No Fixtures Today: 2013-11-01T00:00:00
No Fixtures Today: 2013-11-02T00:00:00
No Fixtures Today: 2013-11-03T00:00:00
No Fixtures Today: 2013-11-04T00:00:00
No Fixtures Today: 2013-11-05T00:00:00
No Fixtures Today: 2013-11-06T00:00:00
No Fixtures Today: 2013-11-07T00:00:00
785460, 1, Friday, Nov 8 2013, 19:45, 185, Bristol Rovers, 0, 100, York, 0, 3 : 3, 1 : 2, 1, 0, FT, 0, 0, 0, 0, 0,
785442, 1, Friday, Nov 8 2013, 20:00, 5955, AFC Wimbledon, 0, 17, Coventry, 0, 1 : 3, 0 : 0, 1, 0, FT, 2, 0, 0, 0, 0
785433, 1, Saturday, Nov 9 2013, 13:00, 164, Grimsby, 0, 186, Scunthorpe, 0, 0 : 0, 0 : 0, 0, 0, FT, 0, 0, 0, 0, 0,
785429, 1, Saturday, Nov 9 2013, 14:00, 1976, Boreham Wood, 0, 322, Carlisle, 0, 0 : 0, 0 : 0, 0, 0, FT, 0, 0, 0, 0, 0,
785430, 1, Saturday, Nov 9 2013, 15:00, 1978, St.Albans, 0, 99, Mansfield, 0, 1 : 8, 1 : 2, 1, 0, FT, 2, 0, 0, 0, 0,
785431, 1, Saturday, Nov 9 2013, 15:00, 28, Milton Keynes Dons, 0, 201, Halifax, 0, 4 : 1, 1 : 0, 1, 0, FT, 1, 0, 0, 0, 0,
785432, 1, Saturday, Nov 9 2013, 15:00, 1386, Tamworth, 0, 258, Cheltenham, 0, 1 : 0, 1 : 0, 1, 0, FT, 1, 0, 0, 0, 0,...
如果您希望日期如2013年11月1日星期五00:00:00

使用
date.ctime()

if fixtures:
如果您的数据返回一个
空列表
字符串
元组
等,则将为False。
python中的
。因此,如果fixtures包含数据,它将打印它
,否则它将
打印“今天没有fixtures:”

In [1]: l =[]

In [2]: if l:
   ...:     print "Foo" # no output
   ...:     

In [3]: l =[1] # now l has an element

In [4]: if l:
    print "Foo" # this time `if l` is True so we get output
   .
  ..:     
  Foo

In [7]: l = []
In [8]: l is None
Out[8]: False

除非有原因,我会保持列表的原样,否则我不确定您为什么要将其作为字符串。 无论如何,这将起作用:

try:
    fixtures = literal_eval(response.content)
    if fixtures: # will be True if there is no falsey values like [] etc..
        fixtures = str(fixtures) # make string in here where needed

        fixtures = re.sub("\[", '', fixtures)
        fixtures = re.sub("\], ", ',\n', fixtures)
        fixtures = re.sub("\'", '', fixtures)
        fixtures = re.sub("\]", '', fixtures)

        print fixtures
        time.sleep(1)
    else:
       print "No Fixtures Today: " + date.isoformat() # cannot use `date` it is not a string
       time.sleep(1)
产出:

No Fixtures Today: 2013-11-01T00:00:00
No Fixtures Today: 2013-11-02T00:00:00
No Fixtures Today: 2013-11-03T00:00:00
No Fixtures Today: 2013-11-04T00:00:00
No Fixtures Today: 2013-11-05T00:00:00
No Fixtures Today: 2013-11-06T00:00:00
No Fixtures Today: 2013-11-07T00:00:00
785460, 1, Friday, Nov 8 2013, 19:45, 185, Bristol Rovers, 0, 100, York, 0, 3 : 3, 1 : 2, 1, 0, FT, 0, 0, 0, 0, 0,
785442, 1, Friday, Nov 8 2013, 20:00, 5955, AFC Wimbledon, 0, 17, Coventry, 0, 1 : 3, 0 : 0, 1, 0, FT, 2, 0, 0, 0, 0
785433, 1, Saturday, Nov 9 2013, 13:00, 164, Grimsby, 0, 186, Scunthorpe, 0, 0 : 0, 0 : 0, 0, 0, FT, 0, 0, 0, 0, 0,
785429, 1, Saturday, Nov 9 2013, 14:00, 1976, Boreham Wood, 0, 322, Carlisle, 0, 0 : 0, 0 : 0, 0, 0, FT, 0, 0, 0, 0, 0,
785430, 1, Saturday, Nov 9 2013, 15:00, 1978, St.Albans, 0, 99, Mansfield, 0, 1 : 8, 1 : 2, 1, 0, FT, 2, 0, 0, 0, 0,
785431, 1, Saturday, Nov 9 2013, 15:00, 28, Milton Keynes Dons, 0, 201, Halifax, 0, 4 : 1, 1 : 0, 1, 0, FT, 1, 0, 0, 0, 0,
785432, 1, Saturday, Nov 9 2013, 15:00, 1386, Tamworth, 0, 258, Cheltenham, 0, 1 : 0, 1 : 0, 1, 0, FT, 1, 0, 0, 0, 0,...
如果您希望日期如2013年11月1日星期五00:00:00

使用
date.ctime()

if fixtures:
如果您的数据返回一个
空列表
字符串
元组
等,则将为False。
python中的
。因此,如果fixtures包含数据,它将打印它
,否则它将
打印“今天没有fixtures:”

In [1]: l =[]

In [2]: if l:
   ...:     print "Foo" # no output
   ...:     

In [3]: l =[1] # now l has an element

In [4]: if l:
    print "Foo" # this time `if l` is True so we get output
   .
  ..:     
  Foo

In [7]: l = []
In [8]: l is None
Out[8]: False
我打赌你应该使用:

elif fixtures == '[]':
fixture是一个字符串,因此字符串应该是带引号的“[]”。

我打赌您应该使用:

elif fixtures == '[]':
fixture是一个字符串,因此字符串应该是带引号的“[]”。

我打赌您应该使用:

elif fixtures == '[]':
fixture是一个字符串,因此字符串应该是带引号的“[]”。

我打赌您应该使用:

elif fixtures == '[]':
fixture是一个字符串,因此字符串应该是带引号的“[]”。

您的代码不起作用,因为您正在将字符串
fixtures
与列表
[]
进行比较。虽然您可以使用
elif fixtures=='[]':
,但将列表转换为那样的字符串,然后去掉括号通常是个坏主意。尝试将其保留为列表并使用
字符串。改为加入

fixtures=literal\u eval(response.content)
如果fixture不是None且len(fixture)>0:#如果有fixture
打印“,\n”.join([“,”.join(str(x)表示fixture中的x)表示fixture中的fixture])#`fixtures`是一个嵌套列表
时间。睡眠(1)
其他:
打印“今天无固定装置:”+str(日期)
时间。睡眠(1)
如果存在装置(例如,
fixtures
包含
[['fixture1','A'],['fixture2','B'],['fixture3','C']]
),您的输出将如下所示

fixture1,A,
固定件2,B,
固定件3,C
与原始代码一样,因为
“,\n”.join
装置的项目之间插入逗号和换行符。如果没有固定装置,输出将保持预期状态。请注意,
date
在打印之前也会转换为字符串

解释: 使用
literal\u eval
加载装置后,此代码检查a)
fixtures
是否为
None
,b)
fixtures
是否包含项目(
len(fixtures)>0
)。如果两者都为真,则进入第一个分支

print“,\n”.join([“,”.join(str(x)表示fixture中的x)表示fixture中的fixture])
在这里,我们在
fixtures
中遍历每个
fixture
,并使用
,“
,”.join
进行连接),但只有在将每个项目转换为字符串(我们不能连接其他类型)后,才能使用
str(x)for fixture
中的x将其连接起来。这为我们提供了一个字符串列表,如
[“fixture1,a”,“fixture2,B”,“fixture3,C”]
。然后,我们将该列表中的所有项目与
,\n“
连接起来,以创建所需的输出

如果其中一个条件为false,则没有固定装置,我们可以输出
“今天没有固定装置”

您的代码不起作用,因为您正在将字符串
fixtures
与列表
[]
进行比较。而您可以使用
elif-fix